Disable Home button by making my app a homeScreen App

Viewed 263

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

1 Answers

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;
    }

Related