0

I am trying to implement a simple service which send users' current location to server every hour. My app has just login Activity and setting activity and usually working in background. I use AlarmManager to achieve my purpose and it works well but my receiverdoes not work when I forced to stop my app. Here is my code to do that.

MyApplication.java

    @Override
    public void onCreate() {
      super.onCreate();
      startService();
    }

    private void startService(){
        Intent receiver = new Intent(this, LocationAlaramReceiver.class);
        PendingIntent intent = PendingIntent.getBroadcast(this, 0, receiver,  PendingIntent.FLAG_UPDATE_CURRENT);

       AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
       mgr.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), AlarmManager.INTERVAL_HOUR, intent);
    }

AndroidManifest.xml

    <receiver
         android:name=".receiver.LocationAlarmReceiver">
    </receiver>

LocationAlarmReceiver.java

    public class LocationAlarmReceiver extends BroadcastReceiver 
     {
    private final static String TAG = "LocationAlarmReceiver";

        @Override
        public void onReceive(Context context, Intent intent) 
             {
          Log.d(TAG, "=========== onReceive ==============");
              //Do something later

             }
    }

Just seeing my case, it does not look it is guaranteed AlarmManager will be invoked always. I want to keep LocationAlramReceiver working for each hour. How can I fix my problem?

jonsca
  • 10,218
  • 26
  • 54
  • 62
sunghun
  • 1,424
  • 4
  • 25
  • 49
  • it says on the force stop dialog that your app cannot be trusted or expected to behave correctly after it is forced stopped. Frankly expecting it do so sounds like you're trying to create spam. – Kristopher Micinski Oct 08 '12 at 01:58
  • Noop. My app will be installed on the disable's phones. It will send their location to a help centre and it can look after them easily. So my app should run always in the background even when somebody kills it. – sunghun Oct 08 '12 at 03:04
  • there is no way to do this unless the app ships with your firmware. If you don't want to have the app die, then tell the user not to kill it. – Kristopher Micinski Oct 08 '12 at 03:06
  • Then, how does task killer app work? It runs again when I kill it. It is not on the firmware but just app. I think there is some trick to do that. – sunghun Oct 08 '12 at 03:09
  • you are incorrect... unless you're talking about an older version of the system, in which case it's typically done by reading logs. Please see this answer: http://stackoverflow.com/questions/10671460/restart-the-service-even-if-app-is-force-stopped-and-keep-running-service-in-bac – Kristopher Micinski Oct 08 '12 at 03:11

1 Answers1

0

When user force stops your application, Android (since version 3.1) puts it in STOPPED STATE (more here http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html). Your application does not receive any broadcasts in STOPPED STATE. In order to get out of this state, your application must be explicitly started by the user or another application.

Nikolai Samteladze
  • 7,699
  • 6
  • 44
  • 70