我有一个场景,我每 5 分钟获取一次 GPS 坐标。为此,我使用警报管理器。但从昨天开始我就很困惑,因为警报管理器只触发了一次,之后就不再重复了。
下面是代码
MainActivity.Class
enter code here
私有无效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
公共类 LocationAlarmReceiver 扩展了 BroadcastReceiver { 上下文上下文;
@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);}
}
清单.类
<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"/>
代码仅运行一次,但不会每 5 分钟再次重复一次。如果有人遇到了确切的问题并解决了它可以指导我,这将是一个很大的帮助。
我通过利用其他技术找到了解决方案,因为重复功能
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,triggerAtMillis,pendingIntent)
仍然没有在我身边奔跑。我使用了 Alarm Manager 的一项功能,该功能仅在 MainActivity.class 中触发一次
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (1000 * 2), pi);
这样警报管理器将被调用一次,并按照给定的时间间隔启动服务。一旦服务启动,它将再次调用该函数,并且警报将设置为在下一个时间间隔调用。
它将通过广播接收器启动服务,并且在服务类中我再次调用了警报管理器的功能。
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);
}
}
}
在服务类中再次触发警报管理器
服务。班级
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);
}