我陷入了通过Alarm Manager调度任务的困境

时间:2016-07-19 12:27:20

标签: java android scheduled-tasks scheduling

我想每天安排10次闹钟启动和停止服务。但只有过去的警报触发器,我在谷歌搜索了很多,但没有得到正确的解决方案。这是我的代码,请检查并帮助我。

Intent start_intent,stop_intent;
List<Calendar> Calander_list;

static PendingIntent pendingIntent;

Calendar[] startCal,stopCal;
AlarmManager[] startAlarm,stopAlarm;
PendingIntent[] startIntent,stopIntent;


SharedPreferences sp;
Random rd;
Context context;

全局变量sp存储时间。

 public Alarm_schedul(Context cont) {

    context = cont;
    rd = new Random();
    Calander_list = new ArrayList<Calendar>();

    startAlarm=new AlarmManager[5];
    stopAlarm=new AlarmManager[5];
    startCal=new Calendar[5];
    stopCal=new Calendar[5];
    startIntent=new PendingIntent[5];
    stopIntent=new PendingIntent[5];

    start_intent=new Intent(context,alarm_reciver.class);
    start_intent.putExtra("alarmtype","start");

    stop_intent=new Intent(context,alarm_reciver.class);
    stop_intent.putExtra("alarmtype","stop");

    sp = context.getSharedPreferences("salah", Context.MODE_PRIVATE);
    setup_calander();
    init();
}

Alarm_schedul(Context cont)类构造函数。

public void setup_calander() {
    Fajar();
    Zuhar();
    Ashar();
    Magrab();
    Ishia();
}
public void Fajar()
 {

    String time = "", h = "", m = "";
    int index = 0;

    time = sp.getString("Fajar_from", "00:00");
    index = time.indexOf(":");
    h = time.substring(0, index);
    m = time.substring(index + 1, time.length());
    //set up calander
    startCal[0]=Calendar.getInstance();
    startCal[0].setTimeInMillis(System.currentTimeMillis());
    startCal[0].set(Calendar.HOUR_OF_DAY, Integer.parseInt(h));
    startCal[0].set(Calendar.MINUTE, Integer.parseInt(m));
    Calander_list.add(startCal[0]);
    //====

    time = sp.getString("Fajar_to", "00:00");
    index = time.indexOf(":");
    h = time.substring(0, index);
    m = time.substring(index + 1, time.length());
    stopCal[0]=Calendar.getInstance();
    stopCal[0].setTimeInMillis(System.currentTimeMillis());

    stopCal[0].set(Calendar.HOUR_OF_DAY, Integer.parseInt(h));
    stopCal[0].set(Calendar.MINUTE, Integer.parseInt(m));


}

public void Zuhar()
{
    String time="",h="",m="";
    int index=0;

    time=sp.getString("Zuhar_from", "00:00");
    index=time.indexOf(":");
    h =time.substring(0, index);
    m=time.substring(index + 1, time.length());
    startCal[1]=Calendar.getInstance();
    startCal[1].setTimeInMillis(System.currentTimeMillis());
    startCal[1].set(Calendar.HOUR_OF_DAY, Integer.parseInt(h));
    startCal[1].set(Calendar.MINUTE, Integer.parseInt(m));
    Calander_list.add(startCal[1]);

    //====
    time=sp.getString("Zuhar_to", "00:00");
    index=time.indexOf(":");
    h =time.substring(0, index);
    m=time.substring(index + 1, time.length());
    stopCal[1]=Calendar.getInstance();
    stopCal[1].setTimeInMillis(System.currentTimeMillis());
    stopCal[1].set(Calendar.HOUR_OF_DAY, Integer.parseInt(h));
    stopCal[1].set(Calendar.MINUTE, Integer.parseInt(m));

}

public void Ashar()
{

    String time="",h="",m="";
    int index=0;
    time=sp.getString("Asar_from", "no values");
    index=time.indexOf(":");
    h =time.substring(0, index);
    m=time.substring(index + 1, time.length());
    startCal[2]=Calendar.getInstance();
    startCal[2].setTimeInMillis(System.currentTimeMillis());
    startCal[2].set(Calendar.HOUR_OF_DAY, Integer.parseInt(h));
    startCal[2].set(Calendar.MINUTE, Integer.parseInt(m));
    Calander_list.add(startCal[2]);

    //====
    time=sp.getString("Asar_to", "no values");
    index=time.indexOf(":");
    h =time.substring(0, index);
    m=time.substring(index + 1, time.length());
    stopCal[2]=Calendar.getInstance();
    stopCal[2].setTimeInMillis(System.currentTimeMillis());
    stopCal[2].set(Calendar.HOUR_OF_DAY, Integer.parseInt(h));
    stopCal[2].set(Calendar.MINUTE, Integer.parseInt(m));

}

public void Magrab()
{
    String time="",h="",m="";
    int index=0;

    time=sp.getString("Magrab_from", "no values");
    index=time.indexOf(":");
    h =time.substring(0, index);
    m=time.substring(index + 1, time.length());

    startCal[3]=Calendar.getInstance();
    startCal[3].setTimeInMillis(System.currentTimeMillis());
    startCal[3].set(Calendar.HOUR_OF_DAY, Integer.parseInt(h));
    startCal[3].set(Calendar.MINUTE, Integer.parseInt(m));
    Calander_list.add(startCal[3]);

    //====
    time=sp.getString("Magrab_to", "no values");
    index=time.indexOf(":");
    h =time.substring(0, index);
    m=time.substring(index + 1, time.length());
    stopCal[3]=Calendar.getInstance();
    stopCal[3].setTimeInMillis(System.currentTimeMillis());
    stopCal[3].set(Calendar.HOUR_OF_DAY, Integer.parseInt(h));
    stopCal[3].set(Calendar.MINUTE, Integer.parseInt(m));
}

public void Ishia()
{
    String time="",h="",m="";
    int index=0;
    time=sp.getString("Ishia_from", "no values");
    index=time.indexOf(":");
    h =time.substring(0, index);
    m=time.substring(index + 1, time.length());
    startCal[4]=Calendar.getInstance();
    startCal[4].setTimeInMillis(System.currentTimeMillis());
    startCal[4].set(Calendar.HOUR_OF_DAY, Integer.parseInt(h));
    startCal[4].set(Calendar.MINUTE, Integer.parseInt(m));
    Calander_list.add(startCal[4]);
    //====
    time=sp.getString("Ishia_to", "no values");
    index=time.indexOf(":");
    h =time.substring(0, index);
    m=time.substring(index + 1, time.length());
    stopCal[4]=Calendar.getInstance();
    stopCal[4].setTimeInMillis(System.currentTimeMillis());
    stopCal[4].set(Calendar.HOUR_OF_DAY, Integer.parseInt(h));
    stopCal[4].set(Calendar.MINUTE, Integer.parseInt(m));
}

设置日历

public void init()
{

    for(int i=0; i<5; i++)
    {
        startAlarm[i]= (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        startIntent[i]=PendingIntent.getBroadcast(context,i,start_intent,PendingIntent.FLAG_UPDATE_CURRENT);
        startAlarm[i].setRepeating(AlarmManager.RTC_WAKEUP,startCal[i].getTimeInMillis(),AlarmManager.INTERVAL_DAY,startIntent[i]);
    }
    for(int i=0; i<5; i++)
    {
        stopAlarm[i]= (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        stopIntent[i]=PendingIntent.getBroadcast(context,i+5,stop_intent,PendingIntent.FLAG_UPDATE_CURRENT);
        stopAlarm[i].setRepeating(AlarmManager.RTC_WAKEUP,stopCal[i].getTimeInMillis(),AlarmManager.INTERVAL_DAY,stopIntent[i]);
    }

}

init()方法用于初始化Alarm Manager。

0 个答案:

没有答案