PendingIntent不发送广播

时间:2013-08-14 06:49:07

标签: alarmmanager android-pendingintent android-broadcast

onCreate中的sendBroadcast正在运行,但pendingIntent未在特定时间发送广播。

我检查了日志中的时间,没关系。不知道为什么PendingIntent不起作用。请帮忙。

公共类MainActivity扩展了Activity {

Intent intent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    intent = new Intent();
    intent.setAction("Broadcast");
    sendBroadcast(intent);
}

public void onClick(View v){

    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(System.currentTimeMillis());
    c.add(Calendar.MINUTE, 1);      

    AlarmManager mgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
    mgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, c.getTimeInMillis() , pi);

    Log.v("system current",""+System.currentTimeMillis());
    Log.v("calender",""+c.getTimeInMillis());
}

}

1 个答案:

答案 0 :(得分:1)

你应该尝试使用mgr.set(AlarmManager.RTC,c.getTimeInMillis(),pi);