ユーザーが他のアプリをインストールするのに役立つAndroidアプリがあるとします。ホーム画面にこのアプリのショートカットを作成する方法はありますか? これらのショートカットの位置も指定できますか?
4994 次
3 に答える
1
これを試して :
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("com.whatsapp"));
sendBroadcast(shortcutintent);
}
于 2013-11-14T12:47:01.280 に答える
-1
少し改善されたバージョン: ショートカットが既に作成されているかどうかを確認しており、ユーザーが画面から削除した場合は作成してはなりません
final static private String PREF_KEY_SHORTCUT_ADDED = "PREF_KEY_SHORTCUT_ADDED";
@Override
public void onStart(){
super.onStart();
// Checking if ShortCut was already added
sharedPreferences = getPreferences(MODE_PRIVATE);
boolean shortCutWasAlreadyAdded = sharedPreferences.getBoolean(PREF_KEY_SHORTCUT_ADDED, false);
if (!shortCutWasAlreadyAdded) createShortcutIcon();
} // end onStart
// Creates shortcut on Android widget screen
private void createShortcutIcon(){
Intent shortcutIntent = new Intent(getApplicationContext(), Splash.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(),R.drawable.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
// Remembering that Shortcut was already added
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(PREF_KEY_SHORTCUT_ADDED, true);
editor.commit();
objPublicDelegate.showToast(getString(R.string.app_name)+ " shortcut created on screen.");
} // end createShortcutIcon
于 2014-10-02T13:20:21.353 に答える
-2
Android にはそのような API はありません。
于 2013-11-14T12:44:01.060 に答える