MainActivity中的BroadcastReceiver不接收广播意图

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MainActivity中的BroadcastReceiver不接收广播意图相关的知识,希望对你有一定的参考价值。

我是android编程的新手,所以我可能会犯下非常愚蠢的错误。我正在编写一个简单的应用程序来收集活动数据。我有一个MainActivity和一个ActivityRecognizedService来查询Google的活动识别API。我正在尝试接收从ActivityRecognizedServiceMainActivity的公认活动,以便我以后可以定期将记录写入文件以及我将收集的其他数据。问题是,我将用于在文件上写入数据的线程没有接收广播消息。这是该项目的MWE。

在:ActivityRecognizedService.java

public class ActivityRecognizedService extends IntentService {

    public static final String LOCAL_BROADCAST_NAME = new String("API.ACTIVITY.BROADCAST_NAME");

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

    public ActivityRecognizedService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        if (ActivityRecognitionResult.hasResult(intent)) {
            ActivityRecognitionResult activityRecognitionResult = ActivityRecognitionResult.extractResult(intent);
            Log.e("API.ACTIVITY.DETECTION", "Activity detected: " + activityRecognitionResult.getMostProbableActivity().toString());
            /* This DOES log the activity when detected */
            LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
        }
    }
}

在MainActivity.java中

public class MainActivity
        extends AppCompatActivity 
        implements GoogleApiClient.ConnectionCallbacks,
            GoogleApiClient.OnConnectionFailedListener {
    private ActivityRecognitionResult activityRecognitionResult;
    private GoogleApiClient googleApiClient;
    private BroadcastReceiver broadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstance) {
         super.onCreate(savedInstance);
         setContentView(R.layout.activity_main);

         /* check permissions, Google Play Services availabililty ... */

         googleApiClient = new GoogleApiClient.Builder(this);
                 .addApi(ActivityRecognition.API)
                 .addConnectionCallbacks(this)
                 .addOnConnectionFailedListener(this)
                 .build();

         broadcastReceiver = new BroadcastReceiver() {
             @Override
             public void onReceive(Context context, Intent intent) {
                 Log.d("API.ACTIVITY.RECEIVE", ActivityRecognitionResult.extractResult(intent).toString());
                 /* This DOES NOT log the activity result */
             }
         }

         googleApiClient.connect();

         // This is the thread I will use to write records on file, to do
         new Thread((Runnable) -> {
             LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, new IntentFilter(ActivityRecognizedService.LOCAL_BROADCAST_NAME));
             while (true) {
                 SystemClock.sleep(250);
                 // do stuff..
             }
         }).start();
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        Intent intent = new Intent(this, ActivityRecognizedService.class);
        PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        ActivityRecognitionClient activityRecognitionClient = ActivityRecognition.getClient(this);
        Task task = activityRecognitionClient.requestActivityUpdates(250, pendingIntent);
}

}

当然,记录接收到的广播不是最终目标,因为我可以将其保留在onHandleIntent()中,而是我需要将提取的结果与我将在最后一个线程中收集的其他数据一起写入文件。我也用registerReceiver方法调用onResume()

答案

我认为问题是你播放了相同的传入Intent。广告没有附加任何行动。因此,您注册了动作“API.ACTIVITY.BROADCAST_NAME”的BroadcastReceiver将不会收到通知。

将动作设置为Intent

@Override
    protected void onHandleIntent(@Nullable Intent intent) {
        Intent sendIntent=new Intent(LOCAL_BROADCAST_NAME);
        LocalBroadcastManager.getInstance(this).sendBroadcast(sendIntent);
    }

以上是关于MainActivity中的BroadcastReceiver不接收广播意图的主要内容,如果未能解决你的问题,请参考以下文章

从 MainActivity 中的另一个 xml 获取元素 [关闭]

MainActivity中的BroadcastReceiver不接收广播意图

为啥在 MainActivity.kt 中的 setOnCLickListener 中调用 AppCompatEditText 文本时无法访问?

如何从 Mainactivity 编辑 Fragment 中的 TextView

从 MainActivity 类中的另一个函数访问 WebView

Android react-native 中的 MainActivity 不存在错误