从小米的最新列表中清除后,服务无法重新启动

时间:2019-02-02 05:15:51

标签: android service broadcastreceiver xiaomi

我已经从清单启动的服务,该服务正在运行细当它开始首次。它显示2个正在运行的应用程序-1个进程和1个服务。但是当我从最近的任务列表中刷出我的应用程序时,它不会在小米设备上再次自动启动我的服务。它成功地自动开始在像联想的其它设备。

这是我的服务班级:-

public class StickyService extends Service {

    private static final String TAG = "StickyService";

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

        startReceiver();

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
            startMyOwnForeground();
        } else {
            startForeground(1, new Notification());
        }
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private void startMyOwnForeground() {
        startTimer();
        String NOTIFICATION_CHANNEL_ID = "e xample.permanence";
        String channelName = "Background Service";
        NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
        chan.setLightColor(Color.BLUE);
        chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        assert manager != null;
        manager.createNotificationChannel(chan);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        Notification notification = notificationBuilder.setOngoing(true)
                .setContentTitle("App is running in background")
                .setPriority(NotificationManager.IMPORTANCE_MIN)
                .setCategory(Notification.CATEGORY_SERVICE)
                .build();
        startForeground(2, notification);
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e(TAG, "onUnbind");
        return super.onUnbind(intent);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand");
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy");

        sendBroadcast(new Intent("GET_BUZZER"));

        //create an intent that you want to start again.
        Intent intent = new Intent(getApplicationContext(), StickyService.class);
        PendingIntent pendingIntent = PendingIntent.getService(this, 1, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime() + 2000, pendingIntent);
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        Log.e(TAG, "onTaskRemoved");

        sendBroadcast(new Intent("GET_BUZZER"));

        //create an intent that you want to start again.
        Intent intent = new Intent(getApplicationContext(), StickyService.class);
        PendingIntent pendingIntent = PendingIntent.getService(this, 1, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime() + 2000, pendingIntent);

        super.onTaskRemoved(rootIntent);
    }

    private void startReceiver() {
        RestartReceiver receiver = new RestartReceiver();
        IntentFilter localIntentFilter = new IntentFilter();
        localIntentFilter.addAction("GET_BUZZER");
        registerReceiver(receiver, localIntentFilter);
        startService(new Intent(this, StickyService.class));
    }

    public class RestartReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e(TAG, "Inside receiver inline class");

            Intent myIntent = new Intent(context, StickyService.class);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(myIntent);
            } else {
                context.startService(myIntent);
            }
        }
    }

}

我注册我的广播接收器中的Java代码(未在清单)。我使用了服务类来注册广播接收器。

清单:-

<service
    android:name=".ForceCloseAutoStart.StickyService"
    android:process=":StickyService" />

我还对小米设备进行了以下更改:-Settings->Battery->Manage Apps Battery Usage仍然无法正常工作。

请与任何溶液在此设备其工作的帮助。

1 个答案:

答案 0 :(得分:0)

我觉得你需要启用自动启动的服务在你的小蜜设备。

  1. 打开小米设备上的“安全性”菜单。
  2. 点击权限。
  3. 点击自动启动。
  4. 滑动以为您的应用启用自动启动。

在这里检查:-http://nine-faq.9folders.com/articles/8772-how-to-manage-autostart-service-on-the-xiaomi-devices