Android O服务停止 [英] Android O service stops

查看:64
本文介绍了Android O服务停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

my background service stops on android 8.1.0

MainClass:

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:

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>

我看到我的日志删除的活动成功更新!"几秒钟后

I see my log "Removed activity updates successfully!" after couple of seconds

推荐答案

在Android 8中,不允许使用后台服务.代替context.startService(),您必须调用context.startForegroundService().然后在服务的onCreate()中,您必须调用startForeground()并提供正在进行的通知,以向用户显示您的服务正在运行.

In Android 8 background services are not allowed. Instead of context.startService() you have to call context.startForegroundService(). And then in onCreate() of your service you must call startForeground() and provide a ongoing Notification to show the user your service is running.

因此,onCreate()应该如下所示:

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

该通知可以是正常的正在进行中的通知.例如:

The notification could be a normal ongoing notification. For example:

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);

这篇关于Android O服务停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆