MainActivity中的BroadcastReceiver不接收广播意图

时间:2018-03-03 15:27:49

标签: java android broadcastreceiver activity-recognition

我在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()调用。

0 个答案:

没有答案