我对应用程序有一个奇怪的问题,我同时设置了活动识别过渡api和融合的位置服务。当应用程序在前台运行时,它运行良好,当过渡发生时,它会在logcat上成功打印出新的Transition并继续发送位置更新。
但是,当应用程序置于后台时,使用前景服务会继续发送位置更新,但是当检测到过渡时,该应用程序会在logcat上打印过渡,但在崩溃后立即崩溃..... < / p>
-24919 / com.mts.salestracker E / ActivityThread:服务com.google.android.gms.location.sample.locationupdatesforegroundservice.LocationUpdatesService泄漏了IntentReceiver com.google.android.gms.location.sample.locationupdatesforegroundservice.LocationUpdatesService $最初在此处注册的TransitionsReceiver @ 8ebc3c7。您是否缺少对unregisterReceiver()的调用? android.app.IntentReceiverLeaked:服务com.google.android.gms.location.sample.locationupdatesforegroundservice.LocationUpdatesService泄漏了最初在此处注册的IntentReceiver com.google.android.gms.location.sample.locationupdatesforegroundservice.LocationUpdatesService$TransitionsReceiver@8ebc3c7。您是否缺少对unregisterReceiver()的调用? 在android.app.LoadedApk $ ReceiverDispatcher。(LoadedApk.java:1344) 在android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:1121) 在android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1415) 在android.app.ContextImpl.registerReceiver(ContextImpl.java:1388) 在android.app.ContextImpl.registerReceiver(ContextImpl.java:1376) 在android.content.ContextWrapper.registerReceiver(ContextWrapper.java:613) 在com.google.android.gms.location.sample.locationupdatesforegroundservice.LocationUpdatesService.onCreate(LocationUpdatesService.java:181) 在android.app.ActivityThread.handleCreateService(ActivityThread.java:3443) 在android.app.ActivityThread.-wrap4(未知来源:0) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1712) 在android.os.Handler.dispatchMessage(Handler.java:105) 在android.os.Looper.loop(Looper.java:173) 在android.app.ActivityThread.main(ActivityThread.java:6698) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
前台服务:----
创建时
@Override
public void onCreate() {
mFusedLocationClient =
LocationServices.getFusedLocationProviderClient(this);
Log.i(TAG, "Oncreate");
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
onNewLocation(locationResult.getLastLocation());
}
};
createLocationRequest();
startService(new Intent(getApplicationContext(),
LocationUpdatesService.class));
requestLocationUpdates();
getLastLocation();
Intent intent = new Intent(TRANSITIONS_RECEIVER_ACTION);
mPendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
if(mTransitionsReceiver==null) {
mTransitionsReceiver = new TransitionsReceiver();
registerReceiver(mTransitionsReceiver, new IntentFilter(TRANSITIONS_RECEIVER_ACTION));
setupActivityTransitions();
}
HandlerThread handlerThread = new HandlerThread(TAG);
handlerThread.start();
mServiceHandler = new Handler(handlerThread.getLooper());
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
启动
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "Service started");
// Set the Notification Channel for the Notification Manager.
mNotificationManager.createNotificationChannel(mChannel);
startForeground(NOTIFICATION_ID, getNotification());
// Tells the system to not try to recreate the service after it has been killed.
return START_STICKY;
}
OnUnbind
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "Last client unbound from service");
// Unregister the transitions:
ActivityRecognition.getClient(this).removeActivityTransitionUpdates(mPendingIntent)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.i(TAG, "Transitions successfully unregistered.");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e(TAG, "Transitions could not be unregistered: " + e);
}
});
if(mTransitionsReceiver!=null) {
unregisterReceiver(mTransitionsReceiver);
mTransitionsReceiver=null;
}
}
毁灭
@Override
public void onDestroy() {
mServiceHandler.removeCallbacksAndMessages(null);
}
BroadCast接收者
public class TransitionsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (!TextUtils.equals(TRANSITIONS_RECEIVER_ACTION, intent.getAction())) {
Log.v(TAG, "Received an unsupported action in TransitionsReceiver: action="
+ intent.getAction());
return;
}
if (ActivityTransitionResult.hasResult(intent)) {
ActivityTransitionResult result = ActivityTransitionResult.extractResult(intent);
for (ActivityTransitionEvent event : result.getTransitionEvents()) {
String activity = toActivityString(event.getActivityType());
String transitionType = toTransitionType(event.getTransitionType());
accTransition = activity+" "+transitionType;
Log.v(TAG,"Transition: "
+ activity + " (" + transitionType + ")" + " "
+ new SimpleDateFormat("HH:mm:ss", Locale.US)
.format(new Date()));
}
ChangeLocationRequestIntervals(accTransition);
}
}
private String toActivityString(int activity) {
switch (activity) {
case DetectedActivity.STILL:
return "STILL";
case DetectedActivity.WALKING:
return "WALKING";
default:
return "UNKNOWN";
}
}
private String toTransitionType(int transitionType) {
switch (transitionType) {
case ActivityTransition.ACTIVITY_TRANSITION_ENTER:
return "ENTER";
case ActivityTransition.ACTIVITY_TRANSITION_EXIT:
return "EXIT";
default:
return "UNKNOWN";
}
}
清单文件
<receiver android:name="com.google.android.gms.location.sample.locationupdatesforegroundservice.LocationUpdatesService.TransitionsReceiver"
android:enabled="false"
android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.location.sample.locationupdatesforegroundservice.TRANSITIONS_RECEIVER_ACTION"/> // i.e com.example.receiver
</intent-filter>
</receiver>