安卓桌面快捷方式

写一个简单的给安卓桌面增加快捷方式的例子,结果网上查询到的代码无论如何都无法实现。经过调查,发现是安卓从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

猜你喜欢

转载自blog.csdn.net/wjyyawjx/article/details/79975159