搜索
您的当前位置:首页正文

Android App动态显示多个入口

来源:二三娱乐

经常看到有App提供多个入口,就是桌面上显示有多种图标,点不同图标进入不同的功能页。
基本实现很简单,以下是步骤。

1. AndroidManifest.xml里注册别名。

<application>里添加<activity-alias>

        <activity-alias
            android:name=".test"
            android:enabled="false"
            android:icon="@drawable/applogo"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:clearTaskOnLaunch="true"
            android:targetActivity=".splashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity-alias>

run一下你就会发现桌面存在两个app的图标了,也就是存在两个入口了。

说明一下:
android:name参数就是这个入口的名称。
android:enabled打开和关闭的参数。
android:icon入口显示的图标,这样就可以给不同的入口设置不同的图标了。
android:label就是桌面上显示的名称。
android:targetActivity是重点,就是你点击了不同的入口会选择进入的页面。通过这个就能设置不同入口的不同功能了。
搞定,So easy!
不要满足,我们拓展一下。

2.动态更新入口。

刚才我们设置了android:name,通过名称,我们就可以使用PackageManager对他进行操作了。
假设我们有个主要的LAUNCHER。(像这样我们把名叫splashActivity的页面设置为了LAUNCHER):

<activity
            android:name=".splashActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@style/FirstTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

以及一个上面这样的activity-alias,名字叫test
我们通过PackageManagersetComponentEnabledSetting方法就可以关闭和打开入口:

    private ComponentName mDefault = getComponentName();
    private ComponentName mDoubleSpring = new ComponentName(getBaseContext(),"com.boosj.boosjapp.test");
    private PackageManager mPm = getApplicationContext().getPackageManager();

    disableComponent(mDefault);
    enableComponent(mDoubleSpring);
    
    private void enableComponent(ComponentName componentName) {
        
    }

    private void disableComponent(ComponentName componentName) {
        
    }

这个例子中就关闭了主入口,仅仅保留了test入口。执行后查看桌面就会发现只剩下test的入口图标了。

当然,也可以给他加上条件,实现动态自动更新。

    private ComponentName mDefault = getComponentName();
    private ComponentName mDoubleSpring = new ComponentName(getBaseContext(),"com.boosj.boosjapp.test");
    private PackageManager mPm = getApplicationContext().getPackageManager();

        /**
         * 计算是否在日期范围内
         */
        try {
            Date date = new Date();
            Log.d("LOGCAT",mathFactory.Date2ms("2018-03-01 00:00:00")+"_"+date.getTime());
            if (mathFactory.Date2ms("2018-03-01 00:00:00")>date.getTime()) {
                disableComponent(mDefault);
                enableComponent(mDoubleSpring);
            }else {
                enableComponent(mDefault);
                disableComponent(mDoubleSpring);
            }
        }catch (Exception e){
            enableComponent(mDefault);
            disableComponent(mDoubleSpring);
        }

    private void enableComponent(ComponentName componentName) {
        
    }

    private void disableComponent(ComponentName componentName) {
        
    }

这个例子中,实现了2018.3.1日前桌面会显示activity-alias的图标,而原来的主要图标被隐藏了。等到2018.3.1过完,图标又回复原样。

3.一个小问题。

这个方法有个问题,就是在主LAUNCHERdisable期间,如果要更新App,安装完毕并启动时就会报找不到LAUNCHER文件的错误而使App崩溃。而不让更新或者让先卸载再重新安装显然是很智障的。所以那种只显示一个图标,按条件动态更新的需求,用此方法是恐怕不能做到了。
(或者有什么更好的主意,欢迎探讨。)


这个方法其实本来是想做App图标的动态更新的。(然而最终失败了,具体原因就是上面说的崩溃问题。)
还是把他作为多入口的方法记录一下吧。

Top