【发布时间】:2016-02-16 13:09:34
【问题描述】:
所以我正在使用 Activity Recognition API,我为它创建了一个名为 ActivityRecognitionIntentService 的服务。
我真的希望它一直在后台工作。
问题是应用程序显示在缓存的运行服务中,但在我退出应用程序时关闭并且不在后台运行。
我正在为我的主要活动使用广播接收器来更新 UI。
我正在使用IntentService,我创建了onStartCommand,它返回START_STICKY。
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.ActivityRecognition;
import com.google.android.gms.location.ActivityRecognitionResult;
import com.google.android.gms.location.DetectedActivity;
public class ActivityRecognitionIntentService extends IntentService implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
Context mContext;
GoogleApiClient mGApiClient;
BroadcastReceiver receiver;
private static final String TAG = ActivityRecognitionIntentService.class.getSimpleName();
public ActivityRecognitionIntentService() {
super("ActivityRecognitionIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
if(ActivityRecognitionResult.hasResult(intent)) {
//Extract the result from the Response
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
DetectedActivity detectedActivity = result.getMostProbableActivity();
//Get the Confidence and Name of Activity
int confidence = detectedActivity.getConfidence();
String mostProbableName = getActivityName(detectedActivity.getType());
//Fire the intent with activity name & confidence
Intent i = new Intent("ImActive");
i.putExtra("activity", mostProbableName);
i.putExtra("confidence", confidence);
Log.d(TAG, "Most Probable Name : " + mostProbableName);
Log.d(TAG, "Confidence : " + confidence);
//Send Broadcast to be listen in MainActivity
this.sendBroadcast(i);
}else {
Log.d(TAG, "Intent had no data returned");
}
}
//Get the activity name
private String getActivityName(int type) {
switch (type)
{
case DetectedActivity.IN_VEHICLE:
return "still";
case DetectedActivity.ON_BICYCLE:
return "moving";
case DetectedActivity.ON_FOOT:
return "moving";
case DetectedActivity.WALKING:
return "moving";
case DetectedActivity.STILL:
return "still";
case DetectedActivity.TILTING:
return "still";
case DetectedActivity.UNKNOWN:
return "still";
case DetectedActivity.RUNNING:
return "moving";
}
return "N/A";
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent,flags,startId);
buildGoogleClient();
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
return START_STICKY;
}
private boolean isPlayServiceAvailable() {
return GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext) == ConnectionResult.SUCCESS;
}
private void buildGoogleClient()
{
//Check Google Play Service Available
if (isPlayServiceAvailable()) {
mGApiClient = new GoogleApiClient.Builder(this)
.addApi(ActivityRecognition.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
//Connect to Google API
mGApiClient.connect();
}
}
@Override
public void onConnected(Bundle bundle) {
Intent i = new Intent(this, ActivityRecognitionIntentService.class);
PendingIntent mActivityRecongPendingIntent = PendingIntent
.getService(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
Log.d(TAG, "connected to ActivityRecognition");
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mGApiClient, 0, mActivityRecongPendingIntent);
}
@Override
public void onConnectionSuspended(int i) {
Log.d(TAG, "Suspended to ActivityRecognition");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG, "Not connected to ActivityRecognition");
}
}
我也在清单中声明了服务
<service android:name=".ActivityRecognitionIntentService"
android:exported="false"/>
以及必要的权限
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
【问题讨论】:
-
IntentService按设计自动关闭,一旦onHandleIntent()返回。它专为事务性工作而设计。如果您希望服务运行更长时间,请使用Service,而不是IntentService。 -
如果我从 Intent 服务切换到服务,我们会看到哪些变化以及有多少变化?
-
@NamanMittal 你找到解决办法了吗?
标签: android broadcastreceiver intentservice activity-recognition