更改MainActivity的活动别名后,应用更新后将删除应用图标

时间:2018-01-07 15:05:58

标签: android android-8.0-oreo

我使用activity-alias来支持特定设备的不同应用图标,如下所示:

    <activity-alias
        android:name=".MainActivity-Vendor"
        android:enabled="false"
        android:icon="@mipmap/app_logo_vendor"
        android:label="@string/app_name_vendor"
        android:exported="true"
        android:targetActivity=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

-----定义MainActivity app图标和文字:

  <activity
        android:name=".MainActivity"
        android:icon="@mipmap/app_logo"
        android:label="@string/app_name">

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

并且它在Android 7.0之前工作正常,但是当我将我的应用程序定位到Android 8.0时,连字符( - )不支持activity-alias名称,所以我更改了活动别名,如下所示,

MainActivity-Vendor ---&gt; MainActivityVendor

我还根据设备供应商更改了活动别名启用/禁用代码,如下所示:

旧代码:

    PackageManager pm = this.getPackageManager();

    // Enable/disable activity-aliases
    pm.setComponentEnabledSetting(
            new ComponentName(this, "com.app.MainActivity-Vendor"),
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP
    );

    int enabled = pm.getComponentEnabledSetting(new ComponentName(this, "com.app.MainActivity"));


    pm.setComponentEnabledSetting(
            new ComponentName(this, "com.app.MainActivity"),
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
            PackageManager.DONT_KILL_APP
    );

新守则:

    PackageManager pm = this.getPackageManager();

    // Enable/disable activity-aliases
    pm.setComponentEnabledSetting(
            new ComponentName(this, "com.app.MainActivityVendor"),
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP
    );

    int enabled = pm.getComponentEnabledSetting(new ComponentName(this, "com.app.MainActivity"));


    pm.setComponentEnabledSetting(
            new ComponentName(this, "com.app.MainActivity"),
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
            PackageManager.DONT_KILL_APP
    );

请注意,在上面我也改变了代码,

MainActivity-Vendor ---&gt; MainActivityVendor

但是在应用升级之后,用户的应用图标消失了。并且在应用程序升级后,用户还没有获得应用程序的“打开”选项。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

虽然我没有找到确切的解决方案,但我设法通过以下方式实现这一目标:

     <receiver android:name=".AppUpdateReceiver">
        <intent-filter>
            <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
        </intent-filter>
    </receiver>

然后在AppUpdateReceiver类中处理启用/禁用活动别名。

  public void onReceive(Context context, Intent intent) {
    //Toast.makeText(context, "Gaana App updated : " + intent.getAction(), Toast.LENGTH_SHORT).show();
    //Log.v(TAG, "app is updated - " + intent.getAction());
    mContext = context;
    if(intent.getAction() != null && intent.getAction().equals("android.intent.action.MY_PACKAGE_REPLACED")) {
        MmxUtils.init();
        if (MmxUtils.isMmxBuild()) {
            setIcon();
        }
    }
}

private void setIcon() {
PackageManager pm = this.getPackageManager();

// Enable/disable activity-aliases
pm.setComponentEnabledSetting(
        new ComponentName(this, "com.app.MainActivityVendor"),
        PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
        PackageManager.DONT_KILL_APP
);

int enabled = pm.getComponentEnabledSetting(new ComponentName(this, "com.app.MainActivity"));


pm.setComponentEnabledSetting(
        new ComponentName(this, "com.app.MainActivity"),
        PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
        PackageManager.DONT_KILL_APP
);
}

它解决了我的问题。