Search code examples
androidreflectionlauncher

How to Refresh Android's Home Launcher?


I've been writing a background service which enforces my client's security policies on company devices. When the service is first installed, i launch an activity so that initial setup data can be entered. After this, i disable the activity:

ComponentName componentToDisable = new ComponentName("com.acme.background","com.acme.background.FirstLaunch");
        getPackageManager().setComponentEnabledSetting(componentToDisable,
         PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
         PackageManager.DONT_KILL_APP);
FirstLaunch.this.finish();

At this point, the temporary icon is still visible until the Home Launcher is refreshed/restarted. This can be quite awhile on some devices, and the issue i'm having is that users get confused and uninstall via the icon. I need to refresh the home launcher. Here is one method i've tried via reflection, which gives me the current home launcher and access to its methods.

PackageManager PM = getPackageManager();
Intent home_intent = new Intent("android.intent.action.MAIN");
home_intent.addCategory("android.intent.category.HOME");
home_intent.addCategory("android.intent.category.DEFAULT");
ComponentName cn = home_intent.resolveActivity(PM);
Context foreignContext = createPackageContext("com.android.launcher", Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
String clName = cn.getClassName();
Class<?> test = foreignContext.getClassLoader().loadClass(clName);

I've tried firing methods and also nulling fields to get a refresh/restart, but it doesnt seem to work. Does anyone have a solution to refresh the appDrawer/Icons?

EDIT:

I've included code that i launch after a dialog is fired. Below is what i'm using:

PackageManager PM = getPackageManager();
Intent home_intent = new Intent("android.intent.action.MAIN");
home_intent.addCategory("android.intent.category.HOME");
home_intent.addCategory("android.intent.category.DEFAULT");
ComponentName cn = home_intent.resolveActivity(PM);
String pkgName = cn.getPackageName();
Intent intent;
if (android.os.Build.VERSION.SDK_INT >= 9) {                       
    Uri packageURI = Uri.parse("package:" + pkgName);
    intent = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS", packageURI);
    startActivity(intent);
    FirstLaunch.this.finish();
    performCleanup();
}  else  {
    //support for 2.1
    intent = new Intent(Intent.ACTION_VIEW); 
    intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails"); 
    intent.putExtra("com.android.settings.ApplicationPkgName", pkgName); 
    intent.putExtra("pkg", pkgName); 
    startActivity(intent);  
    FirstLaunch.this.finish();
    performCleanup();
}

Solution

  • Unfortunately, you can't do that -- you'll have to wait until the launcher is restarted by the user.

    One common way to deal with this is to show a toast/dialog once the icon has been "removed" asking the user the restart the launcher in order for the icon to be fully removed.