我希望能够在Google Fit会话开始或结束时启动BroadcastReceiver。我为接收器设置了清单:
<receiver android:name=".YogaSessionStartedBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.fitness.session_start" />
<data android:mimeType="vnd.google.fitness.activity_type/yoga" />
</intent-filter>
</receiver>
<receiver android:name=".YogaSessionEndedBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.fitness.session_end" />
<data android:mimeType="vnd.google.fitness.activity_type/yoga" />
</intent-filter>
</receiver>
我正在我的应用中开始一个活动类型为YOGA的会话:
mSession = new Session.Builder()
.setName(SESSION_NAME)
.setIdentifier(getString(R.string.app_name) + " " + System.currentTimeMillis())
.setDescription("Yoga Session Description")
.setStartTime(Calendar.getInstance().getTimeInMillis(), TimeUnit.MILLISECONDS)
.setActivity(FitnessActivities.YOGA)
.build();
PendingResult<Status> pendingResult =
Fitness.SessionsApi.startSession(mGoogleApiClient, mSession);
但是,我的BroadcastReceivers永远不会调用onReceive。有什么建议吗?
答案 0 :(得分:1)
您是否注册参加会议?
registerForSessions (GoogleApiClient client, PendingIntent intent)
这样的事情:
Intent intent = new Intent(mContext,YogaSessionEndedBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, alarmIntent, 0);
Fitness.SessionsApi.registerForSessions(mClient,pendingIntent);
哪个mClient是您的GoogleApiClient,而mContext是您的活动背景。