Android O服务停止

时间:2018-05-21 18:04:25

标签: android android-service

我的后台服务在Android 8.1.0上停止

MainClass:

private void startTracking() {
        if (!isMyServiceRunning()) {
            Intent intent = new Intent(MainActivity.this, BackgroundDetectedActivitiesService.class);
            startService(intent);
        }
    }


private boolean isMyServiceRunning() {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (BackgroundDetectedActivitiesService.class.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }

BackgroundDetectedActivitiesService:

public class BackgroundDetectedActivitiesService extends Service {
    private static final String TAG = BackgroundDetectedActivitiesService.class.getSimpleName();

    private Intent mIntentService;
    private PendingIntent mPendingIntent;
    private ActivityRecognitionClient mActivityRecognitionClient;

    IBinder mBinder = new BackgroundDetectedActivitiesService.LocalBinder();

    public class LocalBinder extends Binder {
        public BackgroundDetectedActivitiesService getServerInstance() {
            return BackgroundDetectedActivitiesService.this;
        }
    }

    public BackgroundDetectedActivitiesService() {

    }


    @Override
    public void onCreate() {
        super.onCreate();

        PowerManager mgr = (PowerManager) getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
        wakeLock.acquire();


        mActivityRecognitionClient = new ActivityRecognitionClient(this);
        mIntentService = new Intent(this, DetectedActivitiesIntentService.class);
        mPendingIntent = PendingIntent.getService(this, 1, mIntentService, PendingIntent.FLAG_UPDATE_CURRENT);
        requestActivityUpdatesButtonHandler();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }

    public void requestActivityUpdatesButtonHandler() {
        Task<Void> task = mActivityRecognitionClient.requestActivityUpdates(
                Constants.DETECTION_INTERVAL_IN_MILLISECONDS,
                mPendingIntent);

        task.addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void result) {
                Log.d("DTAG","Successfully requested activity updates");
            }
        });

        task.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.d("DTAG","Requesting activity updates failed to start");
            }
        });
    }

    public void removeActivityUpdatesButtonHandler() {
        Task<Void> task = mActivityRecognitionClient.removeActivityUpdates(
                mPendingIntent);
        task.addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void result) {
                Log.d("DTAG","Removed activity updates successfully!");
            }
        });

        task.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.d("DTAG","Failed to remove activity updates!");
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        removeActivityUpdatesButtonHandler();
    }
}

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.michlind.---">

    <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".Autostart">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <service
            android:name=".DetectedActivitiesIntentService"
            android:enabled="true"
            android:exported="false" />

        <service android:name=".BackgroundDetectedActivitiesService"></service>
        <service android:name=".NotificationIntentService"></service>
    </application>

</manifest>

我看到我的日志“删除了活动更新成功!”几秒后

1 个答案:

答案 0 :(得分:1)

在Android 8中,不允许使用后台服务。而不是context.startService(),您必须致电context.startForegroundService()。然后在onCreate()您的服务中,您必须致电startForeground()并提供持续通知,以向用户显示您的服务正在运行。

所以,onCreate()应该如下:

@Override
public void onCreate() {
    super.onCreate();
    startForeground(notificationID, your notification);
    // Your logic
}

通知可能是正常的持续通知。例如:

NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);  
notificationManager.createNotificationChannel(channel);


Notification.Builder builder = new Notification.Builder(ctx, "channel_id");
builder.setContentIntent(contentIntent)
        .setSmallIcon(R.drawable.some_img)
        .setContentTitle(res.getString(R.string.your_notif_title))
        .setOngoinf(true);
Notification n = builder.build();

startForeground(1, n);