一键启用或禁用启动意图过滤器活动 [英] Enable or disable launch intent-filter activities with one button

查看:131
本文介绍了一键启用或禁用启动意图过滤器活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个活动的应用程序,它们在清单中都有一个启动意图过滤器,因此它们可以在启动器上显示几个图标,有一个主要活动,其余的默认情况下已被android:enabled="false"禁用这是我的清单的一部分:

I have an app with several activities, they all have a launch intent-filter in the manifest so they can show several icons on the launcher, there is a main activity and the rest of them are disabled by default with android:enabled="false" here is a part of my manifest:

    <activity
        android:name="com.myapp.MainActivity"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.myapp.Activity_1"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/secondary_activity"
        android:enabled="false">// HERE I DISABLE THE ACTIVITY
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

我找到了一种使用以下代码启用或禁用其他活动的方法:

I found a way to enable or disable the other activities using the following code:

public static void enableComponent(Context context, Class<?> componentClass, boolean isEnable) {
   int enableFlag = isEnable ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
   context.getPackageManager().setComponentEnabledSetting(new ComponentName(context, componentClass), enableFlag, PackageManager.DONT_KILL_APP);
}
private void setupDetailsOverviewRowPresenter() {
  detailsPresenter.setOnActionClickedListener(new OnActionClickedListener() {

    @Override
    public void onActionClicked(Action action) {
        if (action.getId() == ACTION_ENABLE){
                mSelectedApp = (App) getActivity().getIntent().getSerializableExtra(DetailsActivity.APP);
                enableComponent(mContext, com.myapp.Activity_1.class, true);
            }
        }else if (action.getId() == ACTION_DISABLED){
                mSelectedApp = (App) getActivity().getIntent().getSerializableExtra(DetailsActivity.APP);
                enableComponent(mContext, com.myapp.Activity_1.class, false);
        }
       }
    });
}

这可以通过使用ACTION_ENABLEACTION_DISABLE按钮启用或禁用活动来完美地实现,但这对可用性不利,相反,我只想使用一个按钮来启用或禁用活动.

This works perfectly by enabling or disabling the activity with the ACTION_ENABLE or ACTION_DISABLE buttons, but that's not good for usability, instead I would like to use just one button to enable or disable the activity.

我需要知道的是如何获取活动的状态,因此,如果活动是android:enabled="false",则显示带有ACTION_EANBLE的按钮,如果活动是android:enabled="true",则显示带有ACTION_DISABLE的按钮./p>

What I need to know is how to get the status of the activity, so if the activity is android:enabled="false" display the button with ACTION_EANBLE and if the activity is android:enabled="true" display the button with ACTION_DISABLE.

推荐答案

您可以查询PackageManager以确定是否启用了组件:

You can query the PackageManager to determine if a component is enabled or not:

PackageManager pm = getPackageManager();
ComponentName cn = new ComponentName(...);
ActivityInfo info = pm.getActivityInfo(cn, 0);
if (info != null && info.enabled) {
    // Component is enabled
    ...
} else {
    // Component is disabled
    ... 
}

这篇关于一键启用或禁用启动意图过滤器活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆