在android中重复任务

时间:2015-08-15 20:17:22

标签: android service alarmmanager

我想在每天的特定时间重复任务,所以我在Main Activity上有这样的事情:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent doServiceIntent = new Intent(this, Service.class);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, doServiceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 22);
    calendar.set(Calendar.MINUTE, 00);
    calendar.set(Calendar.SECOND, 00);
    if (Build.VERSION.SDK_INT < 19)
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
    else
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

我的服务类是这样的:

@Override
public void onStart(Intent intent, int startId) {
        Intent intent = new Intent(this, Activity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
}

但不准确

有什么问题?我怎么解决呢?

0 个答案:

没有答案