Reputation: 51
I have a scenario, where I do get Gps coordinates on every 5 minutes. For this I am using Alarm manager. But I got sucked since yesterday as Alarm Manager triggered only once and it's not repeating afterward.
Here is the code below
MainActivity.Class
enter code here
private void setAlarm() {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, LocationAlarmReceiver.class);
intent.setAction("com.mhz.evs.ACTION_FETCH_LOCATION"+System.currentTimeMillis());
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_IMMUTABLE|FLAG_UPDATE_CURRENT );
long interval = AlarmManager.INTERVAL_FIFTEEN_MINUTES / 3; // 5 minutes in milliseconds
long triggerAtMillis = System.currentTimeMillis() + interval;
if (alarmManager != null) {
alarmManager.setExactAndAllowWhileIdle ( AlarmManager.RTC_WAKEUP, triggerAtMillis, pendingIntent);
}
}
LocationReceiver.Class
public class LocationAlarmReceiver extends BroadcastReceiver { Context context;
@Override
public void onReceive(Context context, Intent intent) {
this.context=context;
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Intent alarmIntent = new Intent(context, LocationAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_IMMUTABLE);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MINUTE, 1);
if (alarmManager != null) {
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5 * 60 * 1000, pendingIntent);
}
} else {
Toast.makeText(context,"Triggered",Toast.LENGTH_SHORT).show();
Intent serviceIntent = new Intent(context, LocationService.class);
context.startService(serviceIntent);}
}
Manifest.Class
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
<uses-permission android:name="android.permission.USE_EXACT_ALARM"/>
<receiver
android:name=".Receivers.LocationAlarmReceiver" android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="com.mhz.evs.ACTION_FETCH_LOCATION"/>
</intent-filter>
</receiver>
<service android:name=".Services.LocationService" android:exported="true" android:enabled="true"/>
Code is working fine for one time only but its not repeating again on every 5 minutes. If someone faced the exact issue and resolved it could guide me would be a great help.
Upvotes: 0
Views: 76
Reputation: 51
I found a solution by utilizing other technique, since repeating function
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtMillis, pendingIntent)
is still not running at myside. I used a function of Alarm Manager that would trigger it one time only in MainActivity.class
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (1000 * 2), pi);
In this way alarm Manager will be called one time and start the service on the given interval. As soon as the service start, it would again fire Alarm Manager function and Alarm would be set again to call for the next interval.
MainActivity.Class
private void setAlarmOneTime()
{
AlarmManager alarmManager = (AlarmManager)
getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, LocationAlarmReceiver.class);
intent.setAction("com.mhz.evs.ACTION_FETCH_LOCATION"
+System.currentTimeMillis());
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_IMMUTABLE|FLAG_UPDATE_CURRENT );
long interval = AlarmManager.INTERVAL_FIFTEEN_MINUTES / 3; // 5 minutes in milliseconds
long triggerAtMillis = System.currentTimeMillis() + interval;
if (alarmManager != null) {
alarmManager.setExactAndAllowWhileIdle ( AlarmManager.RTC_WAKEUP, triggerAtMillis, pendingIntent);
}}
LocationReceiver.Class
public class LocationAlarmReceiver extends BroadcastReceiver {
Context context;
@Override
public void onReceive(Context context, Intent intent) {
this.context=context;
if (intent.getAction().equals("AlarmFire"))
{
Toast.makeText(context,"Triggered",Toast.LENGTH_SHORT).show();
Intent serviceIntent = new Intent(context, LocationService.class);
context.startForegroundService(serviceIntent);
}
}
}
In Service Class again fire Alarm Manager
Service. Class
public void setAgainOnetimeTimer(Context context) {
AlarmManager am
(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, LocationAlarmReceiver.class);
intent.setAction("AlarmFire");
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_IMMUTABLE);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (1000 * 60
*10), pi);
}
Upvotes: 0