Android在代码中设置新的默认主页

Android在代码中设置新的默认主页,android,xamarin,Android,Xamarin,我有一个应用程序,我想设置为默认主屏幕,但我遇到了一个奇怪的问题 我有一个允许用户选择默认主屏幕的设置 我使用以下代码允许他们选择默认活动: Intent selector = new Intent(Intent.ActionMain); selector.AddCategory(Intent.CategoryHome); selector.AddCategory(Intent.CategoryDefault); selector.SetComponent(new ComponentName("

我有一个应用程序,我想设置为默认主屏幕,但我遇到了一个奇怪的问题

我有一个允许用户选择默认主屏幕的设置

我使用以下代码允许他们选择默认活动:

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);
在当前未设置默认值的情况下,我运行该代码,选择我的应用程序作为默认值,并告诉它始终使用该代码

现在,我再次运行代码,并告诉它使用不同的活动(不是我的),并始终使用它

问题是,如果默认设置为0,它将永远不会切换到任何不同的位置

我见过其他应用程序允许您这样做,所以我遗漏了一些东西,我只是不知道是什么


我正在我的三星Galaxy S4上进行测试,API级别设置为14。

好的,我找到了问题的答案

答案如下:

您应该记住的一点是组件名称必须正确。显然,在API14中,如果类名不正确,它会忽略该组件(并表现得像它在工作一样)。因此,它经历了启用组件、启动选择器,然后禁用组件的过程。但是,它从不保存新的默认值

在我尝试在API19下编译之后,操作系统抛出了一个异常,导致我修复了它工作时遇到的问题(这是一个不正确的类名)

一旦我弄清楚了,它就完全符合我的要求

为完整起见,以下是代码:

创建类似以下内容的伪造活动:

[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
    }
}
string className = Java.Lang.Class.FromType(typeof(FakeLauncher)).Name;
然后,无论您想在何处更改默认主页,都要运行以下代码:

 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);
ComponentName ComponentName=newcomponentname(Application.PackageName,”);
PackageManager.SetComponentEnabledSetting(componentName,Android.Content.PM.ComponentEnabledState.Enabled,Android.Content.PM.ComponentEnableOption.DontKillApp);
Intent tempIntent=新Intent(Intent.ActionMain);
tempIntent.AddCategory(Intent.CategoryHome);
星触觉(tempIntent);
PackageManager.SetComponentEnabledSetting(componentName,Android.Content.PM.ComponentEnabledState.Disabled,Android.Content.PM.ComponentEnableOption.DontKillApp);

好的,我找到了问题的答案

答案如下:

您应该记住的一点是组件名称必须正确。显然,在API14中,如果类名不正确,它会忽略该组件(并表现得像它在工作一样)。因此,它经历了启用组件、启动选择器,然后禁用组件的过程。但是,它从不保存新的默认值

在我尝试在API19下编译之后,操作系统抛出了一个异常,导致我修复了它工作时遇到的问题(这是一个不正确的类名)

一旦我弄清楚了,它就完全符合我的要求

为完整起见,以下是代码:

创建类似以下内容的伪造活动:

[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
    }
}
string className = Java.Lang.Class.FromType(typeof(FakeLauncher)).Name;
然后,无论您想在何处更改默认主页,都要运行以下代码:

 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);
ComponentName ComponentName=newcomponentname(Application.PackageName,”);
PackageManager.SetComponentEnabledSetting(componentName,Android.Content.PM.ComponentEnabledState.Enabled,Android.Content.PM.ComponentEnableOption.DontKillApp);
Intent tempIntent=新Intent(Intent.ActionMain);
tempIntent.AddCategory(Intent.CategoryHome);
星触觉(tempIntent);
PackageManager.SetComponentEnabledSetting(componentName,Android.Content.PM.ComponentEnabledState.Disabled,Android.Content.PM.ComponentEnableOption.DontKillApp);

如果您仍然收到组件名称不正确的异常

Unhandled Exception:
Java.Lang.IllegalArgumentException: Component class FakeLauncher does not exist in com.application.name
您可以在Xamarin中获得正确的“假活动类名”,如下所示:

[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
    }
}
string className = Java.Lang.Class.FromType(typeof(FakeLauncher)).Name;

组件名称可以是非常随机的,如下所示:md546c42bb6607ccd2474e44efa088a043.FakeLauncher

如果您仍然收到组件名称不正确的异常

Unhandled Exception:
Java.Lang.IllegalArgumentException: Component class FakeLauncher does not exist in com.application.name
您可以在Xamarin中获得正确的“假活动类名”,如下所示:

[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
    }
}
string className = Java.Lang.Class.FromType(typeof(FakeLauncher)).Name;

组件名称可以是非常随机的,如下所示:md546c42bb6607ccd2474e44efa088a043。FakeLauncher

使用
startActivity(selector)
而不是
startActivity(selector)
这是Xamarin和C#,更改不会编译。作为后续操作,我尝试了以下方法:它似乎不起作用。在三星Galaxy4和三星Galaxy5上使用API14进行测试。使用
startActivity(选择器)
而不是
startActivity(选择器)
这是Xamarin和C#,该更改将无法编译。作为后续操作,我尝试了以下操作:它似乎不起作用。在三星Galaxy4和三星Galaxy5上使用API14进行测试。