0

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

}

4

1 回答 1

1

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

于 2013-08-16T03:24:32.363 回答