当前位置:   article > 正文

Android创建快捷方式到桌面

Android创建快捷方式到桌面

效果图

参考

https://blog.51cto.com/u_16175498/8811197icon-default.png?t=N7T8https://blog.51cto.com/u_16175498/8811197

权限

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

实现

  1. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  2. ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
  3. if (shortcutManager.isRequestPinShortcutSupported()) {
  4. String title = list_webView.get(currentPage).getTitle();
  5. String url = list_webView.get(currentPage).getUrl();
  6. Bitmap bitmap = list_webView.get(currentPage).getFavicon();
  7. if (bitmap == null)
  8. bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
  9. Icon icon = Icon.createWithBitmap(bitmap);
  10. Intent shortcutIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
  11. ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(getApplicationContext(), url)
  12. .setShortLabel(title)
  13. .setLongLabel(title)
  14. .setIcon(icon)
  15. .setIntent(shortcutIntent)
  16. .build();
  17. Intent pinnedShortcutCallbackIntent = shortcutManager.createShortcutResultIntent(shortcutInfo);
  18. PendingIntent successCallback = PendingIntent.getBroadcast(getApplicationContext(), 0, pinnedShortcutCallbackIntent, 0);
  19. shortcutManager.requestPinShortcut(shortcutInfo, successCallback.getIntentSender());
  20. }
  21. } else {
  22. Toast.makeText(getApplicationContext(), "不支持创建快捷方式", Toast.LENGTH_SHORT).show();
  23. }

注意

1.Android O 以上才支持。

2.ShortcutInfo.Builder(getApplicationContext(), id),id不一样才会添加,我直接填网址了。

3.标题太长,添加之前需要编辑一下。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/552216
推荐阅读
相关标签
  

闽ICP备14008679号