我在android中实现了一个警报,但它大部分时间都会通知。
在我的应用程序中,我可以一次创建多个警报,但如果这样做,则只会通知最后一个警报。
请帮我找到解决方案。
Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.HOUR_OF_DAY, 11);
calendar1.set(Calendar.MINUTE, 55);
calendar1.set(Calendar.SECOND, 0);
calendar1.set(Calendar.AM_PM,Calendar.AM);
Intent myIntent1 = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent1,0);
AlarmManager alarmManager1 = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager1.setRepeating(AlarmManager.RTC, calendar1.getTimeInMillis(),3600000, pendingIntent);
答案 0 :(得分:1)
这应设置两个闹钟,并允许闹钟唤醒您的设备,如果它已经睡着了......
Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.HOUR_OF_DAY, 11);
calendar1.set(Calendar.MINUTE, 55);
calendar1.set(Calendar.SECOND, 0);
PendingIntent pendingIntent1, pendingIntent2;
Intent myIntent1 = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent1 = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent1, PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent2 = PendingIntent.getBroadcast(MainActivity.this, 1, myIntent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager1 = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), 3600000, pendingIntent1);
calendar1.set(Calendar.MINUTE, 56);
alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), 3600000, pendingIntent2);
注意getBroadcast(...)
的第二个参数。这是requestCode
,如果您想同时设置两个不同的警报,则必须是唯一的。
还要注意在触发警报时使用AlarmManager.RTC_WAKEUP
唤醒设备。
答案 1 :(得分:1)
作为mentioned,Android no longer triggers the alarm at the exact time, unless you explicitly request it:
注意:从API 19(KITKAT)开始,警报传递不准确:操作系统将移动警报以最小化唤醒和电池使用。有新的API支持需要严格交付保证的应用程序;请参阅
setWindow
(int,long,long,PendingIntent)和setExact
(int,long,PendingIntent)。 targetSdkVersion早于API 19的应用程序将继续查看之前的行为,其中所有警报都在请求时准确传递。
重复警报无法做到这一点,警报总是在系统认为最佳时触发:
从API 19开始,所有重复警报都不准确。
有关多次警报的问题seems solved。
答案 2 :(得分:0)
//更新清单
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".AlarmReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
//创建类AlarmReceiver
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// For our recurring task, we'll just display a message
Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
}
}
//在主要活动中
//我设置了1号码闹钟,你可以拨打任意数量的闹钟
private PendingIntent pendingIntent;
AlarmManager alarmManager;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);
alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
callAlarmWithNumber(alarmManager,1);
}
public void callAlarmWithNumber(AlarmManager alarm,int number) {
switch (number)
{
case 1 :
alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
/* Set the alarm to start at 10:30 AM */
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 17);
calendar.set(Calendar.MINUTE, 42);
/* Repeating on every 1 minutes interval */
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 1, pendingIntent);
break;
default :
break;
}
}