是否可以在运行时以编程方式更新Android Manifest? [英] Is it possible to update Android Manifest programmatically at runtime?

查看:82
本文介绍了是否可以在运行时以编程方式更新Android Manifest?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个程序,该程序以编程方式更改应用程序图标和应用程序名称.

I am creating an application which changes the app icon and app name programmatically.

我已经实现了这一点,因为通过在Android清单中声明活动别名,只有5个名称和图标.

I've already achieved this because there are only 5 names and icons by declaring activity-alias in android manifest.

如果您只有5个名字,那么很容易在清单中声明它.但是,如果您不知道该怎么办.的名称,您必须以编程方式更新清单.

If you have only 5 names then it's easy to declare it in manifest. But what if you don't know the no. of names and you have to update the manifest programmatically.

下面是我的代码:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.drawerstack">

    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity-alias android:label="@string/app_name1"
            android:icon="@drawable/ic_launcher"
            android:name=".MainActivity-Red"
            android:enabled="false"
            android:targetActivity=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>

        <activity-alias android:label="@string/app_name2"
            android:icon="@drawable/ic_launcher_1"
            android:name=".MainActivity-Pink"
            android:enabled="false"
            android:targetActivity=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>

        <activity-alias android:label="@string/app_name3"
            android:icon="@drawable/ic_launcher_2"
            android:name=".MainActivity-Blue"
            android:enabled="false"
            android:targetActivity=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>

        <activity-alias android:label="@string/app_name4"
            android:icon="@drawable/ic_launcher_3"
            android:name=".MainActivity-Grey"
            android:enabled="false"
            android:targetActivity=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>
    </application>
</manifest>

MainActivity.java

    img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (view.getTag().equals(0)) {
                changeIcon("com.drawerstack.MainActivity-Red");
                removeIcon("com.drawerstack.MainActivity");
                removeIcon("com.drawerstack.MainActivity-Pink");
                removeIcon("com.drawerstack.MainActivity-Blue");
                removeIcon("com.drawerstack.MainActivity-Grey");
            } else if (view.getTag().equals(1)) {
                changeIcon("com.drawerstack.MainActivity-Pink");
                removeIcon("com.drawerstack.MainActivity-Blue");
                removeIcon("com.drawerstack.MainActivity-Grey");
                removeIcon("com.drawerstack.MainActivity-Red");
                removeIcon("com.drawerstack.MainActivity");
            } else if (view.getTag().equals(2)) {
                changeIcon("com.drawerstack.MainActivity-Blue");
                removeIcon("com.drawerstack.MainActivity-Pink");
                removeIcon("com.drawerstack.MainActivity-Grey");
                removeIcon("com.drawerstack.MainActivity-Red");
                removeIcon("com.drawerstack.MainActivity");
            } else if (view.getTag().equals(3)) {
                changeIcon("com.drawerstack.MainActivity-Grey");
                removeIcon("com.drawerstack.MainActivity-Pink");
                removeIcon("com.drawerstack.MainActivity-Red");
                removeIcon("com.drawerstack.MainActivity-Blue");
                removeIcon("com.drawerstack.MainActivity");
            }
        }
    });


private void changeIcon(final String pkgName) {

    doAsynchronousTask = new TimerTask() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            Date now = new Date();
            if(now.after(afterDate)){
                timer.cancel();
                Common.dismissProgressDialog(MainActivity.this);
                finish();
            }
        }
    };
    timer.schedule(doAsynchronousTask, 0, 50000);

    Common.loadProgressDialog(MainActivity.this,false);
    getPackageManager().setComponentEnabledSetting(
            new ComponentName("com.drawerstack", pkgName),
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}


private void removeIcon(String removePkg) {
        getPackageManager().setComponentEnabledSetting(
                new ComponentName("com.drawerstack", removePkg),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}

推荐答案

假设您的问题是标题中的问题:

Assuming your question is the one in title:

是否可以通过编程方式更新Android Manifest?

Is it possible to update Android Manifest programmatically?

答案为,因此无法通过编程方式进行.

The answer is no, it is not possible to do that programmatically.

您可以启用/禁用清单中声明的​​组件,但是不能在其中添加或删除某些内容,因为顾名思义,这是清单文件,系统在安装应用时会读取"该文件.因此,您要在该清单中声明您的应用程序界面,以后您将无法更改它,除非您使用其他清单重新安装该应用程序.

You can enable/disable components that are declared in your manifest, but you cannot add or remove something from there, because, as the name says, it's a manifest file, which system "reads" when it installs your app. So you are declaring your app's interface in that manifest, later on you cannot change that, unless you reinstall the app with another manifest.

假设系统为您提供了执行此操作的能力.然后,您将为清单动态添加一些权限,而用户从Play商店下载您的应用时并未批准该权限.

Assume system provided you ability to do that. Then you'd add some permission to your manifest dynamically, whereas user hasn't approved that while he was downloading your app from Play Store.

这篇关于是否可以在运行时以编程方式更新Android Manifest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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