1

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");
} 
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
h.hejabi
  • 85
  • 1
  • 9

2 Answers2

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.

MarkWalczak
  • 1,532
  • 3
  • 16
  • 24
0

You should stop the service on the method @Override onDestroy()

solamente
  • 266
  • 2
  • 15
  • I don't want stop service. I want service continuity for ever. – h.hejabi Feb 01 '18 at 10:05
  • You app is doing a Toast in your activity so when you close the activity you can't keep calling the Toast or it will crash your activity – solamente Feb 01 '18 at 10:36
  • App can't keep calling the Toast just in Huawei device. I test it in Samsung device and every thing is OK. – h.hejabi Feb 01 '18 at 11:01
  • That's beacause in Huawei and Xiaomi you can't leave app openned in background listening events, if you want to emulate the same as Samsung you should block the app and don't let it get closed [Here you have the extended explanation]https://docs.telerik.com/platform/knowledge-base/troubleshooting/troubleshooting-cannot-receive-push-notifications-on-android-when-the-app-is-closed – solamente Feb 01 '18 at 11:05
  • This service want to check that when user start app and send notification when app is not running. This service should be run in all device. but this device kill the service and i don't have any idea. – h.hejabi Feb 01 '18 at 11:17
  • If it works in Samsung and not in Huawei you got your answer above If that not the case I don't have more ideas sorry – solamente Feb 01 '18 at 11:26
  • Thanks dude for time – h.hejabi Feb 01 '18 at 11:44