在 satat 命令服务上,我想在一天中的特定时间发送多个未决意图广播,所以我使用多个警报管理器是正确还是错误的方式,请建议我。
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Traffic Control BK Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
Calendar calNow = Calendar.getInstance();
calendar1 = Calendar.getInstance();
Date date1 = calendar1.getTime();
calendar2 = Calendar.getInstance();
Date date2 = calendar2.getTime();
calendar3 = Calendar.getInstance();
Date date3 = calendar3.getTime();
calendar4 = Calendar.getInstance();
Date date4 = calendar4.getTime();
calendar5 = Calendar.getInstance();
Date date5 = calendar5.getTime();
calendar6 = Calendar.getInstance();
Date date6 = calendar6.getTime();
calendar7 = Calendar.getInstance();
Date date7 = calendar7.getTime();
calendar8 = Calendar.getInstance();
Date date8 = calendar8.getTime();
calendar9 = Calendar.getInstance();
Date date9 = calendar9.getTime();
//int x=0;
if(tg111.charAt(0)=='1'){
//calendar1 = Calendar.getInstance();
calendar1.set(Calendar.DAY_OF_MONTH, date1.getDate());
calendar1.set(Calendar.HOUR_OF_DAY, 3);
calendar1.set(Calendar.MINUTE, 30);
calendar1.set(Calendar.SECOND, 0);
i1 = new Intent(getApplicationContext(), Trafficc.class);
pi1 = PendingIntent.getBroadcast(getApplicationContext(), 0, i1, 0);
am1 = (AlarmManager) getSystemService(ALARM_SERVICE);
am1.setRepeating(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi1);
}
if(tg111.charAt(1)=='1'){
//calendar2 = Calendar.getInstance();
calendar2.set(Calendar.DAY_OF_MONTH, date2.getDate());
calendar2.set(Calendar.HOUR_OF_DAY, 4);
calendar2.set(Calendar.MINUTE, 0);
calendar2.set(Calendar.SECOND, 0);
i2 = new Intent(getApplicationContext(), Trafficc.class);
pi2 = PendingIntent.getBroadcast(getApplicationContext(), 1, i1, 0);
am2 = (AlarmManager) getSystemService(ALARM_SERVICE);
am2.setRepeating(AlarmManager.RTC_WAKEUP, calendar2.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi2);
}
if(tg111.charAt(2)=='1'){
//calendar3 = Calendar.getInstance();
calendar3.set(Calendar.DAY_OF_MONTH,date3.getDate());
calendar3.set(Calendar.HOUR_OF_DAY, 5);
calendar3.set(Calendar.MINUTE, 45);
calendar3.set(Calendar.SECOND, 0);
i3 = new Intent(getApplicationContext(), Trafficc.class);
pi3 = PendingIntent.getBroadcast(getApplicationContext(), 0, i3, 0);
am3 = (AlarmManager) getSystemService(ALARM_SERVICE);
am3.setRepeating(AlarmManager.RTC_WAKEUP, calendar3.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi3);
}
if(tg111.charAt(3)=='1'){
//calendar4 = Calendar.getInstance();
calendar4.set(Calendar.DAY_OF_MONTH,date4.getDate());
calendar4.set(Calendar.HOUR_OF_DAY, 7);
calendar4.set(Calendar.MINUTE, 0);
calendar4.set(Calendar.SECOND, 0);
i4 = new Intent(getApplicationContext(), Trafficc.class);
pi4 = PendingIntent.getBroadcast(getApplicationContext(), 0, i4, 0);
am4 = (AlarmManager) getSystemService(ALARM_SERVICE);
am4.setRepeating(AlarmManager.RTC_WAKEUP, calendar4.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi4);
}
if(tg111.charAt(4)=='1'){
//calendar5 = Calendar.getInstance();
calendar5.set(Calendar.DAY_OF_MONTH,date5.getDate());
calendar5.set(Calendar.HOUR_OF_DAY, 10);
calendar5.set(Calendar.MINUTE, 30);
calendar5.set(Calendar.SECOND, 0);
i5 = new Intent(getApplicationContext(), Trafficc.class);
pi5 = PendingIntent.getBroadcast(getApplicationContext(), 0, i5, 0);
am5 = (AlarmManager) getSystemService(ALARM_SERVICE);
am5.setRepeating(AlarmManager.RTC_WAKEUP, calendar5.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi5);
}
if(tg111.charAt(5)=='1'){
// calendar6 = Calendar.getInstance();
calendar6.set(Calendar.DAY_OF_MONTH,date6.getDate());
calendar6.set(Calendar.HOUR_OF_DAY, 12);
calendar6.set(Calendar.MINUTE, 00);
calendar6.set(Calendar.SECOND, 0);
i6 = new Intent(getApplicationContext(), Trafficc.class);
pi6 = PendingIntent.getBroadcast(getApplicationContext(), 0, i6, 0);
am6 = (AlarmManager) getSystemService(ALARM_SERVICE);
am6.setRepeating(AlarmManager.RTC_WAKEUP, calendar6.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi6);
}
if(tg111.charAt(6)=='1'){
//calendar7 = Calendar.getInstance();
calendar7.set(Calendar.DAY_OF_MONTH,date7.getDate());
calendar7.set(Calendar.HOUR_OF_DAY, 17);
calendar7.set(Calendar.MINUTE, 30);
calendar7.set(Calendar.SECOND, 0);
i7 = new Intent(getApplicationContext(), Trafficc.class);
pi7 = PendingIntent.getBroadcast(getApplicationContext(), 0, i7, 0);
am7 = (AlarmManager) getSystemService(ALARM_SERVICE);
am7.setRepeating(AlarmManager.RTC_WAKEUP, calendar7.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi7);
}
if(tg111.charAt(7)=='1'){
//calendar8 = Calendar.getInstance();
calendar8.set(Calendar.DAY_OF_MONTH,date8.getDate());
calendar8.set(Calendar.HOUR_OF_DAY, 19);
calendar8.set(Calendar.MINUTE, 30);
calendar8.set(Calendar.SECOND, 0);
i8 = new Intent(getApplicationContext(), Trafficc.class);
pi8 = PendingIntent.getBroadcast(getApplicationContext(), 0, i8, 0);
am8 = (AlarmManager) getSystemService(ALARM_SERVICE);
am8.setRepeating(AlarmManager.RTC_WAKEUP, calendar8.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi8);
}
if(tg111.charAt(8)=='1'){
//calendar9 = Calendar.getInstance();
calendar9.set(Calendar.DAY_OF_MONTH,date9.getDate());
calendar9.set(Calendar.HOUR_OF_DAY, 21);
calendar9.set(Calendar.MINUTE, 30);
calendar9.set(Calendar.SECOND, 0);
i9 = new Intent(getApplicationContext(), Trafficc.class);
pi9 = PendingIntent.getBroadcast(getApplicationContext(), 0, i9, 0);
am9 = (AlarmManager) getSystemService(ALARM_SERVICE);
am9.setRepeating(AlarmManager.RTC_WAKEUP, calendar9.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi9);
}
return START_STICKY;
}
广播接收器
package com.atpl.bkapp;
import java.util.Calendar;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class Trafficc extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Calendar cal1= Calendar.getInstance();
int hour = cal1.get(Calendar.HOUR);
int min = cal1.get(Calendar.MINUTE);
if ((hour==3) && (min==30))
{
Toast.makeText(arg0, "showname-->3:30", Toast.LENGTH_SHORT).show();
}
else if((hour==4) && (min==00))
{
Toast.makeText(arg0, "showname--> 4:00", Toast.LENGTH_SHORT).show();
}
else if((hour==5) && (min==45))
{
Toast.makeText(arg0, "showname--> 5:45", Toast.LENGTH_SHORT).show();
}
else if((hour==7) && (min==00))
{
Toast.makeText(arg0, "showname--> 7:00", Toast.LENGTH_SHORT).show();
}
else if((hour==10) && (min==30))
{
Toast.makeText(arg0, "showname--> 10:30", Toast.LENGTH_SHORT).show();
}
else if((hour==12) && (min==00))
{
Toast.makeText(arg0, "showname--> 12:00", Toast.LENGTH_SHORT).show();
}
else if((hour==17) && (min==30))
{
Toast.makeText(arg0, "showname--> 17:30", Toast.LENGTH_SHORT).show();
}
else if((hour==19) && (min==30))
{
Toast.makeText(arg0, "showname--> 19:30", Toast.LENGTH_SHORT).show();
}
else if((hour==21) && (min==30))
{
Toast.makeText(arg0, "showname--> 21:30", Toast.LENGTH_SHORT).show();
}
}
}
请帮我解决一天内多次设置闹钟的问题提前谢谢。