How To Show Activity On Lock Screen In Android?

 

Are you wondering about how to show activity on lock screen on Devices? , If your answer is yes then this simple tutorial is for you.You may have seen that when any application whats app, facebook receive call they display calling activity to receive call even when your phone is lock.
How they do that? ,Well showing activity on lock screen is every easy task.

Lock Screen.


In Android activity class has one called "setShowWhenLocked" , which we can called in other show any activity on lock screen.

Let Look at the method signature.
public void setShowWhenLocked (boolean showWhenLocked)

Specifies whether an Activity should be shown on top of the lock screen whenever the lockscreen is up and the activity is resumed. Normally an activity will be transitioned to the stopped state if it is started while the lockscreen is up, but with this flag set the activity will remain in the resumed state visible on-top of the lock screen. This value can be set as a manifest attribute using R.attr.showWhenLocked.

It's public method , it's returns nothing so it has return type void, and it's take one boolean argument which specificy you want to show activity on lock screen or not.

You can call this method from onCreate method of Activity.

JAVA: 
public void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
        //Show Activity On Lock Screen
        setShowWhenLocked(true); 
 
        // load the layout
        setContentView(R.layout.trenyprogrammer_activity); 
}
 
KOTLIN:
override fun onCreate(savedInstanceState : Bundle){
 
        super.onCreate(savedInstanceState)
        //Show Activity On Lock Screen 
        setShowWhenLocked(true) 
 
        // load the layout
        setContentView(R.layout.trenyprogrammer_activity)
} 

It's Easy to show Activity on lock screen.If you like what you read then give comment about this tutorial and other tutorial which you want to learn , Your valuable feedback make our tutorial quality batter.

No comments:

Post a Comment