문제

Is there a way to determine whether a particular home screen shortcut exists? Or is there a way to stop the Toast messages that appear when a home screen shortcut is installed and uninstalled?

My application installs a shortcut on the home screen at device startup time under certain conditions and I don't want duplicate shortcuts to appear. I also don't want Toast messages appearing saying "Shortcut created" or "Shortcut already exists" every single time the device boots. Is there any solution to stop these Toast messages?

Thanks

도움이 되었습니까?

해결책

It should not install on device reboot. use the following code:

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

}

I update it when the user change the language to reflect the name in new language.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top