I am trying to run an Android Service which runs in background every 20 sec and send user's lat-long data to server for tracking. It works for the first time when I launch my application. Now If I click the Home Button, It still runs in the background. But, now if I kill my application from the app list using the home button. And restart my App with the launcher icon. Now the Service doesn't start. I am using Alarm Manager to trigger my service after every 20 sec. But on Restart my Alarm is set but doesn't registers on Broadcast Receiver, as a Result My Service is not called. Below is my code:- MyFragment.java's onCreateView() where I am setting My Alarm:-
Intent alarm = new Intent(mContext, AlarmReceiver.class);
boolean alarmRunning = (PendingIntent.getBroadcast(mContext, 0, alarm, PendingIntent.FLAG_NO_CREATE) != null);
if (alarmRunning == false) {
Log.e("In OnCreateView DDFrag", "AlarmRunning == False");
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, alarm, 0);
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 20000, pendingIntent);
} else{
Log.e("In OnCreateView DDFrag", "AlarmRunning == True");
}
AlarmReceiver.class:-
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent background = new Intent(context, MyService.class);
Log.e("AlarmReceiver", "Broadcasr Receiver started");
context.startService(background);
}
}
MyService.class :-
public class MyService extends Service {
public boolean isServiceRunning;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
this.isServiceRunning = false;
}
@Override
public void onDestroy() {
this.isServiceRunning = false;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(!this.isServiceRunning) {
sendDataToServer();
this.isServiceRunning = true;
}
return START_STICKY;
}
private void sendDataToServer() {
// Performing my operation in this method..
// On Success of the method performed I am calling the below method and setting the below variables:
stopSelf();
this.isServiceRunning = false;
}
}
Also I am defining my service and receiver in the manifest.xml file as:-
<service android:name="com.mypackagename.services.MyService" />
<receiver android:name="com.mypackagename.services.AlarmReceiver" />
Please help me to resolve the issue, or point me what i am doing wroung. As For the first time. as my Alarm manager is not set, it sets and the service is called after 20 sec appropiatley. But if I kill my application and start it again, then My Alarm is set so it doesn't start or set again. and now my AlarmReceiver class never receives the Alarm BroadcastReceiver.