3

I have been trying to (ADD and then) REMOVE my APP's shortcut from HOME-SCREEN. ADDING a shortcut works perfectly however I'm not able to remove the shortcut I've created using below code.

public void setupShortCut(boolean create) {
        shortcutIntent = new Intent();
        shortcutIntent.setClassName("com.abc.xyz", "XYZActivity");
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);

        Intent intentShortcut = new Intent();
        intentShortcut.putExtra("android.intent.extra.shortcut.INTENT", shortcutIntent);
        intentShortcut.putExtra("android.intent.extra.shortcut.NAME", getResources().getString(R.string.app_name));
        intentShortcut.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", icon);
        if(create) {
          intentShortcut.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        } else {
        intentShortcut.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
        }
        sendBroadcast(intentShortcut);
    }

Please suggest where am I going wrong?

EDIT 1:

I have required permission in my Manifest file:

<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /
4

3 回答 3

4

干得好:

private void deleteShortCut(Context context) {

    Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
    shortcutIntent.setClassName("com.example.androidapp", "SampleIntent");
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    shortcutIntent.putExtra("someParameter", "HelloWorld");

    Intent removeIntent = new Intent();
    removeIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    removeIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ShortcutName");
    removeIntent.putExtra("duplicate", false);

    removeIntent
            .setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");       
    context.sendBroadcast(removeIntent);
}
于 2012-10-12T06:43:15.357 回答
1

要删除快捷方式,请尝试使用以下代码...

final Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setComponent(new ComponentName(this.getPackageName(), "YourClassName"));

final Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));          
intent.setComponent(new ComponentName(this.getPackageName(), "YourClassName"));                     
intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");

sendBroadcast(intent, null);

向您的清单文件添加以下权限:

<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />   
于 2012-10-12T06:46:18.717 回答
1

找出根本原因:-)

我想出了它对我不起作用的原因。我在手机上使用了不同的第 3 方启动器(Stock android 启动器除外)。App-Shortcut只要您使用的启动器支持该操作,就可以创建和删除作品。我在默认启动器上运行了上面的代码,它就像一个魅力:)

谢谢大家的回复!

于 2013-11-29T05:14:59.880 回答