1

I have an app that I want to set as the Default Home Screen, but I'm running into a strange problem.

I have a setting that allows the user to select the default Home Screen.

I use the following code to allow them to select the Default Activity:

Intent selector = new Intent(Intent.ActionMain);
selector.AddCategory(Intent.CategoryHome);
selector.AddCategory(Intent.CategoryDefault);
selector.SetComponent(new ComponentName("android", "com.android.internal.app.ResolverActivity"));

StartActivity(selector);

With no Default currently set, I run that code and I select my app as the Default and tell it to Always use that.

Now, I run the code again, and tell it to use a different Activity (not mine) and to Always use it.

The problem is, is that it never switches to anything different if the default set.

I've seen other applications that allow you to do this, so I'm missing something, I just don't know what.

I'm testing this on my Samsung Galaxy S4 with API Level set to 14.

Brien King
  • 334
  • 2
  • 16
  • use `startActivity(selector)` instead of `StartActivity(selector)` – TN888 Jun 24 '14 at 08:32
  • This is Xamarin and C#, that change would not compile. – Brien King Jun 25 '14 at 04:59
  • As a follow up, I've tried the following: http://stackoverflow.com/questions/13167583/clearing-and-setting-the-default-home-application-solved and it doesn't appear to work. Testing with API14 on Samsung Galaxy4 and Samsung Galaxy5. – Brien King Jul 03 '14 at 05:15

2 Answers2

3

Ok, I found the answer to my question.

The answer is here: http://www.trustydroid.com/2014/05/19/force-show-default-app-chooser-dialog/

The one thing you should keep in mind is that the Component Name MUST be correct. Apparently under API14, it ignores the component (and acts like it works) if the class name is not correct. So it goes through the motions of Enabling the component, Launching the Chooser, then disabling the component. However, it never saves the new default.

After I tried compiling under API19, the OS threw an exception which lead me to fix the issue I was having with it working (which was an incorrect class name).

Once I had that straightened out, it did exactly what I wanted.

For completeness sake, here is the code:

Create a FakeActivity like this:

[Activity(Label = "FakeLauncher", Enabled = false)]
[IntentFilter(new[] { Intent.ActionMain }, Categories = new[] { Intent.CategoryHome, Intent.CategoryDefault })]
public class FakeLauncher : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Create your application here
    }
}

Then where ever you want to change your default home you run this code:

 ComponentName componentName = new ComponentName(Application.PackageName, "<fake activity class name>");
 PackageManager.SetComponentEnabledSetting(componentName, Android.Content.PM.ComponentEnabledState.Enabled, Android.Content.PM.ComponentEnableOption.DontKillApp);

 Intent tempIntent = new Intent(Intent.ActionMain);
 tempIntent.AddCategory(Intent.CategoryHome);

 StartActivity(tempIntent);

 PackageManager.SetComponentEnabledSetting(componentName, Android.Content.PM.ComponentEnabledState.Disabled, Android.Content.PM.ComponentEnableOption.DontKillApp);
Brien King
  • 334
  • 2
  • 16
2

If you are still receiving the exception that Component Name is not correct

Unhandled Exception:
Java.Lang.IllegalArgumentException: Component class FakeLauncher does not exist in com.application.name

You can get the correct "fake activity class name" in Xamarin like this:

string className = Java.Lang.Class.FromType(typeof(FakeLauncher)).Name;

The component name can be pretty random, like this: md546c42bb6607ccd24974e44efa088a043.FakeLauncher