警报管理器如何在特定时间运行任务

时间:2019-05-04 19:09:43

标签: android broadcastreceiver alarmmanager

我编写了与Alarm Manager相关的代码,我想每天3点运行 但是只要我进入应用程序,它就会运行 我只想不停地跑步,而不是每次输入。

MainActivity类:

public class MainActivity extends AppCompatActivity
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

        Calendar updateTime = Calendar.getInstance();
        updateTime.setTimeZone(TimeZone.getTimeZone("GMT+5:00"));
        updateTime.set(Calendar.HOUR_OF_DAY, 5);
        updateTime.set(Calendar.MINUTE, 1);
        Intent intent = new Intent(context, BroadConnect.class);
        PendingIntent recurringDownload = PendingIntent.getBroadcast(context,
                0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarms = (AlarmManager) this.getSystemService(
                Context.ALARM_SERVICE);
        alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                updateTime.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, recurringDownload);
    }

和BroadcastReceiver类:

       public class BroadConnect extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context,"ok" ,Toast.LENGTH_LONG).show();

}

0 个答案:

没有答案