2012-08-22 43 views
1

有一個BroadcastReceiver,我想創建一個PendingIntentonReceive方法創建的PendingIntent廣播接收器的的onReceive()

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

     // How to start a PendingIntent here? 

    } 

大部分來自谷歌的文檔內內是不onReceive方法內開始,所以任何樣品的CODË我可以用?

謝謝。

回答

1

其使用意圖未決在廣播reciver一個示例代碼請檢查

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

相關問題