有一个BroadcastReceiver
,我想在onReceive
方法中创建一个PendingIntent
public class MyPushNotificationReceiver extends BroadcastReceiver {
..
public void onReceive(Context context, Intent intent) {
// How to start a PendingIntent here?
}
Google 的大部分文档都不是从 onReceive
方法开始的,所以我可以使用任何示例代码吗?
谢谢。
最佳答案
在广播接收器中使用挂起 Intent 的示例代码请检查
public class MyScheduleReceiver extends BroadcastReceiver {
// Restart service every 30 minute
private static final long REPEAT_TIME = 1000 * 30 ;
@Override
public void onReceive(Context context, Intent intent) {
AlarmManager service = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, MyStartServiceReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,
PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
// Start 30 seconds after boot completed
cal.add(Calendar.SECOND, 30);
//
// Fetch every 30 seconds
// InexactRepeating allows Android to optimize the energy consumption
service.setInexactRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), REPEAT_TIME, pending);
// service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
// REPEAT_TIME, pending);
}
}
此代码来自 vogella ...
关于android - 在 BroadcastReceiver 的 onReceive() 中创建 PendingIntent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12070704/