Skip to content Skip to sidebar Skip to footer

Android: Change Default Home Application

for some specific requirement I am required to change Android Default Home application with my customized Home application ( a setting inside my app that will toggle default home =

Solution 1:

I did an extensive research on that and starting from 2.2 there is no way to do that. The only way is using some hacking that toddler lock app does but this app put samsung phones recently in the infinite loop, so it is a risky approach.

if you look at the froyo source code here of packagemanager class, you will see this small condition in the addPreferredActivity method:

if (getUidTargetSdkVersionLockedLP(Binder.getCallingUid())
                     < Build.VERSION_CODES.FROYO) {
                 Slog.w(TAG, "Ignoring addPreferredActivity() from uid"
                        + Binder.getCallingUid());
                 return;
             }

HomeSwitcher does not work properly on 2.2 since it uses this very method and developer made a comment on app page "Froyo(2.2) is not supported due to the API change"

Solution 2:

"Result set changed" means that the set of packages matching that intent has changed from the set you specified when you created the default - - so the default is no longer valid. Your list of components (which you are currently setting to null) needs to contain all homescreen apps present on device, not just yours.

Here's example code that I have tested (using adb shell am start http://www.google.co.uk/ ) and used to set the default browser. XXX represents a customer name that I had to black out.

Note that in order to call addPreferredActivity you must have compiled against a minimum-sdk version of 8 (2.2) and you must have specified the SET_PREFERRED_APPLICATIONS permission. That permission is protection level 2, so you need to be signed with the same certificate as PackageManager.

IntentFilterfilter=newIntentFilter();
filter.addAction("android.intent.action.VIEW");
filter.addCategory("android.intent.category.DEFAULT");
filter.addDataScheme("http");
Contextcontext= getApplicationContext();
ComponentNamecomponent=newComponentName("com.opera.mini.XXX", "com.opera.mini.XXX.CustomerBrowser");
ComponentName[] components = newComponentName[] {newComponentName("com.android.browser", "com.android.browser.BrowserActivity"),
                                                  component};
pm.addPreferredActivity(filter, IntentFilter.MATCH_CATEGORY_SCHEME, components, component);

ETA - if you marked this response down, could you let me know why. The code I posted above is tested and working...

Solution 3:

startActivity(new Intent(Settings.ACTION_HOME_SETTINGS));

Solution 4:

This code works on my ICS device: I use a service that is sensible to some call, one of them is called SET_PREFERRED_LAUNCHER, the put in a bundle your new default Launcher package (PREFERRED_PACKAGE_KEY) and it's activity (PREFERRED_ACTIVITY_KEY)

MethodinstallPackageMethod=null;
MethoddeletePackageMethod=null;
MethodsetPreferredActivityMethod=null;
MethodreplacePreferredActivityMethod=null;
Objectpm=null;


    @OverridepublicvoidonCreate() {
    super.onCreate();
        if (pm == null)
        pm = getPackageManager();
    try {
        if (setPreferredActivityMethod == null)
            setPreferredActivityMethod = pm.getClass().getMethod(
                    "addPreferredActivity", IntentFilter.class, int.class,
                    ComponentName[].class, ComponentName.class);
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
}


privatefinalclassServiceHandlerextendsHandler {
    private Context context;

    publicServiceHandler(Looper looper, Context ctx) {
        super(looper);
        context = ctx;
    }

    @OverridepublicvoidhandleMessage(Message msg) {
        Intentintent= (Intent) msg.getData().getParcelable(
                UPDATER_SERVICE_ACTION);
        intrequest= intent.getIntExtra(
                REQUEST_KEY,
                REQUEST_UNKNOWN);
        Bundlebundle= intent.getExtras();

        switch (request) {
        case INSTALL_APPLICATION: {
            if (bundle != null) {
                StringappPath= bundle
                        .getString(APP_PATH_KEY);
                if (appPath != null) {
                    LogUtil.e(TAG, "try to install " + appPath);

                    try {
                        am.installPackage(appPath);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    LogUtil.e(TAG, "install of " + appPath + " done");
                }
            }
            break;
        }
        case UNISTALL_PACKAGE: {
            if (bundle != null) {
                Stringpackagename= bundle
                        .getString(PACKAGE_NAME_KEY);
                if (packagename != null) {
                    LogUtil.e(TAG, "unistall " + packagename);

                    try {
                        deletePackageMethod
                                .invoke(pm, packagename, null, 0);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            break;
        }
        case SET_PREFERRED_LAUNCHER: {
            if (bundle != null) {
                Stringpackage_name= bundle
                        .getString(PREFERRED_PACKAGE_KEY);
                if (package_name == null) {
                    LogUtil.e(TAG,
                            "WARNING: setDefaultActivity cannot continue, package is NULL");
                    return;
                }
                Stringactivity_name= bundle
                        .getString(PREFERRED_ACTIVITY_KEY);
                if (activity_name == null) {
                    LogUtil.e(TAG,
                            "WARNING: setDefaultActivity cannot continue, activity is NULL");
                    return;
                }

                LogUtil.e(TAG, "setDefaultActivity activity="
                        + activity_name + " package=" + package_name);

                IntentFilterfilter=newIntentFilter(
                        "android.intent.action.MAIN");
                filter.addCategory("android.intent.category.HOME");
                filter.addCategory("android.intent.category.DEFAULT");
                ComponentName[] components = newComponentName[] {
                        newComponentName("com.android.launcher",
                                "com.android.launcher2.Launcher"),
                        newComponentName(package_name, activity_name) };
                ComponentNameactivity=newComponentName(package_name,
                        activity_name);
                try {
                    setPreferredActivityMethod.invoke(pm, filter,
                            IntentFilter.MATCH_CATEGORY_EMPTY, components,
                            activity);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        }

    }

}

Remember to add in your manifest file this permission:

<uses-permissionandroid:name="android.permission.SET_PREFERRED_APPLICATIONS"/>

Usage:

publicvoidsetPreferredLauncher(String activity_name,String package_name)
{
    Intent intent = newIntent(UPDATER_SERVICE_ACTION);
    intent.putExtra(REQUEST_KEY, SET_PREFERRED_LAUNCHER);
    intent.putExtra(PREFERRED_ACTIVITY_KEY, activity_name);
    intent.putExtra(PREFERRED_PACKAGE_KEY, package_name);
    context.startService(intent);
}

where:

publicstaticfinalString_UPDATER_SERVICE_ACTION="com.android.updaterservice.ACTION";
publicstaticfinalStringREQUEST_KEY="com.android.updaterservice.REQUEST_KEY";
publicstaticfinalStringPACKAGE_NAME_KEY="com.android.updaterservice.PACKAGE_NAME_KEY";
publicstaticfinalStringAPP_PATH_KEY="com.android.updaterservice.APP_PATH_KEY";
publicstaticfinalStringPREFERRED_ACTIVITY_KEY="com.android.updaterservice.PREFERRED_ACTIVITY_KEY";
publicstaticfinalStringPREFERRED_PACKAGE_KEY="com.android.updaterservice.PREFERRED_PACKAGE_KEY";
publicstaticfinalStringINSTALL_PACKAGE_RESULT="com.android.updaterservice.INSTALL_PACKAGE_RESULT";
publicstaticfinalStringPACKAGE_NAME="PACKAGE_NAME";
publicstaticfinalStringINSTALL_SUCCEEDED="INSTALL_SUCCEEDED";
publicstaticfinalintREQUEST_UNKNOWN= -1;
publicstaticfinalintINSTALL_APPLICATION=1;
publicstaticfinalintUNISTALL_PACKAGE=2;
publicstaticfinalintSET_PREFERRED_LAUNCHER=3;

Post a Comment for "Android: Change Default Home Application"