我在MainActivity中编写代码以重复闹钟,每当我每次重复启动应用程序闹钟时都会重复。但我希望每24小时重复一次。
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
sessionManager = new SessionManager(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setBackgroundColor(getResources().getColor(R.color.black));
setSupportActionBar(toolbar);
repeatAlarm();
}
private void repeatAlarm() {
Date when = new Date(System.currentTimeMillis());
try {
Intent someIntent = new Intent(getApplicationContext(), MyReciever.class); // intent to be launched
// note this could be getActivity if you want to launch an activity
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, // id, optional
someIntent, // intent to launch
PendingIntent.FLAG_CANCEL_CURRENT); // PendintIntent flag
AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarms.setRepeating(AlarmManager.RTC_WAKEUP, when.getTime(), AlarmManager.INTERVAL_DAY, pendingIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:1)
使用PendingIntent.getService
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, // id, optional
someIntent, // intent to launch
PendingIntent.FLAG_CANCEL_CURRENT);
而不是PendingIntent.getBroadcast
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, // id, optional
someIntent, // intent to launch
PendingIntent.FLAG_CANCEL_CURRENT);
更多详情请参阅Here