我的程序是实现BroadcastReceiver来设置Alarm。但我遇到了问题:在设定闹钟时间后,我关闭设备电源。之后,我启动设备和BroadcastReceiver的onReceive()函数总是调用。这是我的代码:
<receiver android:name="com.futaba.broadcastservice.AlarmBroadCastReceiver"
>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
如何解决此问题?
先谢谢。
答案 0 :(得分:0)
您是否阅读过BOOT_COMPLETED的文档?它将在系统完成启动时运行。
答案 1 :(得分:0)
这是重启后重新安排闹钟的方法。
代码在您的接收器上。
public class AlarmBroadCastReceiver extends BroadcastReceiver {
Context ct;
@Override
public void onReceive(Context context, Intent intent)
{
ct=context;
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
Intent contentIntent = new Intent(ct, AlarmBroadCastReceiver.class);
PendingIntent theappIntent = PendingIntent.getService(ct, 0,contentIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day, hour,minute);
AlarmManager am = (AlarmManager) ct.getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), theappIntent);
}
}
为了确保您必须声明uses-permission
如下
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name="com.futaba.broadcastservice.AlarmBroadCastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>