2013-06-26 65 views
0

当我尝试使用LocalBroadcastManager发送来自ScheduledExecutorService的任何操作时,看起来真正的操作尚未发送(或尚未发送)。下面是代码示例:从ScheduledExecutorService发送广播

public class SomeService extends IntentService { 
    private static final String TAG = SomeService.class.getSimpleName(); 
    public static final String ACTION = "some-action"; 

    private ScheduledExecutorService scheduledTaskExecutor = Executors.newScheduledThreadPool(1); 

    public SomeService() { 
     super(TAG); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     String action = intent.getAction(); 
     if (action.equals("scheduled_action")) { 
      scheduledTaskExecutor.schedule(new ScheduledAction(ACTION), 10, SECONDS); 
     } else if (action.equals("send_it_now")) { 
      sendAction(ACTION); 
     } 
    } 

    private void sendAction(String action) { 
     Log.d(TAG, "send action [" + action + "]"); 
     Intent intent = new Intent(action); 
     LocalBroadcastManager.getInstance(SomeService.this).sendBroadcast(intent); 
    } 

    // it also could be Callable, but effect is pretty the same 
    private class ScheduledAction implements Runnable { 
     final String action; 

     ScheduledAction(String action) { 
      this.action = action; 
     } 

     @Override 
     public void run() { 
      sendAction(action); 
     } 
    } 
} 

取决于进来的行动,服务发送ACTION马上或10秒后发送。这是被认购ACTION活动:

public class SomeActivity extends Activity { 
    private static final String TAG = SomeActivity.class.getName(); 
    private BroadcastReceiver actionReceiver = new ActionReceiver(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     IntentFilter filter = new IntentFilter(); 
     filter.addAction(SomeService.ACTION); 
     LocalBroadcastManager.getInstance(this).registerReceiver(actionReceiver, filter); 
    } 

    @Override 
    protected void onDestroy() { 
     LocalBroadcastManager.getInstance(this).unregisterReceiver(actionReceiver); 
     super.onDestroy(); 
    } 

    private class ActionReceiver extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 
      Log.d(TAG, "received action [" + action + "]"); 
     } 
    } 
} 

send_it_now一切的情况下按预期工作:

Intent intent = new Intent(..., SomeService.class); 
intent.setAction("send_it_now"); 
startService(intent); 

我看到打印到logcat的适当的消息:

... 
SomeService send action [some-action] 
SomeActivity received action [some-action] 
... 

但是,当我尝试使用scheduled_action

Intent intent = new Intent(..., SomeService.class); 
intent.setAction("scheduled_action"); 
startService(intent); 

我看动作被发送,但它并没有收到活动:

... 
SomeService send action [some-action] 
... 

那么,有谁能够告诉解释什么不对的代码,或者至少表明在哪里可以找到解释的方向?

一些更新。如果我使用TimerTimerTask出于同样的目的,我使用了ScheduledExecutorServiceScheduledFuture - 它的工作原理!

回答

0

既然你是不是UI线程上,试试这个:

public void run() { Looper.prepare(); sendAction(action); Looper.loop(); }

public void run() 
{ 
    new AsyncTask<Void, Void, Void>() 
    { 
     @Override 
     protected Void doInBackground(Void... params) 
     { 
      return null; 
     } 

     protected void onPostExecute(Void result) 
     { 
      sendAction(action); 
     } 
    }.execute(); 
} 
+0

我猜测它不应该依赖于UI线程。无论如何,我已经检查过,没有任何帮助。 – Eugene