Reputation: 347
I have set a work manager to do task at specific time periodically. But It is working with other android version like 25 or lower. When I run it on API 28 It is going to stop firing a broadcast when I clear app from recent lists.
I have scheduled work manager like this in activity :
OneTimeWorkRequest mywork = new OneTimeWorkRequest.Builder(MyWorker.class).setInitialDelay(15, TimeUnit.SECONDS).build();
WorkManager.getInstance().enqueue(mywork);
This is my Worker class :
public class MyWorker extends Worker {
@NonNull
@Override
public Worker.WorkerResult doWork() {
// Do the work here
Mylogger.getInstance().printLog("MyWorr","doWork()");
//fire broadcast from here
Intent broadcastIntent = new Intent(getApplicationContext(), TimeMatchingBroadcastReceiver.class);
getApplicationContext().sendBroadcast(broadcastIntent);
CommonUtils.scheduleWorkManager();
// Indicate success or failure with your return value:
return WorkerResult.SUCCESS;
// (Returning RETRY tells WorkManager to try this task again
// later; FAILURE says not to try again.)
}
}
I want execute work manager even when I close or clear app from recent list on all version of android. Thanks.
Upvotes: 3
Views: 3498
Reputation: 91
As per the ankuranurag2 answer it is true that some android brands are force stopping the application after killing from the recent. check this issue in the work manager google issue tracker https://issuetracker.google.com/issues/110745313 for tag #2 .
Even I am also tried to run the job in background after killing app from recent (device - HUAWEI BKL-L09) but not able to do that.
Checked for the auto launch option after changing it to manually then it was working fine. But this is not a good idea to for UX to tell the user to change the auto launch option and we cant able to find whether it is on or off.
After searching I have given a patch fix, I think it is not a better solution but can check it is working for me.
1st Create a service which identifies killing the app process I refered to this url https://stackoverflow.com/a/42856811/8264148
UserKillingService
class UserKillingService : Service() {
override fun onBind(intent: Intent?)= null
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int)= Service.START_NOT_STICKY
override fun onDestroy() {
super.onDestroy()
}
override fun onTaskRemoved(rootIntent: Intent?) {
val i = Intent(this,StartUpActivity::class.java)
i!!.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
i!!.putExtra("USER_KILLED", "USER_KILLED")
startActivity(i)
//stop the service
stopSelf()
}
}
Configure the service in manifest
<service android:name=".UserKillingService"
android:stopWithTask="false"/>
This Service calls the StartUpActivity in onTaskRemoved and Stops the service which is not required at all.
this activity is a main launcher activity with no UI
StartUpActivity
<activity android:name=".StartUpActivity"
android:theme="@android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
or you can use android:theme = "@android:style/Theme.Translucent.NoTitleBar"
Inside onCreate of StartUpActivity
if (intent.hasExtra("USER_KILLED")) {
//finish the instance
finish()
} else {
SplashActivity::class.start(this@StartUpActivity,finish = true)
}
This Startupactivity will finish without force stop the application. This activity calls splashActivity when it is not triggered from UserKillingService.
Inside SplashActivity onCreate call the UserKillingService
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
startService(Intent(baseContext,UserKillingService::class.java))
Please tell me if you get any better solution. Sorry for my english
Upvotes: 7
Reputation: 2441
If you go through THE LINK, you will find:
Unfortunately, some devices implement killing the app from the recents menu as a force stop. Stock Android does not do this. When an app is force stopped, it cannot execute jobs, receive alarms or broadcasts, etc. So unfortunately, it's infeasible for us to address it - the problem lies in the OS and there is no workaround.
It is a known issue. To save battery, many manufacturers force close the app, thus cancelling all the period tasks, alarms and broadcast recievers etc. Major manufacturers being OnePlus(you have option to toogle),Redmi,Vivo,Oppo,Huwaei.
Each of these devices have AutoStartManagers/AutoLaunch/StartManager type of optimization managers. Which prevent the background activities to start again. You will have to manually ask the user to whitelist your application, so that app can auto start its background processess. Follow THIS and THIS link, for more info.
The methods to add to whitelist for different manufactures are give in this stackoverflow answer. Even after adding to whitelist, your app might not work because of DOZE Mode, for that you will have to ignore battery otimizations
Also in case you might be wondering, apps like Gmail/Hangout/WhatsApp/Slack/LinkedIn etc are already whitelisted by these AutoStart Managers. Hence, there is no effect on their background processes. You always receive timely updates & notifications.
P.S: Copied from my own answer HERE
Upvotes: 1