美文网首页
Android生成桌面快捷方式

Android生成桌面快捷方式

作者: 小李同学今天博学了吗 | 来源:发表于2019-08-15 19:43 被阅读0次

    现在好多手机都不是点击更多展开应用菜单,但今天自己学习了就把它记录下来,其中Google的原生系统还是有必要的

    图为api29上的快捷方式

    代码很少,主要针对Android 8.0及以上

    // 1.得到快捷方式的管理者

    ShortcutManager shortcutManager = (ShortcutManager) getSystemService(Context.SHORTCUT_SERVICE);

    // 2.构建点击快捷方式跳转的意图

    Intent intent =new Intent(this,MainActivity.class);

    intent.setAction(Intent.ACTION_VIEW);

    //3.创建快捷方式的信息,必须为其指定一个 id

    ShortcutInfo shortcutInfo =new ShortcutInfo.Builder(this,"the only id")

    //快捷方式的图标

            .setIcon(Icon.createWithResource(this,R.drawable.ic_launcher_background))

    // 名称

            .setShortLabel("哈哈哈")

    // 设置其意图

            .setIntent(intent)

    .build();

    //3.创建生成快捷方式的意图,第二个参数为请求码,第三个参数代表立即执行

    PendingIntent shortcutIntent = PendingIntent.getBroadcast(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

    //4. 添加权限检查,请求创建快捷方式

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

    shortcutManager.requestPinShortcut(shortcutInfo,shortcutIntent.getIntentSender());

    }

    相关文章

      网友评论

          本文标题:Android生成桌面快捷方式

          本文链接:https://www.haomeiwen.com/subject/rvstsctx.html