![](/img/trans.png)
[英]Samsung A10 android 11 how to create other apps pinned shortcut programmatically from my app
[英]How to programmatically create a shortcut of another app in Android?
假设我有一些Android应用程序,可以帮助用户安装其他一些应用程序。 有什么方法可以在主屏幕上创建此应用的快捷方式吗? 我还可以指定这些快捷方式的位置吗?
尝试这个 :
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);
}
改进了一点:我们正在检查快捷方式是否已经创建,如果用户将其从屏幕上删除,则不能创建该快捷方式
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
Android上没有此类API。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.