下面列出了android.content.pm.ShortcutManager#createShortcutResultIntent() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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();
}
}
@NonNull
public Intent createShortcut(Contact contact, boolean legacy) {
Intent intent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !legacy) {
ShortcutInfo shortcut = getShortcutInfo(contact);
ShortcutManager shortcutManager = xmppConnectionService.getSystemService(ShortcutManager.class);
intent = shortcutManager.createShortcutResultIntent(shortcut);
} else {
intent = createShortcutResultIntent(contact);
}
return intent;
}
@NonNull
public Intent createShortcut(Contact contact, boolean legacy) {
Intent intent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !legacy) {
ShortcutInfo shortcut = getShortcutInfo(contact);
ShortcutManager shortcutManager = xmppConnectionService.getSystemService(ShortcutManager.class);
intent = shortcutManager.createShortcutResultIntent(shortcut);
} else {
intent = createShortcutResultIntent(contact);
}
return intent;
}
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);
}
}
}
/**
* 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);
}
}