2

I am trying to develop a lock application, I disabled the home button click but only in the phone where the button is hard clicked, but in the latest versions where there is no hard home button, the home button is not disabled. So After search I discovered that the only way to handle this problem is to make my app like the HomeScreen, I added this to my manifest:

    <activity
        android:name=".HomeActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActivity"
            android:clearTaskOnLaunch="true"
            android:excludeFromRecents="true"
            android:launchMode="singleTask"
            android:screenOrientation="portrait"
            android:stateNotNeeded="true" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.HOME" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

When running this app, it shows me to choose between my app and android screen, when i choose my app, everytime i click the home button it opens my app!

My questions are:

1- Is that the only way to handle the home button in all android versions? by making my app a HomeScreen app?

2- Is there any other way to disable the home button only when my app is opened?

3- How Can I switch to TouchWiz default home screen programatically and remove my app as homeScreen when the lock is opened successfully ?

Thank you

Mr.Geek
  • 61
  • 7

1 Answers1

0

Try removing the other activity and proceed this way:

    <activity android:name=".MainActivity"
            android:clearTaskOnLaunch="true"
            android:excludeFromRecents="true"
            android:launchMode="singleTask"
            android:screenOrientation="portrait"
            android:stateNotNeeded="true" >
            <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

Every time someone hits the home button, it will be opening your activity which is a home screen and this time it will not have to open the home activity first.

EDIT:

If then you want to comeback to your default launcher after home button pressed, you can override these methods:

   
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_HOME)
        {
            Intent intent=null;
final PackageManager packageManager=getPackageManager();
for(final ResolveInfo resolveInfo:packageManager.queryIntentActivities(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME), PackageManager.MATCH_DEFAULT_ONLY))
{
    if(!getPackageName().equals(resolveInfo.activityInfo.packageName))  //if this activity is not in our activity (in other words, it's another default home screen)
    {
        intent=packageManager.getLaunchIntentForPackage(resolveInfo.activityInfo.packageName));
startActivity(intent);
        break;
    }
}
        }
        if(keyCode==KeyEvent.KEYCODE_BACK)
        {
            finish();
        }
        return false;
    }
Gratien Asimbahwe
  • 1,606
  • 4
  • 19
  • 30
  • Yes, but my application is only a phone lock app, that means when the user enters the code successfuly, i want the default home screen to be the normal one : TouchWiz and not my app! is there any way to do it programatically? – Mr.Geek Nov 09 '18 at 12:36
  • It's exactly what i needed! what is the code to launch default launcher or to open the chooser of home screens? – Mr.Geek Nov 09 '18 at 13:02
  • I have updated my answer, please check. if it works come back and mention it works to help future readers. – Gratien Asimbahwe Nov 09 '18 at 13:15
  • App crashes! it shows me this exception: java.lang.IllegalArgumentException: Window type can not be changed after the window is added. – Mr.Geek Nov 09 '18 at 13:34
  • And this line didn't compile the TYPE_KEYGUARD this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD); I changed it to : TYPE_KEYGUARD_DIALOG (the only available) – Mr.Geek Nov 09 '18 at 13:34
  • doesn't disable home button! the home button stay as default TouchWiz and doen't give me any more the selector of home screen as it did before! is there any way to launch the selector as it does at the first time? – Mr.Geek Nov 09 '18 at 13:55
  • even when i removed your code, the selector doesn't appear any more! now i have to do it manually. – Mr.Geek Nov 09 '18 at 13:58