gpt4 book ai didi

android - 如何检查主屏幕快捷方式是否存在?

转载 作者:太空宇宙 更新时间:2023-11-03 11:39:47 25 4
gpt4 key购买 nike

有没有办法确定某个特定的主屏幕快捷方式存在?或者有没有办法停止出现的 Toast 消息何时安装和卸载主屏幕快捷方式?

我的应用程序在设备的主屏幕上安装了一个快捷方式某些条件下的启动时间,我不想重复出现的快捷方式。我也不希望 toast 消息出现说每次“创建快捷方式”或“快捷方式已存在”设备启动。有什么解决方案可以阻止这些 Toast 消息吗?

谢谢

最佳答案

它不应在设备重启时安装。使用以下代码:

public void createOrUpdateShortcut() {

appPreferences = PreferenceManager.getDefaultSharedPreferences(this);
isAppInstalled = appPreferences.getBoolean("isAppInstalled", false);

String currentLanguage = Locale.getDefault().getDisplayLanguage();
String previousSetLanguage = appPreferences.getString("phoneLanguage", Locale.getDefault().getDisplayLanguage());

if (!previousSetLanguage.equals(currentLanguage)) {
shortcutReinstall = true;
}

if(!isAppInstalled || shortcutReinstall){

Intent HomeScreenShortCut= new Intent(getApplicationContext(),
BrowserLauncherActivity.class);

HomeScreenShortCut.setAction(Intent.ACTION_MAIN);
HomeScreenShortCut.putExtra("duplicate", false);

if(shortcutReinstall) {
Intent removeIntent = new Intent();
removeIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, HomeScreenShortCut);
String prevAppName = appPreferences.getString("appName", getString(R.string.app_name));
removeIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, prevAppName);
removeIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(removeIntent);
}

Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, HomeScreenShortCut);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.drawable.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);


//Make preference true
SharedPreferences.Editor editor = appPreferences.edit();
editor.putBoolean("isAppInstalled", true);
editor.putString("phoneLanguage", currentLanguage);
editor.putString("appName", getString(R.string.app_name));
editor.commit();
}

}

当用户更改语言以反射(reflect)新语言的名称时,我会更新它。

关于android - 如何检查主屏幕快捷方式是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22344639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com