java - 多次执行 Runnable post 到 Looper 的 Activity

标签 java android

我已经在 Android 中实现了一个应用程序。共有三个组件:MainActivity、MyWorker 和 PublishService。 MyWorker 从 Android 的 SensorManager 获取传感器数据,然后广播到 MainActivity。当 MainActivity 获取消息时,它会调用发布函数以将其推送到我的 PublishService。请看下图: enter image description here

下面我展示了我的代码演示:

public class MyActivity extends Activity implements  Runnable {
  public class PushReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent i){
      Result = i.getStringExtra("Result");
      Log.d("onReceive_72", Result);
      mSend();
    }
  }

  private void  mSend(){
    Log.d("mSend_80","sent");
    handler.post(this);
  }

  @Override
  public void run() {
    Log.d("run_87", Result);
    handler.postDelayed(this, 500);
    Publish(Result); // publish function 
  }

  // more code here <--->

}

我的问题是当应用程序运行时,Logcat 多次显示 "run_87": Result"onReceive_72": Result"mSend_80":"sent " 只出现一次。例如:

05-11 11:42:01.286  11631-11631/example I/onReceive_72﹕ Result
05-11 11:42:01.286  11631-11631/example I/mSend_80﹕ sent
05-11 11:42:01.286  11631-11631/example I/run_87﹕ Result
05-11 11:42:01.786  11631-11631/example I/run_87﹕ Result
05-11 11:42:01.946  11631-11631/example I/onReceive_72﹕ Result
05-11 11:42:01.946  11631-11631/example I/mSend_80﹕ sent
05-11 11:42:01.946  11631-11631/example I/run_87﹕ Result
05-11 11:42:02.286  11631-11631/example I/run_87﹕ Result
05-11 11:42:02.446  11631-11631/example I/run_87﹕ Result
05-11 11:42:02.556  11631-11631/example I/onReceive_72﹕ Result
05-11 11:42:02.556  11631-11631/example I/mSend_80﹕ sent
05-11 11:42:02.556  11631-11631/example I/run_87﹕ Result
05-11 11:42:02.786  11631-11631/example I/run_87﹕ Result
05-11 11:42:02.946  11631-11631/example I/run_87﹕ Result
05-11 11:42:03.056  11631-11631/example I/run_87﹕ Result
05-11 11:42:03.221  11631-11631/example I/onReceive_72﹕ Result
05-11 11:42:03.221  11631-11631/example I/mSend_80﹕ sent
05-11 11:42:03.221  11631-11631/example I/run_87﹕ Result
05-11 11:42:03.286  11631-11631/example I/run_87﹕ Result
05-11 11:42:03.461  11631-11631/example I/run_87﹕ Result
05-11 11:42:03.556  11631-11631/example I/run_87﹕ Result
05-11 11:42:03.721  11631-11631/example I/run_87﹕ Result
05-11 11:42:03.786  11631-11631/example I/run_87﹕ Result
05-11 11:42:03.836  11631-11631/example I/onReceive_72﹕ Result
05-11 11:42:03.836  11631-11631/example I/mSend_80﹕ sent
05-11 11:42:03.836  11631-11631/example I/run_87﹕ Result
05-11 11:42:03.961  11631-11631/example I/run_87﹕ Result
05-11 11:42:04.056  11631-11631/example I/run_87﹕ Result
05-11 11:42:04.221  11631-11631/example I/run_87﹕ Result
05-11 11:42:04.286  11631-11631/example I/run_87﹕ Result
05-11 11:42:04.336  11631-11631/example I/run_87﹕ Result
05-11 11:42:04.461  11631-11631/example I/run_87﹕ Result
05-11 11:42:04.486  11631-11631/example I/onReceive_72﹕ Result
05-11 11:42:04.486  11631-11631/example I/mSend_80﹕ sent
05-11 11:42:04.486  11631-11631/example I/run_87﹕ Result
05-11 11:42:04.556  11631-11631/example I/run_87﹕ Result
05-11 11:42:04.721  11631-11631/example I/run_87﹕ Result
05-11 11:42:04.786  11631-11631/example I/run_87﹕ Result
05-11 11:42:04.836  11631-11631/example I/run_87﹕ Result
05-11 11:42:04.961  11631-11631/example I/run_87﹕ Result
05-11 11:42:04.986  11631-11631/example I/run_87﹕ Result
05-11 11:42:05.056  11631-11631/example I/run_87﹕ Result

如您所见,对于每个圆圈,它以 onReceive_72mSend_80 开始,然后生成 run_87run_87 的出现超出预期。而下一次,run_87 往往会产生比这次更多的结果。我不知道为什么会这样?立刻,我认为这是由于 Runnable 而发生的,但我不知道如何解决。能否请您给我适当的建议,在此先感谢您。

最佳答案

我已经自己解决了,但我想在这里向可能遇到此问题的每个人展示答案。原因是多线程不是有效管理线程。事实是我们的系统不能保证多线程同时运行。例如,我放了一些代码行

private Boolean receiveResult = false;

在onReceive 中,receiveResult = true;

if (receiveResult) {
     receiveResult = false;
     Log.i("run_87", "Result");
     Publish(Resulty);
}

MyActivity 的线程具有更高的优先级,因此它被调用的时间比 MyWorkerService 多。

关于java - 多次执行 Runnable post 到 Looper 的 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30159905/

相关文章:

java - 在python/java中编辑acp的内容

android - 从其 Activity 启动的服务是否在新线程中运行?

android - setProgressDrawble 正在填充整个进度条

android - 以编程方式将 GIF 转换为 PNG

java - Android 和 Google Play : how to get/getting provider tag data?

Java - 将文件从 Explorer 移动到 Java GUI - 那是什么类型的事件?

java - 使用 AsyncTask 时屏幕闪烁

java - 如何删除空指针异常

java - 不可修改集合中的 ConcurrentModificationException

android - 在 Android 上访问 native 串口