views:

1633

answers:

8

In SDK 1.5 I was using the PackageManager class to set the preferred home screen to be my app using PackageManager.addPackageToPreferred(). In the new SDK (using 2.1) this has been deprecated so I'm trying to use addPreferredActivity() for the same result but it's not working as expected.

Some necessary background. I'm writing a lock screen replacement app so I want the home key to launch my app (which will already be running, hence having the effect of disabling the key). When the user "unlocks" the screen I intend to restore the mapping so everything works as normal.

In my AndroidManifest.xml I have:

<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>
<uses-permission android:name="android.permission.SET_PREFERRED_APPLICATIONS">
</uses-permission>

In my code I have the following snippet:

// Set as home activity
// This is done so we can appear to disable the Home key.
PackageManager pm = getPackageManager();
//pm.addPackageToPreferred(getPackageName());

IntentFilter filter = new IntentFilter("android.intent.action.MAIN");
filter.addCategory("android.intent.category.HOME");
filter.addCategory("android.intent.category.DEFAULT");

ComponentName[] components = new ComponentName[] 
{
    new ComponentName("com.android.launcher", ".Launcher")
};

Context context = getApplicationContext();
ComponentName component = new ComponentName(context.getPackageName(),
MyApp.class.getName());

pm.clearPackagePreferredActivities("com.android.launcher");
pm.addPreferredActivity(filter, IntentFilter.MATCH_CATEGORY_EMPTY,
components, component);

The resulting behavior is that the app chooser comes up when I press the Home key, which indicates that the clearPackagePreferredActivities() call worked but my app did not get added as the preferred. Also, the first line in the log below says something about "dropping preferred activity for Intent":

04-06 02:34:42.379: INFO/PackageManager(1017): Result set changed, dropping preferred activity for Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 } type null

04-06 02:34:42.379: INFO/ActivityManager(1017): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 cmp=android/com.android.internal.app.ResolverActivity }

Does anyone know what this first log message means? Maybe I'm not using the API correctly, any ideas? Any help would be greatly appreciated.

A: 

Not really answer (I would be interested in one, as well), just note. After the call of addPreferredActivity method, the record seems to be never stored - if you go into Settings-Application Info, there is not possible to clear defaults. The log message is probably related to this fact. Anyone from Google can comment on that ? Is it possible to set the default activity for intent programmatically ? Thank you.

Pavel Lahoda
+1  A: 

Hi,

This seems to work for me if I initialize the 'components' array to ALL HOME apps on the device:

ComponentName[] components = new ComponentName[] { new ComponentName("com.intuitiveui.android", "com.intuitiveui.android.Friday"), new ComponentName("com.android.launcher2","com.android.launcher2.Launcher") };

My problem is how do I fill this dynamically.

Shimon Shnitzer
+1  A: 

@afonseca: I was dealing with the same problem. Thanks for the code, I used it to start with. Also thanks Shimon. I merged his answer into mine. I've got the code working (on 1.6 and 2.1 update 1). It has been adjusted a little bit, but the 2 main change seem to be Shimons suggestion and: ".Launcher" was changed to "com.android.launcher.Launcher". The working code is posted below.

Ciao, a2ronus

PackageManager pm = getPackageManager();

IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.MAIN");
filter.addCategory("android.intent.category.HOME");
filter.addCategory("android.intent.category.DEFAULT");

Context context = getApplicationContext();
ComponentName component = new ComponentName(context.getPackageName(), TestReplaceHomeAppActivity.class.getName());

ComponentName[] components = new ComponentName[] {new ComponentName("com.android.launcher", "com.android.launcher.Launcher"), component};

pm.clearPackagePreferredActivities("com.android.launcher");
pm.addPreferredActivity(filter, IntentFilter.MATCH_CATEGORY_EMPTY, components, component);
a2ronus
@Shimon: For getting the list of installed Home apps dynamically, you might wanna start at PackageManager.queryIntentActivities(android.content.Intent, int). Good luck, a2ronus.
a2ronus
Thank you both, great answer. I wish there were more examples in the Android docs to begin with.
afonseca
A: 

Hi, I have the similar problem. My app is designed with password protected settings. Also it is needed to protect it from uninstall without the password. When the user try to uninstall the app my Uninstall activity is started , it checks the password, and if it is correct, it resend the intent to standard Uninstaller. For that purpose I have successfully used the next technique for Android 1.5 - 2.0.1 . Now in 2.1 it is deprecated, and I am trying to adopt the addPreferredActivity with no success. Below is my code:

 if (android.os.Build.VERSION.RELEASE.startsWith("2.1")) {
            // The unsuccessful one
  PackageManager pm = getPackageManager();
  IntentFilter filter = new IntentFilter("android.intent.action.DELETE");
  filter.addAction("android.intent.action.VIEW");
  filter.addCategory("android.intent.category.DEFAULT");
  filter.addDataScheme("package");
  Context context = getApplicationContext();
  ComponentName component = new ComponentName(context.getPackageName(), Uninstall.class.getName());
  ComponentName[] components = new ComponentName[] {
    new ComponentName("com.android.settings", ".InstalledAppDetails"),
    new ComponentName("com.android.packageinstaller", ".PackageInstallerActivity"),
    new ComponentName(context.getPackageName(), Uninstall.class.getName())};
  pm.clearPackagePreferredActivities("com.android.packageinstaller");
  pm.addPreferredActivity(filter, IntentFilter.MATCH_CATEGORY_EMPTY, components, component);
 }
 else {
            // works OK for OS 1.5, 1.6, 2.0, 2.0.1
  PackageManager pm = getPackageManager();
  pm.addPackageToPreferred(getPackageName());
 }

What I am missing? Can somebody advice me? Thanks.

Tony Kolev
A: 

Guys,did you really test this code?? It didn't work for me, also the log shows the following:

09-09 07:33:04.456: INFO/PackageManager(972): Adding preferred activity ComponentInfo{com.test/com.test.MyLockScreen}: 09-09 07:33:04.456: INFO/PackageManager(972): Action: "android.intent.action.MAIN" 09-09 07:33:04.456: INFO/PackageManager(972): Category: "android.intent.category.HOME" 09-09 07:33:04.456: INFO/PackageManager(972): Category: "android.intent.category.DEFAULT"

But nothing happened, I got tired from seeing (ResolverActivity) displayed every time I press (HOME).

I'm using Android 2.1

Thanks in advanced..

A: 

Works wonderful thanks guys you've saved my ass :)

bkhashfeh you have to change the AndroidManifest.xml file

sherif
A: 

is there a way to disable this ?

sherif
A: 

addPreferredActivity does not work anymore in 2.2, clearPackagePreferredActivities still works but you can only clear preference for the package you run this on.

there are a lot of threads on android google groups about this problem and google's official position (for now) not to provide you with methods to overide user's preferences.

mishkin