ボタンを押すだけでアプリをホーム画面に簡単に追加できるようにしたい。だから私が考えているのは、アプリの下部にある「ホーム画面に追加」というボタンであり、それを押すと、アプリケーションを閉じずにホーム画面にショートカットが追加されます。そのためにはどのコードを追加すればよいですか?
4 に答える
結果のインテントをエクストラとして INSTALL_SHORTCUT ブロードキャストを送信します (この場合、結果のインテントは何らかのアクティビティを直接開いています)。
//where this is a context (e.g. your current activity)
final Intent shortcutIntent = new Intent(this, SomeActivity.class);
final Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// Sets the custom shortcut's title
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
// Set the custom shortcut icon
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
// add the shortcut
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(intent);
マニフェストにも次の権限が必要です。
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
わかりました...これが古いスレッドであることはわかっていますが、このスレッドにアクセスしているエンジニアが最新の情報を入手できるようにしたかったのです。
Android O 以降 - バックグラウンド チェックの制限 (この場合は暗黙的なレシーバー) の一部として、com.android.launcher.action.INSTALL_SHORTCUT ブロードキャストはアプリに影響しなくなりました。これは、プライベートな暗黙的なブロードキャストになったためです。
Android O ActivityManagerService.java ごと:
case "com.android.launcher.action.INSTALL_SHORTCUT":
// As of O, we no longer support this broadcasts, even for pre-O apps.
// Apps should now be using ShortcutManager.pinRequestShortcut().
Log.w(TAG, "Broadcast " + action
+ " no longer supported. It will not be delivered.");
これが役立つことを願っています!
最初のステップとして、ランチャーがブロードキャストを受信できるようにする必要があります。
<!-- Intent received used to install shortcuts from other applications -->
<receiver
android:name="com.android.launcher2.InstallShortcutReceiver"
android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
<intent-filter>
<action android:name="com.android.launcher.action.INSTALL_SHORTCUT"/>
</intent-filter>
</receiver>
次に、manifest.xml にパーミッションを追加します。
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
最後に、関数を作成し、ボタンをクリックしたときに呼び出します。
public void createShortCut(){
// a Intent to create a shortCut
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//repeat to create is forbidden
shortcutintent.putExtra("duplicate", false);
//set the name of shortCut
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));
//set icon
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
//set the application to lunch when you click the icon
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext() , EnterActivity.class));
//sendBroadcast,done
sendBroadcast(shortcutintent);
}
次のようにします。
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
createShortCut();
}
});
まだこれを実行しようとしている人のために、Android O 以降で動作するように更新されたコードを次に示します。
ShortcutManager shortcutManager = (ShortcutManager) getSystemService(Context.SHORTCUT_SERVICE);
if (shortcutManager.isRequestPinShortcutSupported()) {
ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(getApplicationContext(), "1")
.setIntent(new Intent(getApplicationContext(), DestionationAcitivity.class).setAction(Intent.ACTION_MAIN))
.setShortLabel("My Shortcut")
.setIcon(Icon.createWithResource(this, R.drawable.yourDrawable))
.build();
shortcutManager.requestPinShortcut( shortcutInfo, null);
} else {
Toast.makeText(MainActivity.this,"Creating Shortcuts is not Supported on this Launcher",Toast.LENGTH_SHORT).show();
}