本文介绍了Android的AlarmManager在一个BroadcastReceiver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有braodcastreceiver,该广播接收机应设定闹钟。

I have braodcastreceiver, that broadcast receiver shall schedule an alarm.

通常我会做

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

的问题是,getSystemService不在广播接收器仅在一个Activty。如何将我在这里?

The problem is that getSystemService is not available in a Broadcast receiver only in an Activty. How would I do it here?

谢谢你,A。

推荐答案

AndyAndroid,

AndyAndroid,

getSystemService()上下文的一部分。您将需要保存上下文您在的onReceive()方法,像这样得到...

getSystemService() is part of the Context. You will need to save the Context you receive in your onReceive() method like so...

private Context mContext;

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

Then..where你叫 getSystemService()你用...

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

这篇关于Android的AlarmManager在一个BroadcastReceiver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 15:06