2

プログラムで Android アプリのホーム画面にショートカット アイコンを作成しようとしたときに問題が発生しました。

ショートカットアイコンを作成できましたが、その後、「アプリケーションのショートカットが作成されました」というアラートが表示され、アプリケーションが閉じます。アプリケーションを閉じたくありません。可能であれば、ショートカットを作成した後にポップアップするアラートを取り除きたいです。

どうすればそれを達成できますか?

これが私の現在のコードです:

Intent targetIntent = new Intent (Intent.ACTION_MAIN);
targetIntent.setClassName (getApplicationContext(), "com.mainListActivity");
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutintent.addFlags(Intent.FLAG_FROM_BACKGROUND);

//repeat to create is forbidden
shortcutintent.putExtra("duplicate", false);

//set the name of shortCut
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"SecureLauncher");

//set icon
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher_cloud);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

//set the application to lunch when you click the icon
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,targetIntent);
sendBroadcast(shortcutintent);
4

2 に答える 2

0

ショートカットを作成するときにこのコードを使用します

Intent shortcutIntent = new Intent(getApplicationContext(), LoginScreen.class);     
shortcutIntent.setAction(Intent.ACTION_MAIN);

Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "AIMS DOCTOR");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(),    R.drawable.aims));
addIntent.putExtra("duplicate", false);

addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);

また、マニフェストでこれを使用する必要があります。

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
于 2013-01-28T06:07:59.097 に答える
0

アプリケーションが終了する原因が私のmanifest.xmlにあることがわかりました

これをマニフェストに含める前に: レシーバー部分を削除しました:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<receiver
    android:name=".CreateShortcut"
    android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
    <intent-filter>
        <action android:name="com.android.launcher.action.INSTALL_SHORTCUT"/>
    </intent-filter>
</receiver>

だから私は私のマニフェストにこれしか持っていません:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
于 2012-10-04T08:04:36.747 に答える