Remove Activity as Default Launcher
Asked Answered
M

4

12

I set my activity as a default launcher to intercept home button clicks like so:

<activity
    android:name=".ExampleActivity"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <category android:name="android.intent.category.HOME" />        
        <category android:name="android.intent.category.DEFAULT" />               
    </intent-filter>
</activity>

When my activity, ExampleActivity is launched, if i click the home key, I get prompted to choose. If I select make this my default and chose my activity, I am stuck In my activity as desired.

The problem is, when I leave the activity, I try to remove my activity from the default launcher, but am unsuccessful.

I have tried:

ComponentName componentName = new ComponentName( 
                    "com.example.exampleactivity", 
                    "com.example.exampleactivity.class");

pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP);

And:

PackageManager pm = getActivity().getPackageManager();
             ComponentName name = new ComponentName(this, "com.example.exampleactivity.class");
             pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);

But my designation for the home is never removed.

Does anyone have a working way to fix the above?

I only wan't the home button to be default for a specific activity, not my entire application. When I leave the activity, it should be removed and restored to default.

Marileemarilin answered 26/9, 2012 at 3:36 Comment(2)
Update: Still looking for an answer for this.Marileemarilin
Hi Mike Mackintosh. Did you get solution for this. I stuck with the same problem.Bur
S
17

if it's your app that you're clearing its defaults , you can simply call :

getPackageManager().clearPackagePreferredActivities(getPackageName());

then , in order to show the dialog of choosing which launcher to use , use:

final Intent intent=new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
Synchronize answered 22/12, 2012 at 23:16 Comment(6)
Took the end of the world for this question to get answered ;) Thanks, giving it a try!Marileemarilin
It turns out that the asker did not bother to return to this question to confirm the results.Brockington
I just implemented this and can confirm that it works perfectly!Heilner
how will i t work can someone pls elaborate @android developerRafferty
@androiddeveloper i want to ask how it works ? in my case i m making launcher app but clicking on home button shows me a default dialog "which launcher to use" clicking on my launcher option in dialog should bring the home screen of device while right now in my case its bringing my app activity instead of homescreen of device how to resolve ?Rafferty
@Rafferty I don't understand. You don't understand how the code I've written works, or do you have a new issue? The code I've written is pretty straitforward : it clears the default app that handles the home-key-clicking-event , and when you wish to show the user which app should handle this event, you use the intent of the second part.Synchronize
V
2

This solution is a clever way of doing it: Clearing and setting the default home application

The code in onResume() basically goes like this:

    ComponentName componentName = new ComponentName(MyActivity.this, FakeHome.class);
    if (!isMyLauncherDefault()) {
        Log.e(TAG, "MyActivity is not default home activity!");

        // toggle fake activity
        PackageManager pm = getPackageManager();
        int flag = ((pm.getComponentEnabledSetting(componentName) == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) ? PackageManager.COMPONENT_ENABLED_STATE_DISABLED
                : PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
        pm.setComponentEnabledSetting(componentName, flag, PackageManager.DONT_KILL_APP);

        // start home activity to enable chooser
        Intent selector = new Intent(Intent.ACTION_MAIN);
        selector.addCategory(Intent.CATEGORY_HOME);
        startActivity(selector);
    }

and the method isMyLauncherDefault() is taken from here: How to check if my application is the default launcher

Varus answered 16/2, 2013 at 12:50 Comment(0)
S
1

You cannot override the behavior of the home key to suit your application; this is a design decision by Google, to ensure the user can always return to a static location. There may be some ways around this (if they still exist) but they are unintended bugs which an application should not rely on.

The short answer: you can have any key except the home key.

Sheelagh answered 26/9, 2012 at 3:40 Comment(3)
The problem is not setting usage of the home key, but returning normal usage afterwards. The user is prompted to use the activity as the default launcher app. This is by design and desired. I do not want to change this. I want to be able to remove the my activity from the launcher when the activity is finished instead and return the launcher to default. This is where the issue lies.Marileemarilin
That's not really where the issue lies... the issue is your design is not one permitted by Android. Normal applications do not have the ability to do what you're trying to do because while you might mean well, it makes it far too easy for a malicious application to cause problems for the user. You might be able to work around it by disabling your home activity. See groups.google.com/forum/?fromgroups=#!topic/android-developers/… for a brief discussion where it appears someone wanted exactly what you want, and enable/disable helped him.Sheelagh
It is perfectly allowed if one requests the correct permissions. You shouldn't be doing it for a video player but there are legitimate reasons to register as the user's home app. EG a toddler lock app.Dumpcart
D
1

Have a look at the android.permission.SET_PREFERRED_APPLICATIONS permission. Also this method http://developer.android.com/reference/android/content/pm/PackageManager.html#clearPackagePreferredActivities(java.lang.String)

Dumpcart answered 26/9, 2012 at 3:56 Comment(4)
Have previously tried this, but in 4.x, this throws a user permissions error with no way around it..Marileemarilin
You can only unset yourself. I don't see any bugs for it. Are you sure you are doing it correctly?Dumpcart
Probably not. Documentation and code samples are very sparse. Do you have any examples?Marileemarilin
Did you ever get anywhere with this?Dumpcart

© 2022 - 2024 — McMap. All rights reserved.