I spent half a day studying the custom lock screen and found that the implementation is not very mysterious. However, some points are worth noting.
First of all, the screen lock interface is generally enabled when the screen is turned off and displayed in front of us when the screen is turned on, so we know the time to lock the screen, and the screen switch will broadcast accordingly, so we can also capture the screen switch event like the system.
Two actions corresponding to broadcast
Reference
Android. intent. action. SCREEN_ON
Android. intent. action. SCREEN_OFF
Interestingly, I saw two broadcasts on my me860.
Reference
Android. intent. action. batteryprofile. SCR_OFF
Android. intent. action. batteryprofile. SCR_ON
This may be a broadcast defined by moto.
How can I replace the system lock screen when only broadcasting is intercepted? In fact, we have not replaced the system lock screen here, but we have enabled the system lock screen to achieve a replacement effect.
Note that the preceding action cannot be registered in AndroidManifest. xml and cannot be triggered. So here I use dynamic registration in a service to intercept normal broadcast.
Here, we also mention the service anti-kill function, because if the service process does not exist, the screen switch broadcast will not be received in any way.
If the service is recycled by the system, the system will start up again after a short period of time and with sufficient resources. However, we do not want our service to be on the bench, so we should take the following methods to deal with system shutdown service:
In the service Lifecycle
Java code
Intent startIntent = null;
@ Override
Public void onStart (Intent intent, int startId ){
StartIntent = intent;
IntentFilter filter = new IntentFilter ();
Filter. addAction (Intent. ACTION_SCREEN_ON );
Filter. addAction (Intent. ACTION_SCREEN_OFF );
Explorer = new RelativeBroadcastReceiver ();
RegisterReceiver (receiver, filter );
System. out. println ("service onStart and action is" + intent. getAction ());
System. out. println ("service onStart and startId is" + startId );
};
@ Override
Public void onDestroy (){
// TODO Auto-generated method stub
System. out. println ("service onDestroy ");
UnregisterReceiver (receiver );
If (startIntent! = Null ){
System. out. println ("serviceIntent not null ");
StartService (startIntent );
}
Intent startIntent = null;
@ Override
Public void onStart (Intent intent, int startId ){
StartIntent = intent;
IntentFilter filter = new IntentFilter ();
Filter. addAction (Intent. ACTION_SCREEN_ON );
Filter. addAction (Intent. ACTION_SCREEN_OFF );
Explorer = new RelativeBroadcastReceiver ();
RegisterReceiver (receiver, filter );
System. out. println ("service onStart and action is" + intent. getAction ());
System. out. println ("service onStart and startId is" + startId );
};
@ Override
Public void onDestroy (){
// TODO Auto-generated method stub
System. out. println ("service onDestroy ");
UnregisterReceiver (receiver );
If (startIntent! = Null ){
System. out. println ("serviceIntent not null ");
StartService (startIntent );
}
We keep the intent that enables the service. When we start ourselves again in the ondestroy cycle, the system will have to promise you to stay in the memory.
However, if some memory management software kills the program process, the above method will be useless. We can still start our service by intercepting some key broadcasts, such as the 91 assistant. You can listen to Wi-Fi connections, change battery power, and other broadcasts to start your own service.
Ensure that our service is resident. At this time, our own broadcast receiver should play a role.
Java code
@ Override
Public void onReceive (Context context, Intent intent ){
// TODO Auto-generated method stub
String action = intent. getAction ();
System. out. println ("action is" + action );
Intent lockIntent = new Intent (context, MyLockScreen. class );
LockIntent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
Context. startActivity (lockIntent );
}
@ Override
Public void onReceive (Context context, Intent intent ){
// TODO Auto-generated method stub
String action = intent. getAction ();
System. out. println ("action is" + action );
Intent lockIntent = new Intent (context, MyLockScreen. class );
LockIntent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
Context. startActivity (lockIntent );
}
LockIntent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK) must be added. If no value is added, the system reports an error. However, problems may occur in the future. This will cause multiple exits before exiting the custom screen lock interface.
In fact, you can customize a stack to manage these activities. If you have other methods, Please prompt me to correct them.
After starting an activity, we found that the screen lock is still the original screen lock interface, which is also mentioned above. We need to enable the System screen lock for our screen lock.
Turn on the system lock screen:
Java code
Super. onCreate (savedInstanceState );
GetWindow (). addFlags (WindowManager. LayoutParams. FLAG_SHOW_WHEN_LOCKED );
GetWindow (). addFlags (WindowManager. LayoutParams. FLAG_DISMISS_KEYGUARD );
SetContentView (R. layout. main );
Super. onCreate (savedInstanceState );
GetWindow (). addFlags (WindowManager. LayoutParams. FLAG_SHOW_WHEN_LOCKED );
GetWindow (). addFlags (WindowManager. LayoutParams. FLAG_DISMISS_KEYGUARD );
SetContentView (R. layout. main );
After the two flags are added, the system lock screen is replaced with our own screen lock interface.
The screen lock replacement function is complete. In fact, you can also modify the system lock screen, but the modification to the framework layer is also very troublesome for promotion, so it is not mentioned.
Author wiseideal