When I clear all recent apps, android system kills my app service on Android v7.1.2

偶尔善良 提交于 2019-12-13 02:49:57

问题


enter image description hereI want to create an app which runs in the background. I have tried all stackoverflow solutions. Those solutions are all working when the app is cleared by myself. But when I'm performing "clear all recent apps". The service stops. I'm using Android v7.1.2 Android Nougat.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
            log("Received start command flags:%d, startId:%d", flags, startId);
            Notification.Builder builder = new Notification.Builder(SOSService.this);
            builder.setSmallIcon(R.mipmap.ic_launcher)
                    .setContentText("Ambysure SOS App is running")
                    .setAutoCancel(false);
            Notification notification = builder.getNotification();
            notification.flags |= Notification.FLAG_NO_CLEAR
                    | Notification.FLAG_ONGOING_EVENT; 
            startForeground(100, notification);
            return START_STICKY;
        }



@Override
public void onDestroy() {
    super.onDestroy();
    Log.i("EXIT", "ondestroy!");
    Intent myIntent = new Intent(getApplicationContext(), SOSService.class);
    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent, 0);
    AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 10);
    alarmManager1.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    Toast.makeText(getApplicationContext(), "Start Alarm", Toast.LENGTH_SHORT).show();
}

@Override
public void onTaskRemoved(Intent rootIntent) {
    super.onTaskRemoved(rootIntent);
    Log.i("TASKREMOVED", "onTaskRemoved!");
    Intent myIntent = new Intent(getApplicationContext(), SOSService.class);
    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent, 0);
    AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 10);
    alarmManager1.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
   Log.i("TASKREMOVED", "Start Alarm!");
}

回答1:


Some mobiles not allow to run app in background like oppo,xiaomi,huawei etc.. to solve this issue try this Link




回答2:


Thanks @code4rox. I'm using Xiaomi mobile, So it's required to enable auto start permission. Below code is working well.

if (Build.BRAND.equalsIgnoreCase("Xiaomi")) {
    Intent intent1 = new Intent();
    intent1.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
    startActivity(intent1);
}


来源:https://stackoverflow.com/questions/51303179/when-i-clear-all-recent-apps-android-system-kills-my-app-service-on-android-v7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!