0

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!");
}
Saravanan S
  • 123
  • 1
  • 13
  • use a foreground service: https://developer.android.com/guide/components/services#Foreground ||| Or a scheduled job: https://developer.android.com/topic/performance/scheduling – HedeH Jul 12 '18 at 10:32

2 Answers2

1

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

code4rox
  • 941
  • 9
  • 34
  • My mobile is Redmin 5A. When i restart it will automatically booting in background, thats working good. But I don't know why it's not working at "Clear All Recent Apps". It's possible or not in android? – Saravanan S Jul 12 '18 at 11:04
  • @saNarOcKingZ your broadcast receiver working well but your service not working in background... the problem that I think is **autostart** security in **xiaomi** mobiles ... if u want run service in background.. you must allow the **autostart** security.... – code4rox Jul 12 '18 at 11:14
  • Thankyou so much @code4rox. You are correct, that is auto start permission issue. Now it's working. But a small problem, every time it's navigating to auto start page. – Saravanan S Jul 12 '18 at 11:37
  • you welcome @saNarOcKingZ ... make new intro activity with button.. and other staff .. on button click listener start autostart intent ... – code4rox Jul 12 '18 at 12:06
  • Hey, I tried to give my vote. "They told Thanks for the feedback! Votes cast by those with less than 15 reputation, but not showing in publicly. Sorry, i will do once archive 15 ;D" – Saravanan S Jul 12 '18 at 12:26
  • Done! Finally i reached:D – Saravanan S Aug 09 '18 at 15:32
1

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);
}
Saravanan S
  • 123
  • 1
  • 13