【问题标题】:Starting an ActivityRecognitionService from within another active Service从另一个活动服务中启动 ActivityRecognitionService
【发布时间】:2015-08-20 13:28:34
【问题描述】:

我在让我的 ActivityRecognition 服务保持运行时遇到问题。我目前有一个在后台连续运行的服务(GService)。我想在 GService 中启动 ActivityRecognition 服务,并让 ActivityRecognition 服务将活动结果广播回 GService。我能够启动服务并接收到它正在运行的反馈,并且我还从意图处理程序获得了一个结果(没有实际数据),但再也没有。

这是我的持续服务中设置意图和待定意图的代码部分:

@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 个广播接收器。我不知道这是否会导致问题。

【问题讨论】:

    标签: android android-intent android-service


    【解决方案1】:

    看来我已经解决了这个问题。我的 GService 中有第二个意图用于广播。从这个线程(Pending intent works correctly for first notification but not for the rest) 可以看出,如果使用了多个意图,它们必须是唯一的。因此,我在声明我的意图 intent.setAction(Long.toString(System.currentTimeMillis())); 时添加了一行代码,这足以将其与系统的其他意图区分开来。一旦我这样做了,我就开始从意图服务接收活动广播,并且仍然从 GService 例程中接收位置请求。

    【讨论】:

      猜你喜欢
      • 2015-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多