3

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

在某些情况下,我的应用程序会在设备启动时在主屏幕上安装快捷方式,并且我不希望出现重复的快捷方式。我也不希望每次设备启动时都显示“快捷方式已创建”或“快捷方式已存在”的 Toast 消息。有什么解决方案可以阻止这些 Toast 消息吗?

谢谢

4

1 回答 1

12

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

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();
    }

}

当用户更改语言以反映新语言的名称时,我会更新它。

于 2014-03-12T07:30:41.660 回答