删除活动为默认启动

2023-09-05 06:57:39 作者:不稀罕你的爱

我把我的活动为默认启动拦截home键点击,像这样:

I set my activity as a default launcher to intercept home button clicks like so:

<activity
    android:name=".ExampleActivity"
    android:label="@string/app_name">
    <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>
</activity>

在我的活动,为ExampleActivity 启动,如果我点击主页键,我得到提示进行选择。如果我选择的让我的默认的,并选择我的活动,我陷在我的活动根据需要。

When my activity, ExampleActivity is launched, if i click the home key, I get prompted to choose. If I select make this my default and chose my activity, I am stuck In my activity as desired.

现在的问题是,当我离开这个活动,我尝试从默认的启动器中删除我的活动,但我没有成功。

The problem is, when I leave the activity, I try to remove my activity from the default launcher, but am unsuccessful.

我曾尝试:

ComponentName componentName = new ComponentName( 
                    "com.example.exampleactivity", 
                    "com.example.exampleactivity.class");

pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP);

PackageManager pm = getActivity().getPackageManager();
             ComponentName name = new ComponentName(this, "com.example.exampleactivity.class");
             pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);

但我指定的主页永远不会被删除。

有没有人有一个工作方式来解决上述的?

Does anyone have a working way to fix the above?

我只是wan't home键是默认的一项具体活动,不是我的整个应用程序。当我离开这个活动,它应该被删除并恢复到默认值。

I only wan't the home button to be default for a specific activity, not my entire application. When I leave the activity, it should be removed and restored to default.

推荐答案

如果这是你的应用程序,你清除它的默认值,你可以简单地调用:

if it's your app that you're clearing its defaults , you can simply call :

getPackageManager().clearPackagePreferredActivities(getPackageName());

然后,为了显示所选择的对话使用哪个发射器,使用:

then , in order to show the dialog of choosing which launcher to use , use:

final Intent intent=new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
相关推荐