BroadcastReceiver Doesnt work if I close Recent Ap

2019-01-29 08:07发布

问题:

I am Trying to make an app which app will be called if a particular thing triggered. In my App this thing is sms.. When a new Sms Receiving I Read its contents and i Look for a Specific Content If it presents an alarm will goes on.

If the App is opened its just working fine. The receiver triggers good... but when i clear recent apps receiver not triggering. I searched a lot here.. and Most of us saying that when an app in STOPPED_STATE it wont receive any notification that the particular thing happened.

I have an android device Hauwei and android version 4.4.4 and when i clear my recent application it completely destroy my app. the receiver stopped working.

I have tried to make an service and return start_sticky and for me its only possible with foreground process not for background tasks. But i don't want to push a notification which says myApplication running in your device.

Now What i want is I need A receiver which should receive SMS_RECEIVED even if the app is closed (here I am not talking about the STOPPED_STATE OF THE APP).

After clearRecentApps myApp is in STOPPED_STATE (i can see it in settings myApps FORCE_STOP button disabled). but at the same time WATSAPP , FACEBOOK AND CLASSOFCLANS are not it STOPPED_STATE( in settings those FORCE_STOP BUTTONS ARE ENABLED ONLY). without any foreground notifications.

How do they doing... Please help me guys... this taking lot of days for me..

If i am wrong let me Know. Iam New to android.. Any Help would be appreciated..

回答1:

This may be a work around but to keep your service running even when your app is closed, you have to start your service again in your service's onDestroy() method.

@Override
public void onDestroy() {
    super.onDestroy();
    startService(new Intent(this, YourServiceClass.class));

}