下面列出了android.content.pm.ShortcutManager#setDynamicShortcuts() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
private void listing19_28(String destination) {
// Listing 19-28: Creating and adding dynamic App Shortcuts
ShortcutManager shortcutManager = (ShortcutManager) getSystemService(Context.SHORTCUT_SERVICE);
Intent navIntent = new Intent(this, MainActivity.class);
navIntent.setAction(Intent.ACTION_VIEW);
navIntent.putExtra(DESTINATION_EXTRA, destination);
String id = "dynamicDest" + destination;
ShortcutInfo shortcut =
new ShortcutInfo.Builder(this, id)
.setShortLabel(destination)
.setLongLabel("Navigate to " + destination)
.setDisabledMessage("Navigation Shortcut Disabled")
.setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
.setIntent(navIntent)
.build();
shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));
}
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
private void initAppShortcuts() {
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
if (shortcutManager == null) {
return;
}
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("action", "scan");
intent.setAction(Intent.ACTION_MAIN);
ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "shortcut_new")
.setShortLabel(getString(R.string.new_entry))
.setLongLabel(getString(R.string.add_new_entry))
.setIcon(Icon.createWithResource(this, R.drawable.ic_qr_code))
.setIntent(intent)
.build();
shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut));
}
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
private static void updataShortcuts(Context context, List<AppInfo> items) {
ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
List<ShortcutInfo> shortcutInfoList = new ArrayList<>();
int max = shortcutManager.getMaxShortcutCountPerActivity();
for (int i = 0; i < max && i < items.size(); i++) {
AppInfo appInfo = items.get(i);
ShortcutInfo.Builder shortcut = new ShortcutInfo.Builder(context, appInfo.packageName);
shortcut.setShortLabel(appInfo.appName);
shortcut.setLongLabel(appInfo.appName);
shortcut.setIcon(
Icon.createWithBitmap(drawableToBitmap(LocalImageLoader.getDrawable(context, appInfo))));
Intent intent = new Intent(context, AppPermissionActivity.class);
intent.putExtra(AppPermissionActivity.EXTRA_APP_PKGNAME, appInfo.packageName);
intent.putExtra(AppPermissionActivity.EXTRA_APP_NAME, appInfo.appName);
intent.setAction(Intent.ACTION_DEFAULT);
shortcut.setIntent(intent);
shortcutInfoList.add(shortcut.build());
}
shortcutManager.setDynamicShortcuts(shortcutInfoList);
}
public static void setShortcuts(Activity activity, SoundProfile[] soundProfiles) {
final List<ShortcutInfo> shortcutInfos = new ArrayList<>();
for (SoundProfile soundProfile : soundProfiles) {
if (!soundProfile.name.isEmpty()) {
shortcutInfos.add(createShortcutInfo(activity, soundProfile));
}
}
ShortcutManager shortcutManager = activity.getSystemService(ShortcutManager.class);
if (shortcutManager.getMaxShortcutCountPerActivity() < shortcutInfos.size()) {
int last = shortcutInfos.size() - 1;
int first = last - shortcutManager.getMaxShortcutCountPerActivity();
shortcutManager.setDynamicShortcuts(shortcutInfos.subList(first, last));
} else {
shortcutManager.setDynamicShortcuts(shortcutInfos);
}
}
private void setLauncherShortcuts() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
if(shortcutManager.getDynamicShortcuts().size() == 0) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName(BuildConfig.APPLICATION_ID, TaskerQuickActionsActivity.class.getName());
intent.putExtra("launched-from-app", true);
ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "quick_actions")
.setShortLabel(getString(R.string.label_quick_actions))
.setIcon(Icon.createWithResource(this, R.drawable.shortcut_icon))
.setIntent(intent)
.build();
shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut));
}
}
}
private void createShortcuts() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
if (shortcutManager != null) {
shortcutManager.setDynamicShortcuts(
Arrays.asList(
getShortcutForTextType(),
getShortcutForFileType()
)
);
}
}
}
public static void setApplicationShortcuts(Context context) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
SQRLStorage sqrlStorage = SQRLStorage.getInstance(context);
if (getCurrentId(context) > 0) {
ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
if (sqrlStorage.hasQuickPass()) {
shortcutManager.setDynamicShortcuts(Arrays.asList(scanShortcut, clearQuickPassShortcut));
} else {
shortcutManager.setDynamicShortcuts(Arrays.asList(scanShortcut, logonShortcut));
}
}
}
}
private void saveBookmarks() {
SharedPreferences.Editor editor = prefs.edit();
for (int i = 0; i < bookmarks.size(); i++) {
editor.putInt(PREF_BOOKMARK + i, bookmarks.get(i));
}
editor.putInt(PREF_BOOKMARKS_LENGTH, bookmarks.size());
editor.apply();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
Collections.sort(bookmarks);
ShortcutManager manager = (ShortcutManager) getSystemService(Context.SHORTCUT_SERVICE);
if (manager != null) {
List<ShortcutInfo> shortcuts = new ArrayList<>();
for (int bpm : bookmarks) {
shortcuts.add(
new ShortcutInfo.Builder(this, String.valueOf(bpm))
.setShortLabel(getString(R.string.bpm, String.valueOf(bpm)))
.setIcon(Icon.createWithResource(this, R.drawable.ic_note))
.setIntent(getBookmarkIntent(bpm))
.build()
);
}
manager.setDynamicShortcuts(shortcuts);
}
}
updateBookmarks(true);
}
private void setShortcuts() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) return;
PriorityQueue<Class<? extends Fragment>> queue = new PriorityQueue<>(
(o1, o2) -> {
int opened1 = AppSettings.getFragmentOpened(o1, this);
int opened2 = AppSettings.getFragmentOpened(o2, this);
return opened2 - opened1;
});
for (Map.Entry<Integer, Class<? extends Fragment>> entry : mActualFragments.entrySet()) {
Class<? extends Fragment> fragmentClass = entry.getValue();
if (fragmentClass == null || fragmentClass == SettingsFragment.class) continue;
queue.offer(fragmentClass);
}
List<ShortcutInfo> shortcutInfos = new ArrayList<>();
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
shortcutManager.removeAllDynamicShortcuts();
for (int i = 0; i < 4; i++) {
NavigationFragment fragment = findNavigationFragmentByClass(queue.poll());
if (fragment == null || fragment.mFragmentClass == null) continue;
Intent intent = new Intent(this, MainActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra(INTENT_SECTION, fragment.mFragmentClass.getCanonicalName());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
ShortcutInfo shortcut = new ShortcutInfo.Builder(this,
fragment.mFragmentClass.getSimpleName())
.setShortLabel(getString(fragment.mId))
.setLongLabel(Utils.strFormat(getString(R.string.open), getString(fragment.mId)))
.setIcon(Icon.createWithResource(this, fragment.mDrawable == 0 ?
R.drawable.ic_blank : fragment.mDrawable))
.setIntent(intent)
.build();
shortcutInfos.add(shortcut);
}
shortcutManager.setDynamicShortcuts(shortcutInfos);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
shortcutManager.removeAllDynamicShortcuts();
int newWallId = ShortcutsUtils.getNextRailNumber(shortcutManager);
ShortcutInfo ballShortcut = ShortcutsUtils.createTrainShortcut(this);
ShortcutInfo wallShortcut = ShortcutsUtils.createRailShortcut(this, newWallId);
shortcutManager.setDynamicShortcuts(Arrays.asList(ballShortcut, wallShortcut));
setContentView(R.layout.activity_main);
rootView = findViewById(R.id.activity_main_root);
viewPager = (ViewPager) findViewById(R.id.activity_main_view_pager);
pagerAdapter = new TutorialViewPagerAdapter(getFragmentManager());
viewPager.setAdapter(pagerAdapter);
rootView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finishActivity();
}
});
}
private void setDynamicShortcuts(
JSONArray args) throws PackageManager.NameNotFoundException, JSONException {
int count = args.length();
ArrayList<ShortcutInfo> shortcuts = new ArrayList<ShortcutInfo>(count);
for (int i = 0; i < count; ++i) {
shortcuts.add(buildDynamicShortcut(args.optJSONObject(i)));
}
Context context = this.cordova.getActivity().getApplicationContext();
ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
shortcutManager.setDynamicShortcuts(shortcuts);
Log.i(TAG, String.format("Saved % dynamic shortcuts.", count));
}
private void setShortcut() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
ShortcutManager shortcutManager = mContext.getSystemService(ShortcutManager.class);
List<ShortcutInfo> list = new ArrayList<>();
ShortcutInfo shortGit;
ShortcutInfo shortBlog;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.jianshu.com/u/a229eee96115"));
intent.setClassName(getPackageName(), MainActivity.class.getName());
intent.putExtra("url", "https://www.jianshu.com/u/a229eee96115");
Intent intentGit = new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/AriesHoo"));
intentGit.setClassName(getPackageName(), MainActivity.class.getName());
intentGit.putExtra("url", "https://github.com/AriesHoo");
shortGit = new ShortcutInfo.Builder(this, "github")
.setShortLabel("GitHub")
.setLongLabel("GitHub-AriesHoo")
.setIcon(Icon.createWithResource(mContext, R.drawable.ic_github))
.setIntent(intentGit)
.build();
shortBlog = new ShortcutInfo.Builder(this, "jianshu")
.setShortLabel("简书")
.setLongLabel("简书-AriesHoo")
.setIcon(Icon.createWithResource(mContext, R.drawable.ic_book))
.setIntent(intent)
.build();
list.add(shortGit);
list.add(shortBlog);
shortcutManager.setDynamicShortcuts(list);
}
}
/**
* This method is for getting the short cut if we are
* holding the icon for long time.
*/
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
void dynamicShortcut() {
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
ShortcutInfo webShortcut = new ShortcutInfo.Builder(this, "Web app")
.setShortLabel("Web App")
.setLongLabel("Open the web app")
.setIcon(Icon.createWithResource(this, R.drawable.add))
.setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(editTextCompanyURL.getText().toString())))
.build();
shortcutManager.setDynamicShortcuts(Collections.singletonList(webShortcut));
}
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
private void createShortCut() {
//获取系统服务得到ShortcutManager对象 (若考虑兼容性可用ShortcutManagerCompat)
ShortcutManager systemService = getSystemService(ShortcutManager.class);
if (Build.VERSION.SDK_INT >= 25) {
//设置Intent跳转逻辑
Intent intent = new Intent(ShortCut7_0Activity.this, ShortCutActvivity.class);
intent.setAction(Intent.ACTION_VIEW);
//设置ID
ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(this, "onlyId")
//设置短标题
.setShortLabel("雅蠛蝶")
//设置长标题
.setLongLabel("江山留胜迹")
//设置icon
.setIcon(Icon.createWithResource(this, R.mipmap.momo))
//设置Intent
.setIntent(intent)
.build();
//这样就可以通过长按图标显示出快捷方式了
systemService.setDynamicShortcuts(Arrays.asList(shortcutInfo));
}
}
public static void buildShortcuts(Context context) {
// TODO: Loop across all activities
List<ShortcutInfo> shortcuts = new ArrayList<>();
shortcuts.add(buildShortcut(context, Site.NHENTAI));
ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
if (shortcutManager != null)
shortcutManager.setDynamicShortcuts(shortcuts);
}
private void setShortcuts() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) return;
PriorityQueue<Class<? extends Fragment>> queue = new PriorityQueue<>(
(o1, o2) -> {
int opened1 = AppSettings.getFragmentOpened(o1, this);
int opened2 = AppSettings.getFragmentOpened(o2, this);
return opened2 - opened1;
});
for (Map.Entry<Integer, Class<? extends Fragment>> entry : mActualFragments.entrySet()) {
Class<? extends Fragment> fragmentClass = entry.getValue();
if (fragmentClass == null || fragmentClass == SettingsFragment.class) continue;
queue.offer(fragmentClass);
}
List<ShortcutInfo> shortcutInfos = new ArrayList<>();
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
shortcutManager.removeAllDynamicShortcuts();
for (int i = 0; i < 4; i++) {
NavigationFragment fragment = findNavigationFragmentByClass(queue.poll());
if (fragment == null || fragment.mFragmentClass == null) continue;
Intent intent = new Intent(this, MainActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra(INTENT_SECTION, fragment.mFragmentClass.getCanonicalName());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
ShortcutInfo shortcut = new ShortcutInfo.Builder(this,
fragment.mFragmentClass.getSimpleName())
.setShortLabel(getString(fragment.mId))
.setLongLabel(Utils.strFormat(getString(R.string.open), getString(fragment.mId)))
.setIcon(Icon.createWithResource(this, fragment.mDrawable == 0 ?
R.drawable.ic_blank : fragment.mDrawable))
.setIntent(intent)
.build();
shortcutInfos.add(shortcut);
}
shortcutManager.setDynamicShortcuts(shortcutInfos);
}
public static void populateAppShortcuts(Context context, List<RemoteItem> remotes) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N_MR1) {
return;
}
ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
if (shortcutManager == null) {
return;
}
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
Set<String> shortcutSet = new HashSet<>();
List<ShortcutInfo> shortcutInfoList = new ArrayList<>();
for (RemoteItem remoteItem : remotes) {
String id = getUniqueIdFromString(remoteItem.getName());
Intent intent = new Intent(Intent.ACTION_MAIN, Uri.EMPTY, context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra(APP_SHORTCUT_REMOTE_NAME, remoteItem.getName());
ShortcutInfo shortcut = new ShortcutInfo.Builder(context, id)
.setShortLabel(remoteItem.getName())
.setIcon(Icon.createWithResource(context, AppShortcutsHelper.getRemoteIcon(remoteItem.getType(), remoteItem.isCrypt())))
.setIntent(intent)
.build();
shortcutInfoList.add(shortcut);
shortcutSet.add(id);
if (shortcutInfoList.size() >= 4) {
break;
}
}
shortcutManager.setDynamicShortcuts(shortcutInfoList);
editor.putStringSet(context.getString(R.string.shared_preferences_app_shortcuts), shortcutSet);
editor.apply();
}
private void updateDynamicShortcuts() {
ShortcutManager shortcutManager;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
Intent intentGenerateQrCode = new Intent(this, MainActivity.class);
intentGenerateQrCode.setFlags((Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK));
intentGenerateQrCode.setAction(Constants.ACTION_GENERATE_QR_CODE);
ShortcutInfo shortcutGenerateQrCode = new ShortcutInfo.Builder(this, SHORTCUT_ID_GENERATE_QR_CODE)
.setShortLabel(getString(R.string.shortcut_generate_qr_code))
.setLongLabel(getString(R.string.shortcut_generate_qr_code))
.setIcon(Icon.createWithResource(this, R.drawable.ic_shortcut_qr))
.setIntent(intentGenerateQrCode)
.build();
Intent intentTransferIotas = new Intent(this, MainActivity.class);
intentTransferIotas.setFlags((Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK));
intentTransferIotas.setAction(Constants.ACTION_SEND_TRANSFER);
ShortcutInfo shortcutTransferIotas = new ShortcutInfo.Builder(this, SHORTCUT_ID_SEND_TRANSFER)
.setShortLabel(getString(R.string.shortcut_send_transfer))
.setLongLabel(getString(R.string.shortcut_send_transfer))
.setIcon(Icon.createWithResource(this, R.drawable.ic_shortcut_transaction))
.setIntent(intentTransferIotas)
.build();
shortcutManager = getSystemService(ShortcutManager.class);
if (shortcutManager != null) {
if (IOTA.seed != null) {
shortcutManager.setDynamicShortcuts(Arrays.asList(shortcutGenerateQrCode, shortcutTransferIotas));
shortcutManager.enableShortcuts(Arrays.asList(SHORTCUT_ID_GENERATE_QR_CODE, SHORTCUT_ID_SEND_TRANSFER));
} else {
// remove shortcuts if Iota.seed.isEmpty()
shortcutManager.disableShortcuts(Arrays.asList(SHORTCUT_ID_GENERATE_QR_CODE, SHORTCUT_ID_SEND_TRANSFER));
shortcutManager.removeAllDynamicShortcuts();
}
}
}
}
private void createSimpleDynamicShortcut() {
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
Intent intent1 = new Intent(getApplicationContext(), MainActivity.class);
intent1.setAction(ACTION_KEY);
ShortcutInfo shortcut1 = new ShortcutInfo.Builder(this, "dShortcut1")
.setIntent(intent1)
.setRank(1)
.setLongLabel("Dynamic Shortcut 1")
.setShortLabel("This is the shortcut 1")
.setIcon(Icon.createWithResource(this, R.drawable.ic_home_black_24dp))
.build();
ShortcutInfo shortcut2 = new ShortcutInfo.Builder(this, "web_link")
.setRank(0)
.setShortLabel("Journaldev.com")
.setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.journaldev.com")))
.build();
shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut1, shortcut2));
//shortcutManager.disableShortcuts(Arrays.asList(shortcut1.getId()));
}