zoukankan      html  css  js  c++  java
  • Android开发之创建桌面快捷方式

    Android创建桌面快捷方式就是在桌面这个应用上创建一个快捷方式,桌面应用:launcher2

    通过查看launcher2的清单文件:

    1         <!-- Intent received used to install shortcuts from other applications -->
    2         <receiver
    3             android:name="com.android.launcher2.InstallShortcutReceiver"
    4             android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
    5             <intent-filter>
    6                 <action android:name="com.android.launcher.action.INSTALL_SHORTCUT" />
    7             </intent-filter>
    8         </receiver>

    可以看出来,创建桌面快捷方式,是通过Broadcast。

    那么想要创建一个快捷方式的话,就发送这个广播就可以了。

    同时注意,清单文件中写的很清楚,需要权限:com.android.launcher.permission.INSTALL_SHORTCUT,所以需要在清单文件中添加权限。

    同时需要在添加快捷方式的名称,图标和该快捷方式干什么事情(一个含有action等信息的intent)

    携带的信息:使用intent.pueExtra()

    名称:Intent.EXTRA_SHORTCUT_NAME

    图标:Intent.EXTRA-SHORTCUT_ICON

    意图:Intent.EXTRA_SHORTCUT_INTENT

    创建桌面快捷方式的代码:

     1         Intent intent = new Intent();
     2         intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
     3         intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "桌面快捷方式");
     4         intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
     5         Intent shortIntent = new Intent();
     6         shortIntent.setAction("android.intent.action.MAIN");
     7         shortIntent.addCategory("android.intent.category.LAUNCHER");
     8         shortIntent.setClassName(getPackageName(), "com.xxx.mobile.activity.WelcomeActivity");
     9         intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortIntent);
    10         sendBroadcast(intent);

    通过以上代码,就可以创建一个桌面快捷方式了。

  • 相关阅读:
    shell-用户权限操作
    Python 库列表
    【random】模块运用,随机数实例
    Python 原生文件读写
    Python 运用pymysql+pandas 完成连接 MySQL 数据库读
    MySQL命名、设计及使用规范
    测试for循环计算耗时
    正则表达式速查表
    MySQL8.0.21下载安装详细教程
    MySQL 修改目录重置
  • 原文地址:https://www.cnblogs.com/liyiran/p/5231102.html
Copyright © 2011-2022 走看看