web-dev-qa-db-ja.com

アプリのインストール時に、アプリケーションのショートカットをホーム画面に追加するにはどうすればよいですか?

アプリをインストールしたらすぐに(起動する前でも)、ホーム画面にアプリのショートカット/ランチャーアイコンを作成したい。それは可能ですか?どうすればいいですか?

29
S P

注:この答えは間違っています。これを行う方法については、たとえば、 ロビンの答え を参照してください。


私の知る限り、アプリはホーム画面にそれ自体を強制することはできません。ランチャーアプリが管理するアプリリストに追加されますが、ホーム画面は通常ユーザーが管理します。アプリにホーム画面を乱雑にする機能を与えることは、悪用への開かれた招待になります。

12
Ted Hopp

ICS以降、次のようにできます。

public void createShortCut(){
    Intent shortcutintent = new Intent("com.Android.launcher.action.INSTALL_SHORTCUT");
    shortcutintent.putExtra("duplicate", false);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));
    Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), EnterActivity.class));
    sendBroadcast(shortcutintent);
}

ランチャーのソースコードも参照してください: このリンク

編集:誰かがコメントを読むのを逃した場合、次の行を追加します。

これには"com.Android.launcher.permission.INSTALL_SHORTCUT"権限が必要です

44
Robin

許可を追加するのを忘れました:

 <uses-permission
        Android:name="com.Android.launcher.permission.INSTALL_SHORTCUT" />
    <uses-permission
        Android:name="com.Android.launcher.permission.UNINSTALL_SHORTCUT" />

ここに素晴らしいチュートリアル: http://androidforbegineers.blogspot.in/2014/01/Android-home-screen-shortcut.html#

13
DoronK

アプリケーションをインストールする場合、アプリケーションもサービスも他のプロセスもアクティブになりません! Androidユーザーにアプリケーションを「開く」ための最初のステップを実行してほしい。ショートカットを直接インストールすることはできません)答えです注意:ほとんどのデバイスでは、ランチャーがアプリケーション自体のショートカットを作成します。

ユーザーが何らかの形でアプリを少なくとも1回起動した後に、ランタイムにショートカットをインストールできますか? :yes(ロビンスの回答を参照)。

回避策はありますか?たぶん、しかし良いものはありません。

更新、別のヒント:ユーザーのデバイスに既にアプリケーションがある場合。 (たとえば、インストールされる2番目のアプリが「going pro」などのキーである場合)、ユーザーが2番目のアプリを起動することなく、2番目のアプリを実際に起動できます(ショートカットを追加します) 。

5
JacksOnF1re
  1. ショートカットの意図を呼び出す関数を作成します。

    private void criarAtalho() {
        Intent shortcutIntent = new Intent(getApplicationContext(), SplashScreen.class);
        shortcutIntent.setAction(Intent.ACTION_MAIN);
        Intent addIntent = new Intent();
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "nomeDaApp");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.mipmap.ic_launcher));
    
        addIntent.setAction("com.Android.launcher.action.INSTALL_SHORTCUT");
        addIntent.putExtra("duplicate", false);  //may it's already there so don't duplicate
        getApplicationContext().sendBroadcast(addIntent);
    }
    
  2. onCreatに関数の呼び出しを記述します。

    if(!getSharedPreferences("APP_PREFERENCE", Activity.MODE_PRIVATE).getBoolean("IS_ICON_CREATED", false)){
        criarAtalho();
        getSharedPreferences("APP_PREFERENCE", Activity.MODE_PRIVATE).edit().putBoolean("IS_ICON_CREATED", true).commit();
    }
    
  3. マニフェストにアクセス許可を設定します。

    <uses-permission
        Android:name="com.Android.launcher.permission.INSTALL_SHORTCUT" />
    
3
Pedro Paulo

最初に、マニフェストにショートカットを追加するためのアクセス許可を追加します。

<uses-permission
    Android:name="com.Android.launcher.permission.INSTALL_SHORTCUT" />

次に、以下の関数を呼び出します。

public void createShortcut(){
    Intent intentShortcut = new Intent("com.Android.launcher.action.INSTALL_SHORTCUT");
    intentShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
    Parcelable appicon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher);
    intentShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, appicon);
    intentShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), MainActivity.class));
    intentShortcut.putExtra("duplicate", false);
    sendBroadcast(intentShortcut);
}
1
Aby Olangattu