Wake up app from sleep at set time

后端 未结 2 1240
渐次进展
渐次进展 2021-01-22 09:14

I want to send my app to sleep and then wake it up at set times. I have it going to sleep but not waking up.

This sets the wakelock:

private void setWake         


        
相关标签:
2条回答
  • 2021-01-22 09:45

    as above. The problem was that I was using a broadcast receiver within the calling activity.

    0 讨论(0)
  • 2021-01-22 10:01

    First, you don't want a wakelock; those are for keeping the device from going to sleep, which is highly anti-social unless your app really requires it (it kills the battery).

    Second, your code to set the wakeup time will fail if you call it after 18:14 since you'll now be defining a time in the past. Let's ignore that for now.

    Next, your intent action should be something like "org.user1797190.WAKE_INTENT" rather than simply "WAKE_INTENT" which could cause collisions. If you anticipate making this intent public, consider registering it at http://openintents.org. That's not your problem either, though.

    You don't need alarmManager2 -- there's only one alarm manager in the system, so just re-use the first one.

    I've never heard of making an app go to "sleep" per se. Do you mean you want the app to go away, and then come back later?

    Here is what I would do. Forget about the "SLEEP_INTENT" completely. Just schedule a "WAKE_INTENT" and then call finish(). Your app will simply leave the screen.

    I would forget about the broadcast receiver entirely. Instead, I would use getActivity() instead of getBroadcast() to get a pending intent that will restart the activity. Modify your manifest so that your WAKE_INTENT will go to the activity. Also, you should set the "android:launchMode" property to "singleTask" so multiple instances of your activity aren't created. You'll also need to implement onNewIntent() to handle the wakeup intent if your activity is already running when it arrives.

    Finally, if your activity is part of the same application that will be creating the intent, you don't need a named intent at all; you can create them by class. You'll need another way to let the receiver know that this is a wakeup intent though.

    So, putting it all together:

    Your manifest should contain:

    <activity android:name=".TestActivity" android:launchMode="singleTask">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    

    Your code should contain:

    /**
     * Arrange for the activity to return at a specific time.
     * Call finish() after calling this method().
     * This function can be called from anywhere that has a valid Context.
     */
    public static void scheduleWakeup(Context ctx, long timeMillis) {
        if (DEBUG) Log.d(TAG, "Scheduling wakeup for " + timeMillis);
        Intent intent = new Intent(ctx, TestActivity.class);
        intent.putExtra("wakeup", true);
        PendingIntent pi = PendingIntent.getActivity(ctx, 0, intent,
                              PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager mgr = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
        mgr.cancel(pi);    // Cancel any previously-scheduled wakeups
        mgr.set(AlarmManager.RTC_WAKEUP, timeMillis, pi);
    }
    
    ...
    
    protected void onCreate(Bundle state) {
        Intent intent = getIntent();
        if (intent.getBooleanExtra("wakeup", false)) {
            // We were woken up by the alarm manager
        }
        ...
    }
    
    protected void onNewIntent(Intent intent) {
        if (intent.getBooleanExtra("wakeup", false)) {
            // We were woken up by the alarm manager, but were already running
        }
    }
    

    This is pretty close to what I'm doing in my own apps, and it works pretty well for me.

    You'll have to test this yourself, of course. Log.d() is your friend.

    0 讨论(0)
提交回复
热议问题