Service, WakeLock

后端 未结 2 577
我寻月下人不归
我寻月下人不归 2020-12-11 01:09

I am bit confused after going through the questions & answers in Stackoverflow about WakefulIntentService. I just would like to get some knowledge on this t

2条回答
  •  半阙折子戏
    2020-12-11 01:52

    I used the code below in an app.

    Make sure your service is sticky:

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        //this service will run until we stop it
    
        return START_STICKY;
    }
    

    I you want your phone to be awake constantly u can use this code below:

    private WakeLock wl;
    
    PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "whatever");
        wl.acquire();
    

    Don't forget the permissions in your manifest.

    
    

提交回复
热议问题