在Android usig AlarmManager / BroadcastReceiver中每周一重复警报

前端之家收集整理的这篇文章主要介绍了在Android usig AlarmManager / BroadcastReceiver中每周一重复警报前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在每个星期一上午9点​​重复我的任务. 05:00 PM我使用了以下代码,但我无法重复任务.

活动代码: –

public class AndroidScheduledActivity extends Activity {
    /** Called when the activity is first created. */
    int id = 115;
    Intent myIntent;
    PendingIntent pendingIntent;
    AlarmManager alarmManager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button buttonStart = (Button)findViewById(R.id.start);

        myIntent = new Intent(getBaseContext(),MyScheduledReceiver.class);
        myIntent.putExtra("id",id);
        pendingIntent = PendingIntent.getBroadcast(getBaseContext(),id,myIntent,0);

        alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

        buttonStart.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                setForMonday();
                finish();
            }});
    }

    public void setForMonday() {
        Calendar calendar = Calendar.getInstance();


        calendar.set(Calendar.DAY_OF_WEEK,2);
        calendar.set(Calendar.HOUR,09);
        calendar.set(Calendar.MINUTE,00);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        System.out.println("Old is set@ :== " + calendar.getTime());


        long interval = calendar.getTimeInMillis() + 604800000L;
        System.out.println("Next Millis = " + interval);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),interval,pendingIntent);
    }
}

注意: – 我根据86400000 * 7 = 604800000计算了间隔;

接收人: –

public class MyScheduledReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context,Intent intent) {
        // TODO Auto-generated method stub

        System.out.println("Receiver");
    }

}

如果有人有任何想法请帮助.谢谢

解决方法

删除区间变量并使用以下代码替换它.
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,AlarmManager.INTERVAL_DAY * 7,pendingIntent);
原文链接:https://www.f2er.com/android/315711.html

猜你在找的Android相关文章