A way to get unlock event in android?
There is a Broadcast Receiver Action ACTION_USER_PRESENT here is the implementation of ACTION_USER_PRESENT and ACTION_SHUTDOWN
add this to your application Manifests
<receiver android:name=".UserPresentBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
</intent-filter>
</receiver>
to receive the actions
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class UserPresentBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent intent) {
/*Sent when the user is present after
* device wakes up (e.g when the keyguard is gone)
* */
if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)){
}
/*Device is shutting down. This is broadcast when the device
* is being shut down (completely turned off, not sleeping)
* */
else if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {
}
}
}
UPDATE:
As part of the Android 8.0 (API level 26) Background Execution Limits, apps that target the API level 26 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest.
see
Detect screen unlock
As far as I know, there is no need to add anything to manifest if I choose to use registerReceiver, right?
Wrong. The advantage of an registered receiver in the manifest is the fact that it does not require your app to be running when the Intent is fired.
So your app probably is not active when the user unlocks the screen so there is no registerReceiver()
called and therefore your receiver does not react.
Add the receiver in your manifest and it will work.
How to detect and control the phone lock/unlock on android?
You may want to have a look on Device Policy Manager and Device Admin tutorial. You can lock the screen by a simple sample of code like:
DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
dpm.lockNow();
For the detection, you can follow this tutorial http://chandan-tech.blogspot.fr/2010/10/handling-screen-lock-unlock-in-android.html, it's very clear.
Related Topics
What's Oncreate(Bundle Savedinstancestate)
Add "View More" at the End of Textview After 3 Lines
Android App Crashes When Launched in Debug Mode
Determine Addaction Click for Android Notifications
Sniffing/Logging Your Own Android Bluetooth Traffic
How to Set a Particular Font for a Button Text in Android
Android Edittext - Finished Typing Event
How to Use Custom Permissions in Android
How to Use Getsystemservice in a Non-Activity Class (Locationmanager)
Drawerlayout Double Drawer (Left and Right Drawers Simultaneously)
How to Create a Resizable Rectangle with User Touch Events on Android
How to Re-Sign an .Apk with a Different Certificate Than What It Came With
What Is the Class R in Android
How to Determine If One of My Activities Is in the Foreground
Toolbar Navigation Icon Never Set
Passing Argument to Dialogfragment
How to Disable Displaying "Suggestions" on the Soft Keyboard