[英]Android how to programmatically hide launcher icon
my app is designed to only need to be run once.我的应用程序设计为只需要运行一次。 As such I want to hide the icon from the launcher after the first run, but without uninstalling the app.
因此,我想在第一次运行后隐藏启动器中的图标,但不卸载应用程序。
I have seen similar applications - they can remove their own icons from the launcher app list.我见过类似的应用程序 - 他们可以从启动器应用程序列表中删除自己的图标。 How can I achieve the same results?
我怎样才能达到同样的结果? Thank you.
谢谢你。
PackageManager p = getPackageManager();
p.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
Note that the icon may not be gone until the next reboot.请注意,该图标可能要到下次重新启动时才会消失。
Hide app's icon using below code使用下面的代码隐藏应用程序的图标
PackageManager pkg=this.getPackageManager();
pkg.setComponentEnabledSetting(new ComponentName(this,SplashActivity.class),PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
// activity which is first time open in manifiest file which is declare as <category android:name="android.intent.category.LAUNCHER" />
// 首次在声明为
<category android:name="android.intent.category.LAUNCHER" />
清单文件中打开的活动
Here is how to bring back the app's icon这是恢复应用程序图标的方法
PackageManager p = getPackageManager();
ComponentName componentName = new ComponentName(this,SplashActivity.class);
p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
With Android Q (API 29) Google changed the Launcher icon visibility behaviour.在 Android Q (API 29) 中, Google 更改了启动器图标可见性行为。 Even if you disable your Launcher Activity or completely remove the android.intent.category.LAUNCHER
<intent-filter>
from all your Activities, the app will appear in the launcher and open the Android OS app settings, with the exception of:即使您禁用 Launcher Activity 或从所有 Activity 中完全删除android.intent.category.LAUNCHER
<intent-filter>
,该应用程序也会出现在启动器中并打开 Android 操作系统应用程序设置,但以下情况除外:
通过在 AndroidManifest 的 Activity 声明中不包含带有 MAIN 和 LAUNCHER 的意图过滤器,您可以拥有一个没有启动器的应用程序 - 那么问题就变成了如何进行第一次启动.. Widget 也许?
Hide app icon using this code:使用以下代码隐藏应用程序图标:
PackageManager p = getPackageManager();
ComponentName componentName = new ComponentName(this, com.apps.MainActivity.class); // activity which is first time open in manifiest file which is declare as <category android:name="android.intent.category.LAUNCHER" />
p.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
and bring it back, with this:把它带回来,用这个:
PackageManager p = getPackageManager();
ComponentName componentName = new ComponentName(this, com.apps.MainActivity.class);
p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
NOTE: This will not work for Android 10注意:这不适用于 Android 10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.