安卓如何动态隐藏显示桌面图标,
1
<data android:host="MainActivity" android:scheme="com.example.hide"
tools:ignore="AppLinkUrlError" />
这种方法只能隐藏不能显示。
2
PackageManager packageManager = getPackageManager();
ComponentName componentName = new ComponentName(this, MainActivity.class);
int res = packageManager.getComponentEnabledSetting(componentName);
if (res == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT
|| res == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
// 隐藏应用图标
packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
} else {
// 显示应用图标
packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
PackageManager.DONT_KILL_APP);
}
这种方法隐藏后就打不开了,没有LAUNCHER
【以下回答由 GPT 生成】
在安卓设备上实现动态隐藏和显示桌面图标的方法是通过创建一个桌面快捷方式,并在需要隐藏或显示时更改该快捷方式的可见性。下面是具体的解决方案:
xml <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
然后,在需要添加快捷方式的地方,使用以下代码创建一个快捷方式: ```java // 创建快捷方式的Intent Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class); shortcutIntent.setAction(Intent.ACTION_MAIN);
// 创建快捷方式的图标 Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
// 创建快捷方式的名称 String shortcutName = "My App Shortcut";
// 添加快捷方式 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); ShortcutInfo shortcut = new ShortcutInfo.Builder(getApplicationContext(), "shortcutId") .setShortLabel(shortcutName) .setIcon(Icon.createWithBitmap(icon)) .setIntent(shortcutIntent) .build();
shortcutManager.requestPinShortcut(shortcut, null);
} else { Intent addIntent = new Intent(); addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName); addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false);
getApplicationContext().sendBroadcast(addIntent);
} ```
隐藏和显示快捷方式
在需要隐藏快捷方式的地方,使用以下代码隐藏快捷方式: ```java // 获取快捷方式的Intent Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class); shortcutIntent.setAction(Intent.ACTION_MAIN);
// 隐藏快捷方式 PackageManager pm = getPackageManager(); pm.setComponentEnabledSetting( new ComponentName(getApplicationContext(), MainActivity.class), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP ); ```
在需要显示快捷方式的地方,使用以下代码显示快捷方式: ```java // 获取快捷方式的Intent Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class); shortcutIntent.setAction(Intent.ACTION_MAIN);
// 显示快捷方式 PackageManager pm = getPackageManager(); pm.setComponentEnabledSetting( new ComponentName(getApplicationContext(), MainActivity.class), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP ); ```
请注意,在某些设备上,隐藏或显示快捷方式可能会导致桌面图标出现延迟,并且在部分应用程序上可能无法正常工作。因此,仍然需要根据实际情况进行测试和适配。
【相关推荐】