Xel*_*mae 47 android alarmmanager android-alarms
我是android的新手,我一直在研究警报.如果当天有生日,我想报警.我已经使用了报警管理器.我很困惑因为我已经读过它在重启后会清除.我没有Android手机,所以我只是使用模拟器.
这是我的代码:
public void schedAlarm() {
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, AlarmService.class);
pendingIntent = PendingIntent.getBroadcast(this, contact.id, intent, PendingIntent.FLAG_ONE_SHOT);
am.setRepeating(AlarmManager.RTC, timetoAlarm, nextalarm, pendingIntent);
}
Run Code Online (Sandbox Code Playgroud)
我将这个BroadcastRecever替换为AlarmSerivce Here:
public void onReceive(Context context, Intent intent) {
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "It Birthday!";
CharSequence message =" Greet your friend.";
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
Notification notif = new Notification(R.drawable.ic_launcher, "Birthday", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
nm.notify(1, notif);
}
Run Code Online (Sandbox Code Playgroud)
这够了吗??
Luc*_*fer 87
一个简单的答案是否定的.但是,您可以通过创建一个BroadCastReceiver
在启动完成设备时启动警报来实现此目的.
使用<action android:name="android.intent.action.BOOT_COMPLETED" />
在广播接收器类诱捕启动活动.
您需要在AndroidManifest.xml中添加以上行,如下所示,
<receiver android:name=".AutoStartUp" android:enabled="true" android:exported="false" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
小智 6
是的,即使重新启动后,您也可以使AlarmManager运行。也许这是最简单的方法:在AndroidManifest.xml中添加以下代码:
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
不要忘记将用户权限包括在AndroidManifest.xml中,如下所示:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
25449 次 |
最近记录: |