How to use CPU to perform any operation in Deep Sleep mode

前端 未结 2 897
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 02:43

I\'m new in android. I struggle with my application approximately 3 weeks. I need sent and receive packets in normal mode and sleep mode. My

2条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 03:18

    You can use the AlarmManager class to wake up the device at a particular time, then fire off an operation at whatever interval you'd like. Code from the docs found here:

    private AlarmManager alarmMgr;
    private PendingIntent alarmIntent;
    ...
    alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmReceiver.class);
    alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    
    // Set the alarm to start at 8:30 a.m.
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 8);
    calendar.set(Calendar.MINUTE, 30);
    
    // setRepeating() lets you specify a precise custom interval--in this case,
    // 20 minutes.
    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
    1000 * 60 * 20, alarmIntent);
    

    Notice the last line of this block. You can use the method setRepeating() to set whatever interval you'd like.

提交回复
热议问题