正如您所知道的那样,当应用程序被正常安装时,图标会在启动器菜单屏幕上创建。
我想做的是在安装过程中在用户主屏幕上创建图标。 (无需按下图标5秒钟)
我从另一个来源听说只需添加以下内容即可:
<category android:value="android.intent.category.HOME" />
我将权限添加到AndroidManifest.xml文件中,但没有生效。
是否还有其他方法可行?
正如您所知道的那样,当应用程序被正常安装时,图标会在启动器菜单屏幕上创建。
我想做的是在安装过程中在用户主屏幕上创建图标。 (无需按下图标5秒钟)
我从另一个来源听说只需添加以下内容即可:
<category android:value="android.intent.category.HOME" />
我将权限添加到AndroidManifest.xml文件中,但没有生效。
是否还有其他方法可行?
Intent shortcutIntent = new Intent();
shortcutIntent.setClassName("packageName", "className");
//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, "shortcut_name");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(context, R.drawable.icon));
//intent.putExtra("duplicate", false);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
context.sendBroadcast(addIntent);
你需要在AndroidManifest.xml中使用以下权限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
您可以根据自己的需求使用注释代码。
请注意,上述API可能没有文档记录。但它是有效的。
我使用以下方法来正确添加或删除快捷方式。这些方法非常有效,并且与用户手动添加/删除快捷方式时的安卓系统相同。
public static void addShortcut(Context context)
{
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
ApplicationInfo appInfo = context.getApplicationInfo();
// Shortcut name
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, appInfo.name);
shortcut.putExtra("duplicate", false); // Just create once
// Setup activity shoud be shortcut object
ComponentName component = new ComponentName(appInfo.packageName, appInfo.className);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(component));
// Set shortcut icon
ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(context, appInfo.icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
context.sendBroadcast(shortcut);
}
public static void deleteShortcut(Context context)
{
Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
ApplicationInfo appInfo = context.getApplicationInfo();
// Shortcut name
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, appInfo.name);
ComponentName comp = new ComponentName(appInfo.packageName, appInfo.className);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
context.sendBroadcast(shortcut);
}
权限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
<26
或>26
)。createShortcutOnHome(CurrentActivity.this, ActivityToOpen.class, "app name", R.mipmap.ic_launcher);
这个方法可以放在您的工具类中。
public static void createShortcutOnHome(@NonNull Activity activity, Class activityToOpen, String title, @DrawableRes int icon) {
Intent shortcutIntent = new Intent(activity, activityToOpen);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { // code for adding shortcut on pre oreo device
Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
intent.putExtra("duplicate", false);
Parcelable parcelable = Intent.ShortcutIconResource.fromContext(activity, icon);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, parcelable);
activity.sendBroadcast(intent);
System.out.println("added_to_homescreen");
} else {
ShortcutManager shortcutManager = activity.getSystemService(ShortcutManager.class);
assert shortcutManager != null;
if (shortcutManager.isRequestPinShortcutSupported()) {
ShortcutInfo pinShortcutInfo =
new ShortcutInfo.Builder(activity, "browser-shortcut-")
.setIntent(shortcutIntent)
.setIcon(Icon.createWithResource(activity, icon))
.setShortLabel(title)
.build();
shortcutManager.requestPinShortcut(pinShortcutInfo, null);
System.out.println("added_to_homescreen");
} else {
System.out.println("failed_to_add");
}
}
}
shortcutIntent.setAction(Intent.ACTION_MAIN);
- sss我对以上答案感到困惑,因为API 22和其他API在debug / emulation模式下返回getApplicationInfo()。className:
"com.android.tools.fd.runtime.BootstrapApplication"
最重要的是,我需要为类名设置整个路径。 (请参见更改)
例如,我有以下内容:
packageName = com.example.user.myapp
className = MainActivity
在 Kailash答案中,我需要更改此行:
shortcutIntent.setClassName("packageName", "className");
to
shortcutIntent.setClassName("com.example.user.myapp", "com.example.user.myapp.MainActivity");
ComponentName comp = new ComponentName(appInfo.packageName, appInfo.className);
为了
ComponentName comp = new ComponentName(appInfo.packageName, appInfo.packageName+".MainActivity");
setup
函数,让用户可以使用它来执行所有可能的操作。例如,如果必要的话,创建任何必要的用户可访问文件夹(如果它们不存在),将快捷方式添加到主屏幕中,和/或加载初始数据。onCreate()
中排除,以便它不会每次都被调用!