Android: Wake & unlock phone

前端 未结 3 605
庸人自扰
庸人自扰 2020-12-08 17:15

I am trying to figure out how to wake and unlock the phone with a service. I have been referring to this post but, I can\'t figure out why it isn\'t working. This is the cod

3条回答
  •  天命终不由人
    2020-12-08 18:03

    There is WakefulBroadcastReceiver which does this for you. Example use:

    import android.content.Context;
    import android.content.Intent;
    import android.os.SystemClock;
    import android.support.v4.content.WakefulBroadcastReceiver;
    import android.util.Log;
    
    public class SimpleWakefulReceiver extends WakefulBroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // This is the Intent to deliver to our service.
            Intent service = new Intent(context, SimpleWakefulService.class);
    
            // Start the service, keeping the device awake while it is launching.
            Log.i("SimpleWakefulReceiver", "Starting service @ " + SystemClock.elapsedRealtime());
            startWakefulService(context, service);
        }
    }
    

    After completing the action in the service, call SimpleWakefulReceiver.completeWakefulIntent(intent) to release the wake lock.

    (as @Force already gave you the details about the wakeLock, they need not be repeated here ;-)

    Mind that the class is deprecated from api level 26.1.0, reference here

提交回复
热议问题