如何从onHandleIntent(Android)获取信息

时间:2013-02-25 23:03:37

标签: android android-intent toast

我正在处理IntentService,我在循环中生成值。在覆盖方法onStartCommand()onDestroy()中,可以使用Toast显示消息。我也想在Toast中使用onHandleIntent(),但它不起作用。我知道有更好的方法(文件,属性等)来显示值,但是在创建它们时我会立即需要这些值。最后(仅限控制,如果它正常工作)。拜托,你能帮我辨认一下这个问题吗?提前致谢。

public class HelloIntentService extends IntentService {

  public HelloIntentService() {
      super("HelloIntentService");
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
      return super.onStartCommand(intent,flags,startId);
  }

  @Override
  protected void onHandleIntent(Intent intent) {

      long endTime = System.currentTimeMillis() + 5*1000;
      while (System.currentTimeMillis() < endTime) {
          synchronized (this) {
              try {
                  wait(endTime - System.currentTimeMillis());
              } catch (Exception e) {
              }
          }
      }}

  @Override
  public void onDestroy()
  {
      Toast.makeText(this, "Service is being destroyed now.", Toast.LENGTH_SHORT).show();
  }
}

1 个答案:

答案 0 :(得分:0)

onHandleIntent在每个the source code的后台运行在一个单独的线程上,因此没有任何更新UI的能力。我建议使用日志语句而不是Toasts来显示您的信息:

Log.d("HelloIntentService","service starting");
相关问题