Question

I'm creating a sample lock screen application in this i must override the home button, after i researched in both google and stackoverflow i got the result, it's complicated to do it. Here i mention what i did in my app,

  1. Created a service with broadcast-receiver to show my lock screen when the screen goes to off. - working fine.

  2. To override the home, menu, back and search buttons i used the following code, hope we can override the home button when the application only becomes a launcher so in my manifest.xml i added this code.

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
    
            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" />
            <!-- <category android:name="android.intent.category.LAUNCHER" /> -->
    
        </intent-filter>
    

Also in my Activity i used this code too

@Override
    public void onAttachedToWindow() {

            // TODO Auto-generated method stub
            this.getWindow().setType(
                    WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG
                            | WindowManager.LayoutParams.FLAG_FULLSCREEN);
            this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);

        super.onAttachedToWindow();
    }

So far in my sample application i successfully completed the above to. Now my problem is,

When i unlock the screen then go to any apps, then i click the Device home button, My Lock screen will appear. i tired to disable this but i don't know how can i exactly do this, for this i used some code like below,

/* This should come from a preference that let's the user select an activity that can handle the HOME intent */
String packageName = "com.android.launcher";
String packageClass = "com.android.launcher2.Launcher";

Intent home_intent = new Intent(Intent.ACTION_MAIN);
home_intent.addCategory(Intent.CATEGORY_HOME);
home_intent.setComponent(new ComponentName(packageName, packageClass));
home_intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
/* Here you should catch the exception when the launcher has been uninstalled, and let the user save themselves by opening the Market or an app list or something. Users sometimes use root apps to uninstall the system launcher, so your fake launcher is all that is left. Might as well give the poor user a hand. */
startActivity(home_intent);

No code will help me, my exact need is once i unlock the screen i need to show the default home screen until the screen goes to screen off. is any idea to handle this issue? Thanks in Advance.

Was it helpful?

Solution

Try this solution,

Create a static variable flag which is set to true when you receive the broadcast for when screen goes to off

now in your activity check if the flag is true

@Override
public void onAttachedToWindow() {
    if(MyService.Flag == true){
    //Continue with your code ...
    //....
    }else{
        finish();
    }
}

or do it on onCreate which ever is suitable for you

Once your screen is unlocked then

    //Disable the flag
    MyService.Flag = false;

Now when your user clicks the Home button the activity is called and check again the flag again and if its false then call the finish() to close the activity

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top