Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background Service Stop when removing app from recent

Background service is stop, when removing my app from recent in oppo & vivo mobiles, & Broadcast reciever also not working in that case.

like image 208
Yogesh Choudhary Paliyal Avatar asked May 31 '18 10:05

Yogesh Choudhary Paliyal


People also ask

How run service in background android when app is killed?

First, the easiest way to do what you're trying to do is to launch an Android Broadcast when the app is killed manually, and define a custom BroadcastReceiver to trigger a service restart after that. Dear Dr Sabri Allani, If your Service is started by your app then actually your service is running on main process.

How do I stop background services on android?

Stop Services Running in Background Open Settings of the phone. Now, go to the Developer Options. Tap on Running Services. Tap on the app for which you want to limit battery usage, now tap on stop.

Why are apps running in the background?

Background applications can eat up your battery and utilize the resources. There are some applications which probably won't be optimized well, some might be malicious or have malware, or some applications just have a bug. Don't worry! Closing these background apps just takes a little knowledge.


2 Answers

I had same issue with Oppo, Vivo, Mi and etc phones,

  1. after removing from recent applications app was getting killed even services was getting killed

Solution: I had add autostart permissions like this in my application and it worked.

  1. After resolving this issue my app was getting frozen/killed after sometime running in background due to DOZE mode Solution: for this condition this worked and now my app is working in background in any device

  2. After above things do this:

intent.setClassName("com.coloros.oppoguardelf", "com.coloros.powermanager.fuelgaue.PowerConsumptionActivity"); startActivity(intent);

call above intent, it will redirect you to battery option, "Disable Background Freeze, Abnormal Apps Optimization and Doze from \"Energy Saver ->youAPP"

Note: Once you call above intent, there you may get different options to turn off Battery saving options.

like image 123
Amin Pinjari Avatar answered Nov 05 '22 23:11

Amin Pinjari


Yes, You have to return START_STICKY;

Please refer this link :

https://www.tutorialspoint.com/android/android_services.htm

example :

public class MyService extends Service {

@Nullable

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

   @Override
   public int onStartCommand(Intent intent, int flags, int startId) 

{

  // Let it continue running until it is stopped.

Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
      return START_STICKY;

   }

   @Override
   public void onDestroy() {

 super.onDestroy();

Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();

 }
}
like image 21
Hitesh Kanjani Avatar answered Nov 05 '22 22:11

Hitesh Kanjani