我在让ActivityRecognition Service继续运行时遇到问题。我目前有一个在后台持续运行的服务(GService)。我想在GService中启动ActivityRecognition服务,并让ActivityRecognition服务将活动结果广播回GService。我能够启动服务并接收它正在运行的反馈,并且我也从intent处理程序中获得一个结果(没有实际数据),但是再也没有。
以下是我的连续服务设置意图,未决意图的代码部分:
@Override
public void onConnected(Bundle bundle) {
Log.d(TAG, "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected());
startLocationUpdates();
//start process to receive activity updates
Intent intent = new Intent(this, DetectedActivitiesIntentService.class);
PendingIntent mActivityRecognitionPendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mGoogleApiClient, ActivityConstants.DETECTION_INTERVAL_MILLISECONDS_MOVING,
mActivityRecognitionPendingIntent).setResultCallback(this);
startService(intent); // this should start the DetectedActivitiesIntentService
这是GService中的广播接收器:
public class ActivityDetectionBroadcastReceiver extends BroadcastReceiver {
protected static final String TAG_AR = "ADRR";
@Override
public void onReceive(Context context, Intent intent){
//ArrayList<DetectedActivity> updatedActivities =
// intent.getParcelableArrayListExtra(ActivityConstants.ACTIVITY_EXTRA);
//updateDetectedActivitiesList(updatedActivities);
String action = intent.getAction();
if(action.equals("com.gt.useractivity"))
{
Log.d(TAG_AR, "received broadcast from Activity service");
// below line should grab the resulting string activity from the intent and log it.
Log.d(TAG_AR, "activity is : " + intent.getExtras().getString(ActivityConstants.ACTIVITY_EXTRA));
}
}
}
以下是ActivityRecognition服务代码:
public class DetectedActivitiesIntentService extends IntentService {
protected static final String TAG = "ADIS";
/**
* This constructor is required, and calls the super IntentService(String)
* constructor with the name for a worker thread.
*/
public DetectedActivitiesIntentService() {
// Use the TAG to name the worker thread.
super(TAG);
Log.d(TAG, "Activity service started....");
}
@Override
public void onCreate() {
super.onCreate();
}
/**
* Handles incoming intents.
* @param intent The Intent is provided (inside a PendingIntent) when requestActivityUpdates()
* is called.
*/
@Override
protected void onHandleIntent(Intent intent) {
if(ActivityRecognitionResult.hasResult(intent))
{
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
Intent localIntent = new Intent(ActivityConstants.BROADCAST_ACTION);
// Get the list of the probable activities associated with the current state of the
// device. Each activity is associated with a confidence level, which is an int between
// 0 and 100.
ArrayList<DetectedActivity> detectedActivities = (ArrayList) result.getProbableActivities();
// Log each activity.
Log.i(TAG, "activities detected");
for (DetectedActivity da: detectedActivities) {
Log.i(TAG, ActivityConstants.getActivityString(da.getType()) + " " + da.getConfidence() + "%");
}
String activity = result.getMostProbableActivity().toString(); // get the activity and convert to string
// Broadcast the list of detected activities.
//localIntent.putExtra(ActivityConstants.ACTIVITY_EXTRA, detectedActivities);
//localIntent.setAction("com.gt.useractivity");
localIntent.putExtra(ActivityConstants.ACTIVITY_EXTRA, activity); // set the activity string to be transmitted
LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
}
else{
Log.d(TAG, "Intent had no activity data....");
}
}
}
此活动识别示例基于Google Github示例。
我在使用PendingIntent时找到的所有示例都是从主活动调用的,而不是从服务调用的。我显然做错了什么,但我无法弄清楚。任何意见,将不胜感激。我还应该注意到我的GService中有2个广播接收器。我不知道这是否会引起问题。
答案 0 :(得分:0)
看起来我已经解决了这个问题。我的GService中有第二个用于广播的意图。从我从这个线程(Pending intent works correctly for first notification but not for the rest)可以看出,如果有多个意图被使用,它们必须是唯一的。因此,我在声明我的意图intent.setAction(Long.toString(System.currentTimeMillis()));
时添加了一行代码,这足以将其与系统的其他意图区分开来。一旦我这样做,我就开始从意向服务接收活动广播,并且仍然从GService例程中接收位置请求。