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 receiver
does 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?