java – 如何使用通知播放多个警报?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 如何使用通知播放多个警报?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5025字,纯文字阅读大概需要8分钟。
内容图文
![java – 如何使用通知播放多个警报?](/upload/InfoBanner/zyjiaocheng/770/5cb6db3ef8624446ae58e6b7f79964ac.jpg)
我使用通知开发了一个警报项目.我使用通知ID作为唯一,并且每分钟设置一个警报,即下午2:45,下午2:46,下午2:47,但警报仅在最后一个,下午2:47触发.
为什么?
int dayofweek;
int notify;
int notifyid=0;
Cursor notifyvalue=db.getnotifyid();
if (notifyvalue.moveToPosition(0))
notifyid =notifyvalue.getInt(notifyvalue.getColumnIndex("notifyvalue"));
if(notifyid==0)
{
db.InsertNotifyId(2600);
}
Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();
calSet.set(Calendar.HOUR_OF_DAY,22);
calSet.set(Calendar.MINUTE, 10);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
int notify1 = 0;
if(repeateddayname.equalsIgnoreCase("Everyday"))
{
Cursor notifyvalue1=db.getnotifyid();
if (notifyvalue1.moveToPosition(0))
notify1=notifyvalue1.getInt(notifyvalue1.getColumnIndex("notifyvalue"));
setAlarm(calSet, true,notify1+1,SplitAlramVoice[1]);
db.InsertNotifyId(notify1+1);
}
else
{
String splitrepeateddayname[]=repeateddayname.split(",");
for(int i=0; i < splitrepeateddayname.length; i++)
{
if(splitrepeateddayname[i].equalsIgnoreCase("Sunday"))
{
dayofweek=1+(7-calSet.DAY_OF_WEEK);
Cursor notifyvalue1=db.getnotifyid();
if (notifyvalue1.moveToPosition(0))
notify1=notifyvalue1.getInt(notifyvalue1.getColumnIndex("notifyvalue"));
setAlarm(calSet, false,notify1+1,SplitAlramVoice[1]);
db.InsertNotifyId(notify1+1);
}
else if(splitrepeateddayname[i].equalsIgnoreCase("Monday"))
{
dayofweek=2+(7-calSet.DAY_OF_WEEK);
Cursor notifyvalue1=db.getnotifyid();
if (notifyvalue1.moveToPosition(0))
notify1=notifyvalue1.getInt(notifyvalue1.getColumnIndex("notifyvalue"));
setAlarm(calSet, false,notify1+1,SplitAlramVoice[1]);
db.InsertNotifyId(notify1+1);
}
else if(splitrepeateddayname[i].equalsIgnoreCase("Tuesday"))
{
dayofweek=3+(7-calSet.DAY_OF_WEEK);
Cursor notifyvalue1=db.getnotifyid();
if (notifyvalue1.moveToPosition(0))
notify1=notifyvalue1.getInt(notifyvalue1.getColumnIndex("notifyvalue"));
setAlarm(calSet, false,notify1+1,SplitAlramVoice[1]);
db.InsertNotifyId(notify1+1);
}
else if(splitrepeateddayname[i].equalsIgnoreCase("Wednesday"))
{
dayofweek=4+(7-calSet.DAY_OF_WEEK);
Cursor notifyvalue1=db.getnotifyid();
if (notifyvalue1.moveToPosition(0))
notify1=notifyvalue1.getInt(notifyvalue1.getColumnIndex("notifyvalue"));
setAlarm(calSet, false,notify1+1,SplitAlramVoice[1]);
db.InsertNotifyId(notify1+1);
}
else if(splitrepeateddayname[i].equalsIgnoreCase("Thursday"))
{
dayofweek=5+(7-calSet.DAY_OF_WEEK);
Cursor notifyvalue1=db.getnotifyid();
if (notifyvalue1.moveToPosition(0))
notify1=notifyvalue1.getInt(notifyvalue1.getColumnIndex("notifyvalue"));
setAlarm(calSet, false,notify1+1,SplitAlramVoice[1]);
db.InsertNotifyId(notify1+1);
}
else if(splitrepeateddayname[i].equalsIgnoreCase("Friday"))
{
dayofweek=6+(7-calSet.DAY_OF_WEEK);
Cursor notifyvalue1=db.getnotifyid();
if (notifyvalue1.moveToPosition(0))
notify1=notifyvalue1.getInt(notifyvalue1.getColumnIndex("notifyvalue"));
setAlarm(calSet, false,notify1+1,SplitAlramVoice[1]);
db.InsertNotifyId(notify1+1);
}
else if(splitrepeateddayname[i].equalsIgnoreCase("Saturday"))
{
dayofweek=7+(7-calSet.DAY_OF_WEEK);
Cursor notifyvalue1=db.getnotifyid();
if (notifyvalue1.moveToPosition(0))
notify1=notifyvalue1.getInt(notifyvalue1.getColumnIndex("notifyvalue"));
setAlarm(calSet, false,notify1+1,SplitAlramVoice[1]);
db.InsertNotifyId(notify1+1);
}
}
}
设置报警功能:
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
intent.putExtra("NotifID", notifyid);
intent.putExtra("Audio", AlarmAudio);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
// alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
if(repeat)
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
else
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,targetCal.getTimeInMillis(),dayofweek*24*60*60*1000,pendingIntent);
AlarmReceiver类:
int notifID = intent.getExtras().getInt("NotifID");
String foraudio= intent.getExtras().getString("Audio");
NotificationManager nm;
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "My Dua";
CharSequence message = "Cheking By Ismail";
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(), 0);
Notification notif = new Notification(R.drawable.ic_launcher,"Mohamed Ismail", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
Uri audio=Uri.parse("mnt/sdcard/sounds/"+foraudio+".mp3");
notif.sound=audio;
nm.notify(notifID++, notif);
解决方法:
创建一个唯一的通知ID,并传递如下:
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
intent.putExtra("NotifID", notifyid);
intent.putExtra("Audio", AlarmAudio);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), notifyid, intent, PendingIntent.FLAG_one_shot);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
内容总结
以上是互联网集市为您收集整理的java – 如何使用通知播放多个警报?全部内容,希望文章能够帮你解决java – 如何使用通知播放多个警报?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。