Android应用桌面快捷方式创建
权限
//创建桌面快捷方式需要添加权限
com.android.launcher.permission.INSTALL_SHORTCUT
传统方式
private void creatShortcut(Context context) {
Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//不允许重复创建,
shortcutIntent.putExtra("duplicate",false);
//快捷方式名称
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,getString(R.string.app_name));
//应用图标 shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResoure.fromContext(context, R.mipmap.ic_launcher));
//指定快捷方式启动的activity
ComponentName componentName = new ComponentName(context, SplashActivity.class);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, componentName);
//发送广播
sendBroadcast(shortcutIntent);
}
但是这种传统的创建方式在android 8.0系统上是创建不成功的。
ShortcutManager
从Android 7.1(API25)开始,新增了ShortcutManager,可以长按桌面应用对弹出的快捷方式进行管理。但是Android7.1仍然是采用的传统方式创建的应用快捷方式,在Android O(API26)的时候,Google想通过统一的接口来管理,所以API 26上,ShortcutManager新增了对Pinned Shortcuts(固定快捷方式)的管理。
Apps can pin an existing shortcut (either static or dynamic) or an entirely new shortcut to a supported launcher programatically using requestPinShortcut(ShortcutInfo, IntentSender). You pass two arguments into this method:
A ShortcutInfo object – If the shortcut already exists, this object should contain only the shortcut’s ID. Otherwise, the new ShortcutInfo object must contain an ID, an intent, and a short label for the new shortcut.
A PendingIntent object – This intent represents the callback that your app receives if the shortcut is successfully pinned to the device’s launcher.
Note: If the user doesn’t allow the shortcut to be pinned to the launcher, the pinning process fails, and the Intent object that is passed into this PendingIntent object isn’t executed.
Note: Due to background execution limits introduced in Android O, it’s best to use a manifest-declared receiver to receive a callback. Also, to prevent other apps from invoking the receiver, add the attribute assignment android:exported=”false” to the receiver’s manifest entry.
Note: As you add logic in your app to make requests to pin shortcuts, keep in mind that not all launchers support pinning of shortcuts. To determine whether your app can complete this process on a particular device, check the return value of isRequestPinShortcutSupported(). Based on this return value, you might decide to hide the option in your app that allows users to pin a shortcut.
Note: See also the support library APIs isRequestPinShortcutSupported(Context) and requestPinShortcut(Context, ShortcutInfoCompat, IntentSender), which works on Android versions lower than O by falling back to the deprecated private intent com.android.launcher.action.INSTALL_SHORTCUT.
ShortcutManager类在API level 26上,增加了对isRequestPinShortcutSupported、requestPinShortcut、createShortcutResultIntent三个方法。说明如下:
1.isRequestPinShortcutSupported
Return TRUE if the app is running on a device whose default launcher supports requestPinShortcut(ShortcutInfo, IntentSender).
The return value may change in subsequent calls if the user changes the default launcher app.
Note: See also the support library counterpart isRequestPinShortcutSupported(Context), which supports Android versions lower than O using the legacy private intent com.android.launcher.action.INSTALL_SHORTCUT.
译: 如果默认桌面支持requestPinShortcut(ShortcutInfo,IntentSender)方法,则返回TRUE。
如果用户更改默认启动程序应用程序,返回值可能会在后续调用中更改。
注意:另请参见支持库对应的isRequestPinShortcutSupported(Context),在低于O的Android版本,它支持使用旧的私有意图com.android.launcher.action.INSTALL_SHORTCUT。
2.requestPinShortcut
Request to create a pinned shortcut. The default launcher will receive this request and ask the user for approval. If the user approves it, the shortcut will be created, and resultIntent will be sent. If a request is denied by the user, however, no response will be sent to the caller.
Only apps with a foreground activity or a foreground service can call this method. Otherwise, it’ll throw IllegalStateException.
It’s up to the launcher to decide how to handle previous pending requests when the same package calls this API multiple times in a row. One possible strategy is to ignore any previous requests.
Note: See also the support library counterpart requestPinShortcut(Context, ShortcutInfoCompat, IntentSender), which supports Android versions lower than O using the legacy private intent com.android.launcher.action.INSTALL_SHORTCUT.
译: 请求创建固定的快捷方式。默认启动器将收到该请求,并要求用户批准。如果用户批准,将创建快捷方式,并且将发送resultIntent。但是,如果请求被用户拒绝,则不会向呼叫者发送任何响应。
只有具有前台活动或前台服务的应用程序才能调用此方法。否则,它将抛出IllegalStateException。
当同一个软件包连续多次调用该API时,由开发人员决定如何处理以前的待处理请求。一个可能的策略是忽略任何先前的请求。
注意:另请参见支持库对应件requestPinShortcut(Context,ShortcutInfoCompat,IntentSender),在低于O的Android版本,它支持使用旧的私有意图com.android.launcher.action.INSTALL_SHORTCUT。
3.createShortcutResultIntent
Returns an Intent which can be used by the default launcher to pin a shortcut containing the given ShortcutInfo. This method should be used by an Activity to set a result in response to ACTION_CREATE_SHORTCUT.
Android 8.0及以上创建应用快捷方式
@RequiresApi(api = Build.VERSION_CODES.O)
public static void addShortCut(Context context) {
ShortcutManager shortcutManager = (ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
if(shortcutManager.isRequestPinShortcutSupported()) {
Intent shortcutInfoIntent = new Intent(context, SplashActivity.class);
shortcutInfoIntent.setAction(Intent.ACTION_VIEW);
ShortcutInfo info = new ShortcutInfo.Builder(context, "The only id")
.setIcon(Icon.createWithResources(context, R.mipmap.ic_shortcut))
.setShortLabel(getString(R.string.app_name))
.setIntent(shortcutInfoIntent);
.build();
//当添加快捷方式的确认弹框弹出来时,将被回调
PendingIntent shortcutCallbackIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, MyReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
shortcutManager.requestPinShortcut(info, shortcutCallbackIntent.getIntentSender());
}
}
回调
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive: ");
}
}
ShortcutManagerCompat
在以上三个方法官方介绍中,官方提示我们,可以使用Android support库的ShortcutManagerCompat进行快捷方式的版本兼容适配。
public static void addShortCutCompact(Context context) {
if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
Intent shortcutInfoIntent = new Intent(context, SplashActivity.class);
shortcutInfoIntent.setAction(Intent.ACTION_VIEW); //action必须设置,不然报错
ShortcutInfoCompat info = new ShortcutInfoCompat.Builder(context, "The only id")
.setIcon(R.mipmap.ic_shortcut)
.setShortLabel(getString(R.string.app_name))
.setIntent(shortcutInfoIntent)
.build();
//当添加快捷方式的确认弹框弹出来时,将被回调
PendingIntent shortcutCallbackIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, MyReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
ShortcutManagerCompat.requestPinShortcut(context, info, shortcutCallbackIntent.getIntentSender());
}
}