简体   繁体   English

Broadcastreceiver中的Android AlarmManager

[英]Android AlarmManager in a Broadcastreceiver

I have braodcastreceiver, that broadcast receiver shall schedule an alarm. 我有braodcastreceiver,广播接收器应安排一个警报。

Usually I would do 通常我会这样做

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
am.set(AlarmManager.RTC, time,  myPendingIntent); 

The problem is that getSystemService is not available in a Broadcast receiver only in an Activty. 问题是getSystemService仅在Activty中的广播接收器中不可用。 How would I do it here? 我怎么在这里做?

Thanks, A. 谢谢。

AndyAndroid, AndyAndroid,

getSystemService() is part of the Context . getSystemService()Context一部分。 You will need to save the Context you receive in your onReceive() method like so... 您将需要在onReceive()方法中保存您收到的Context ,如此...

private Context mContext;

@Override
public void onReceive(Context c, Intent i) {
    mContext = c;
}

Then..where you call getSystemService() you use... 然后..你调用getSystemService()你用...

AlarmManager am = (AlarmManager) mContext.getSystemService(mContext.ALARM_SERVICE); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM