Android隐藏应用桌面图标/动态修改桌面图标

Android隐藏应用桌面图标/动态修改桌面图标

应用启动后点击隐藏

注意:在Android11只要在mainifest有权限申请,就不会隐藏

 private void hide() {
        PackageManager packageManager = getPackageManager();
        packageManager.setComponentEnabledSetting(getComponentName(),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
    }

此方式可隐藏桌面图标,即Luncher里看不到应用icon和label,并且后台也看不到,只能手动停止 或卸载,手机管家可清理,启动后进程正常运行,后台日志正常输出

安装后直接隐藏

manifest中配置

<uses-feature
        android:name="android.software.leanback"
        android:required="true" />

启动页的下配置

<category android:name="android.intent.category.LEANBACK_LAUNCHER"/>

完整配置如下示例:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.luncherdemo">
    <uses-feature
        android:name="android.software.leanback"
        android:required="true" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.LuncherDemo">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

此种配置,应用安装后能看到页面,后台也可见,桌面无图标,但是杀死进程后无法再次手动启动app

动态修改应用图标 icon 和 lable

1、清单文件增加权限


<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>

2、下增加一个

<activity-alias
                android:icon="@mipmap/alias_icon"//新的icon
                android:label="@string/app_name2"//新的label
                android:name="changeAfterActivity" //和MainAcitivity同路径
                android:targetActivity=".MainActivity" //targetActivity 和启动页是同一个activity
                android:enabled="false">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

完整代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.luncherdemo">
<!--    <uses-feature-->
<!--        android:name="android.software.leanback"-->
<!--        android:required="true" />-->

    <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.LuncherDemo">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

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

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

</manifest>
	//3调用
    private void chaneIcon() {

        PackageManager packageManager = getPackageManager();
        //显示别名的设置
        packageManager.setComponentEnabledSetting(new ComponentName(this, "com.example.luncherdemo.changeAfter"),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED ,
                PackageManager.DONT_KILL_APP);
        //disable 掉原来的设置
        packageManager.setComponentEnabledSetting(new ComponentName(this, "com.example.luncherdemo.MainActivity"),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED ,
                PackageManager.DONT_KILL_APP);
    }

这样就能动态修改 lable和icon了

    原文作者:legendwon
    原文地址: https://blog.csdn.net/zhongguobaozi/article/details/121166965
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞