Android : Broadcast when screen lock appear

浪尽此生 提交于 2019-12-13 04:13:27

问题


I want to run a method when android phones screen lock appear.

I tested ACTION_SCREEN_ON as a broadcast, but it only works when the activity is live.

I also tested ACTION_USER_PRESENT and it works when the phone is unlocked, but I want to run the method before unlocking (just when the screen lock appears).

I also tested AlarmManager by repeating alarm every 1 minute, but this solution has two defects:

  1. Battery soon gets empty.
  2. It's a deprecated way, and I don't need to do method every 1 minute.

What should I do?


回答1:


You can create a service that listen to ACTION_SCREEN_ON broadcast.

Below are the example code for your reference:

public class LockScreenService extends Service {

    private BroadcastReceiver mScreenStateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()) {
                case Intent.ACTION_SCREEN_ON:
                    // do your stuff
                    break;
            }
        }
    };

    public LockScreenService () {}

    @Override
    public void onCreate() {
        super.onCreate();

        // register ACTION_SCREEN_ON receiver
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        registerReceiver(mScreenStateReceiver, filter);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // do some extra things
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // do not bind service to activity because service will end if activity is destroyed
        return null;
    }

    @Override
    public void onDestroy() {
        // release receiver
        unregisterReceiver(mScreenStateReceiver);
    }
}



回答2:


Full Answer :

use service and set it START_STICKY. It Causes after killing service the service will restart again. it is my code :

android manifest : 
<application 
....
    <service android:name=".UpdateService" />

 </application>

service class :

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;


public class UpdateService extends Service {

    BroadcastReceiver mReceiver;


    @Override
    public void onCreate() {
        super.onCreate();
        // register receiver that handles screen on and screen off logic
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);

        filter.addAction(Intent.ACTION_SCREEN_OFF);
        mReceiver = new MyReceiver();
        registerReceiver(mReceiver, filter);
    }


    @Override
    public void onDestroy() {

        unregisterReceiver(mReceiver);
        Log.i("onDestroy Reciever", "Called");

        super.onDestroy();
    }


    @Override
    public void onStart(Intent intent, int startId) {
        boolean screenOn = intent.getBooleanExtra("screen_state", false);
        if ( !screenOn) {
            Log.i("screenON", "Called");
            Toast.makeText(getApplicationContext(), "Awake", Toast.LENGTH_LONG)
                    .show();
        } else {
            Log.i("screenOFF", "Called");
            Toast.makeText(getApplicationContext(), "Sleep",
                    Toast.LENGTH_LONG)
                    .show();
        }
    }


    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return Service.START_STICKY;
    }
}

receiver class :

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;


public class MyReceiver extends BroadcastReceiver {

    private boolean screenOff;


    @Override
    public void onReceive(Context context, Intent intent) {


        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            screenOff = true;
            Log.i("screenLog", "screen off");

        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            screenOff = false;
            Log.i("screenLog", "screen on");
        }


    }
}

in StartupActivity :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


        Context context = getApplicationContext();
        Intent service = new Intent(context, UpdateService.class);
        context.startService(service);
}


来源:https://stackoverflow.com/questions/34556251/android-broadcast-when-screen-lock-appear

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!