android桌面快捷方式

写一个简单的给安卓桌面增加快捷方式的例子,结果网上查询到的代码无论如何都无法实现。经过调查,发现是安卓从7.1开始引入shotcut 

如果你用的androidsdk25那么要使用下面的方式创建快捷方式

 

//androidsdk25开始 也就是安卓7开始 采用统一的方式管理桌面快捷方式
ShortcutManager shortcutManager = (ShortcutManager) getApplicationContext().getSystemService( Context.SHORTCUT_SERVICE);

if (shortcutManager.isRequestPinShortcutSupported()) {
    Intent shortcutInfoIntent = new Intent(getApplicationContext(), MainActivity.class);
    shortcutInfoIntent.setAction(Intent.ACTION_VIEW); //这里要注意的是android:action一定要配置, 否则会崩溃

    ShortcutInfo info = new ShortcutInfo.Builder(getApplicationContext(), "The only id")
            .setIcon( Icon.createWithResource(getApplicationContext(), R.drawable.heart))
            .setShortLabel("Short Label")
            .setIntent(shortcutInfoIntent)
            .build();

    //当添加快捷方式的确认弹框弹出来时,将被回调
    PendingIntent shortcutCallbackIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(getApplicationContext(), PermissionActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

    shortcutManager.requestPinShortcut(info, shortcutCallbackIntent.getIntentSender());
}
 

 

 

关于shortcut相关相信信息 发现一篇已经写的比较全的文章可以参考。

https://blog.csdn.net/mqcLuck/article/details/53586117

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值