当我清除所有最近的应用程序时,Android系统会在Android v7.1.2上终止我的应用程序服务 [英] When I clear all recent apps, android system kills my app service on Android v7.1.2

查看:181
本文介绍了当我清除所有最近的应用程序时,Android系统会在Android v7.1.2上终止我的应用程序服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此处输入图片描述我要创建一个在后台运行的应用。我已经尝试了所有stackoverflow解决方案。当我自己清除该应用程序时,这些解决方案都可以使用。但是当我执行清除所有最近的应用程序时。服务停止。我使用的是Android v7.1.2 Android牛轧糖。

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!");
}


推荐答案

感谢@ code4rox。我使用的是小米手机,因此必须启用自动启动权限。下面的代码运行良好。

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

这篇关于当我清除所有最近的应用程序时,Android系统会在Android v7.1.2上终止我的应用程序服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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