如何将 AlarmManager 附加到 BootReceiver
Posted
技术标签:
【中文标题】如何将 AlarmManager 附加到 BootReceiver【英文标题】:How to attach an AlarmManager to a BootReceiver 【发布时间】:2012-10-10 13:11:24 【问题描述】:我在一个活动中有以下内容,它基本上在用户登录应用程序时设置了一个 AlarmManager。然后,AlarmManager 会定期调用另一个从手机数据库中删除事务的活动。这一切都很好。
// get a Calendar object with current time
Calendar cal = Calendar.getInstance();
// add 5 minutes to the calendar object
cal.add(Calendar.MINUTE, 1);
Intent intent = new Intent(EntryActivity.this, AlarmReceiver.class);
intent.putExtra("alarm_message", "deleting transactions");
// In reality, you would want to have a static variable for the request code instead of 192837
PendingIntent sender = PendingIntent.getBroadcast(EntryActivity.this, 192837,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
//am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 15000, sender);
.
public class AlarmReceiver extends BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent)
try
Bundle bundle = intent.getExtras();
String message = bundle.getString("alarm_message");
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(context, SendOutstandingTransactions.class);
myIntent.setAction("com.carefreegroup.startatboot.MyService");
context.startService(myIntent);
catch (Exception e)
Toast.makeText(context, "There was an error somewhere, but we still"
+ " received an alarm", Toast.LENGTH_SHORT).show();
e.printStackTrace();
.
public class SendOutstandingTransactions extends IntentService
private static final String TAG = SendOutstandingTransactions.class.getSimpleName();
NfcScannerApplication nfcscannerapplication;
Cursor c;
@Override
public void onCreate()
nfcscannerapplication = (NfcScannerApplication)getApplication();
super.onCreate();
@Override
protected void onHandleIntent(Intent intent)
nfcscannerapplication.loginValidate.deleteTableTransactions();
public SendOutstandingTransactions()
super("SendOutstandingTransactions");
// end of class
仅在用户首次登录应用时才会调用定期删除事务的服务。从那时起它无限期地运行。如果用户重启手机怎么办?服务只会在用户下次登录时恢复。
我有一个 BootReceiver,但我不知道如何附加 AlarmManager。我尝试了以下方法,但 ALARM_SERVICE 无法解决。这是在正确的线路上吗?
public class MyBootReceiver extends BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent)
// Intent myIntent = new Intent(context, SendOutstandingTransactions.class);
// myIntent.setAction("com.carefreegroup.startatboot.MyService");
// context.startService(myIntent);
// get a Calendar object with current time
Calendar cal = Calendar.getInstance();
// add 5 minutes to the calendar object
cal.add(Calendar.MINUTE, 1);
Intent intent = new Intent(MyBootReceiver.this, AlarmReceiver.class);
intent.putExtra("alarm_message", "deleting transactions");
// In reality, you would want to have a static variable for the request code instead of 192837
PendingIntent sender = PendingIntent.getBroadcast(context, 192837,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
//am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 15000, sender);
【问题讨论】:
引导接收器?你的意思是广播接收器。 @PoweRoy 是的,很抱歉接收系统启动的 BroadcastReceiveradd 5 minutes to the calendar object
?你的意思是 1 分钟?
【参考方案1】:
getSystemService
可通过Context
获得。 (您在onReceive
收到它)
你应该这样做:
AlarmManager am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
【讨论】:
我已将其更改为您建议的内容,但不幸的是 ALARM_SERVICE 仍然无法恢复 你还需要Context.ALARM_SERVICE
不,但我已将其更改为 Context.ALARM_SERVICE,它似乎就是这样。现在就去试试。以上是关于如何将 AlarmManager 附加到 BootReceiver的主要内容,如果未能解决你的问题,请参考以下文章
如何在android中向AlarmManager添加3个日期
如何在spring boot gradle项目中将时间戳附加到jar?