下面列出了android.content.pm.ShortcutManager#isRequestPinShortcutSupported() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void pinAppShortcut(Context context) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ShortcutManager mShortcutManager = context.getSystemService(ShortcutManager.class);
if(mShortcutManager.isRequestPinShortcutSupported()) {
ShortcutInfo pinShortcutInfo = new ShortcutInfo.Builder(context, "freeform_mode").build();
mShortcutManager.requestPinShortcut(pinShortcutInfo, null);
} else
showToastLong(context, R.string.tb_pin_shortcut_not_supported);
} else {
Intent intent = ShortcutUtils.getShortcutIntent(context);
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
intent.putExtra("duplicate", false);
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
ResolveInfo defaultLauncher = context.getPackageManager().resolveActivity(homeIntent, PackageManager.MATCH_DEFAULT_ONLY);
intent.setPackage(defaultLauncher.activityInfo.packageName);
context.sendBroadcast(intent);
showToast(context, R.string.tb_shortcut_created);
}
}
public static void createLauncherShortcut(Context context, Intent launchIntent, Icon icon, String id, String label) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
if (shortcutManager.isRequestPinShortcutSupported()) {
ShortcutInfo info = new ShortcutInfo.Builder(context, id)
.setIntent(launchIntent)
.setIcon(icon)
.setShortLabel(label)
.setLongLabel(label)
.build();
Intent addIntent = shortcutManager.createShortcutResultIntent(info);
shortcutManager.requestPinShortcut(info,
PendingIntent.getBroadcast(context, 0, addIntent, 0).getIntentSender());
} else {
// TODO: Maybe implement this for launchers without pin shortcut support?
// TODO: Should be the same with the fallback for Android < O
// for now just show unsupported
Toast.makeText(context, context.getString(R.string.unsupported_launcher), Toast.LENGTH_LONG).show();
}
} else {
Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, drawableToBitmap(icon.loadDrawable(context)));
context.sendBroadcast(shortcutIntent);
Toast.makeText(context, R.string.shortcut_create_success, Toast.LENGTH_SHORT).show();
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
public static void testShortCut(Context context) {
//(若考虑兼容性可用ShortcutManagerCompat)
ShortcutManager shortcutManager = (ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
boolean requestPinShortcutSupported = shortcutManager.isRequestPinShortcutSupported();
Log.d("realmo", "启动器是否支持固定快捷方式: "+requestPinShortcutSupported);
if (requestPinShortcutSupported) {
Intent shortcutInfoIntent = new Intent(context, ShortCutActvivity.class);
shortcutInfoIntent.setAction(Intent.ACTION_VIEW);
ShortcutInfo info = new ShortcutInfo.Builder(context, "tzw")
.setIcon(Icon.createWithResource(context, R.mipmap.momo))
.setShortLabel("O系统短")
.setLongLabel("O系统长")
.setIntent(shortcutInfoIntent)
.build();
//当添加快捷方式的确认弹框弹出来时,将被回调CallBackReceiver里面的onReceive方法
PendingIntent shortcutCallbackIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, CallBackReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
shortcutManager.requestPinShortcut(info, shortcutCallbackIntent.getIntentSender());
}
}
@TargetApi(26)
private static void doCreateShortcut(Context context, String appName, Drawable draw, Intent intent) {
ShortcutManager shortcutManager = Objects.requireNonNull(context.getSystemService(ShortcutManager.class));
if (shortcutManager.isRequestPinShortcutSupported()) {
Bitmap bitmap = getBitmapFromDrawable(draw);
intent.setAction(Intent.ACTION_CREATE_SHORTCUT);
ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(context, appName)
.setShortLabel(appName)
.setLongLabel(appName)
.setIcon(Icon.createWithBitmap(bitmap))
.setIntent(intent)
.build();
shortcutManager.requestPinShortcut(shortcutInfo, null);
} else {
new AlertDialog.Builder(context)
.setTitle(context.getText(R.string.error_creating_shortcut))
.setMessage(context.getText(R.string.error_verbose_pin_shortcut))
.setPositiveButton(context.getText(android.R.string.ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Just close dialog don't do anything
dialog.cancel();
}
})
.show();
}
}
static void requestCreateShortCut(String title, Intent intent, Drawable icon, String id, Context context, Bitmap bm) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
requestCreateShortCutOldApi(title, intent, icon, context, bm);
} else {
ShortcutManager mShortcutManager =
context.getSystemService(ShortcutManager.class);
if (mShortcutManager != null) {
if (mShortcutManager.isRequestPinShortcutSupported()) {
ShortcutInfo.Builder shortcutInfoBuilder =
new ShortcutInfo.Builder(context, id);
shortcutInfoBuilder.setIcon(
Icon.createWithBitmap(bm == null ? getBitmapFromDrawable(icon) : bm));
shortcutInfoBuilder.setIntent(intent);
shortcutInfoBuilder.setShortLabel(title);
shortcutInfoBuilder.setLongLabel(title);
ShortcutInfo pinShortcutInfo = shortcutInfoBuilder.build();
// Create the PendingIntent object only if your app needs to be notified
// that the user allowed the shortcut to be pinned. Note that, if the
// pinning operation fails, your app isn't notified. We assume here that the
// app has implemented a method called createShortcutResultIntent() that
// returns a broadcast intent.
Intent pinnedShortcutCallbackIntent =
mShortcutManager.createShortcutResultIntent(pinShortcutInfo);
// Configure the intent so that your app's broadcast receiver gets
// the callback successfully.
PendingIntent successCallback = PendingIntent.getBroadcast(context, id.hashCode(),
pinnedShortcutCallbackIntent, 0);
mShortcutManager.requestPinShortcut(pinShortcutInfo,
successCallback.getIntentSender());
showToast(context, R.string.requested);
} else {
requestCreateShortCutOldApi(title, intent, icon, context, bm);
}
} else {
requestCreateShortCutOldApi(title, intent, icon, context, bm);
}
}
}
public static boolean isPinnedShortcutSupported(Activity activity) {
ShortcutManager shortcutManager = activity.getSystemService(ShortcutManager.class);
return shortcutManager.isRequestPinShortcutSupported();
}
/**
* Main-Menu-Handler
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_cancel:
if (originalNote == null) {
db.deleteNoteAndSync(ssoAccount, note.getId());
} else {
db.updateNoteAndSync(ssoAccount, localAccount, originalNote, null, null);
}
listener.close();
return true;
case R.id.menu_delete:
db.deleteNoteAndSync(ssoAccount, note.getId());
listener.close();
return true;
case R.id.menu_favorite:
db.toggleFavorite(ssoAccount, note, null);
listener.onNoteUpdated(note);
prepareFavoriteOption(item);
return true;
case R.id.menu_category:
showCategorySelector();
return true;
case R.id.menu_title:
showEditTitleDialog();
return true;
case R.id.menu_move:
AccountPickerDialogFragment.newInstance(note.getAccountId()).show(requireActivity().getSupportFragmentManager(), BaseNoteFragment.class.getSimpleName());
return true;
case R.id.menu_share:
ShareUtil.openShareDialog(requireContext(), note.getTitle(), note.getContent());
return false;
case MENU_ID_PIN:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ShortcutManager shortcutManager = requireActivity().getSystemService(ShortcutManager.class);
if (shortcutManager != null) {
if (shortcutManager.isRequestPinShortcutSupported()) {
Intent intent = new Intent(getActivity(), EditNoteActivity.class);
intent.putExtra(EditNoteActivity.PARAM_NOTE_ID, note.getId());
intent.setAction(ACTION_SHORTCUT);
ShortcutInfo pinShortcutInfo = new ShortcutInfo.Builder(getActivity(), note.getId() + "")
.setShortLabel(note.getTitle())
.setIcon(Icon.createWithResource(requireActivity().getApplicationContext(), note.isFavorite() ? R.drawable.ic_star_yellow_24dp : R.drawable.ic_star_grey_ccc_24dp))
.setIntent(intent)
.build();
Intent pinnedShortcutCallbackIntent =
shortcutManager.createShortcutResultIntent(pinShortcutInfo);
PendingIntent successCallback = PendingIntent.getBroadcast(getActivity(), /* request code */ 0,
pinnedShortcutCallbackIntent, /* flags */ 0);
shortcutManager.requestPinShortcut(pinShortcutInfo,
successCallback.getIntentSender());
} else {
Log.i(TAG, "RequestPinShortcut is not supported");
}
} else {
Log.e(TAG, "ShortcutManager is null");
}
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}