简体   繁体   中英

Creating PendingIntent inside BroadcastReceiver's onReceive()

Have a BroadcastReceiver which I want to create a PendingIntent inside the onReceive method

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

        // How to start a PendingIntent here?

    }

Most of the doc from Google is not starting within the onReceive method, so any sample cod e I can use?

Thanks.

a sample code which uses a pending intent in broadcast reciver pls check

    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);

}
     }

this code is from vogella ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM