在BroadcastReceiver的onReceive()中创建PendingIntent

时间:2012-08-22 10:01:05

标签: android push-notification google-cloud-messaging

我想在BroadcastReceiver方法中创建PendingIntent onReceive

public class MyPushNotificationReceiver extends BroadcastReceiver {
..
    public void onReceive(Context context, Intent intent) {

        // How to start a PendingIntent here?

    }

Google的大部分文档都不是以onReceive方式开头的,所以我可以使用哪个样本代码?

感谢。

1 个答案:

答案 0 :(得分:1)

在广播接收机中使用未决意图的示例代码请检查

    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 ......