问题
I make service to Toast a text always but just in huawei device service killed when i kill app.
I don't know why?
My service is:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
onTaskRemoved(intent);
new CountDownTimer(99999999L,5000 ) {
@Override
public void onTick(long arg0) {
Toast.makeText(getApplicationContext(), "hihihi", Toast.LENGTH_LONG).show();
}
@Override
public void onFinish() {
}
}.start();
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
回答1:
An old question, but I think it’s important to know that HUAWEI has a feature called “power-intensive app monitor“.
It kills every app that runs in the background for a long time unless user gives special permissions to it.
The path to do this: Settings -> Security & privacy -> Location services -> recent location requests: YOUR APP NAME -> Battery -> uncheck Power-intensive prompt, App launch: Manage manually: check all three positions: Auto-launch, secondary launch, run in background.
I don’t know if there's a way to do this programmatically. I think the best way is to create a sort of help activity and explain the user what to do if application won’t work.
回答2:
You should stop the service on the method @Override onDestroy()
来源:https://stackoverflow.com/questions/48559465/service-killed-when-app-cloes-just-in-huawei-device