如何在android中停止已存在的快捷方式消息

时间:2015-02-27 12:25:34

标签: android

我有android设备主页中创建快捷方式图标的代码片段。

private void addShortcut() {
    Intent shortcutIntent = new Intent(getApplicationContext(),MainActivity.class);     
    shortcutIntent.setAction(Intent.ACTION_MAIN);
    shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    int flags = Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT;
    shortcutIntent.addFlags(flags);
    Intent addIntent = new Intent();
    addIntent.putExtra("duplicate", false);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "SPExec");
    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);
}

然后以这种方式调用它:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);      
        addShortcut();
}

我不想在应用程序启动时显示此消息。如何删除此图标重复消息或如何减轻图标重复?
enter image description here

1 个答案:

答案 0 :(得分:-1)

我在其他帖子中找到了解决方案,他们使用此功能,它对我来说很好用

private void addShortcut()
{
    SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    Boolean isAppInstalled = appPreferences.getBoolean("isAppInstalled", false);

    String currentLanguage = Locale.getDefault().getDisplayLanguage();
    String previousSetLanguage = appPreferences.getString("phoneLanguage", Locale.getDefault().getDisplayLanguage());
    Boolean shortcutReinstall = false;
    if (!previousSetLanguage.equals(currentLanguage)) {
        shortcutReinstall = true;
    }

     if(!isAppInstalled || shortcutReinstall){

        Intent HomeScreenShortCut= new Intent(getApplicationContext(),
                BasicEntranceActivity.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();
    }
}