3

APPのショートカットをHOME-SCREENから(追加してから)削除しようとしています。ショートカットの追加は完全に機能しますが、以下のコードを使用して作成したショートカットを削除できません。

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

どこが間違っているのか教えてください。

編集1:

マニフェストファイルで許可が必要です。

<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

根本的な原因を解明:-)

私はそれが私のために働いていなかった理由を理解しました。携帯電話で別のサードパーティ製ランチャー(Stock androidランチャー以外)を使用しています。App-Shortcut使用しているランチャーがその操作をサポートしている限り、作品の作成と削除は機能します。私はデフォルトのランチャーで上記のコードを実行しました、そしてそれは魅力のように機能します:)

みなさん、ありがとうございました!

于 2013-11-29T05:14:59.880 に答える