zoukankan      html  css  js  c++  java
  • android 为应用程序创建桌面快捷方式技巧分享

    手机装的软件过多,找起来很不方便,所以在主页面有一个快捷方式的话会很不错的,本文将介绍如何实现,需要了解跟多的朋友可以参考下
     
     
    我们开发一款软件后,如果手机装的软件过多,去翻的话会很难翻的,所以,在主页面有一个快捷方式的话会很不错的,下面是详细代码: 
    复制代码代码如下:

    /** 
    * 创建桌面快捷方式 
    */ 
    private void createShortcut() { 
    SharedPreferences setting = getSharedPreferences("silent.preferences", 0); 
    // 判断是否第一次启动应用程序(默认为true) 
    boolean firstStart = setting.getBoolean("FIRST_START", true); 
    // 第一次启动时创建桌面快捷方式 
    if (firstStart) { 
    Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); 
    // 快捷方式的名称 
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name2)); 
    // 不允许重复创建 
    shortcut.putExtra("duplicate", false); 
    // 指定快捷方式的启动对象 
    ComponentName comp = new ComponentName(this.getPackageName(), "." + this.getLocalClassName()); 
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp)); 
    // 快捷方式的图标 
    ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.zhangxy); 
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); 
    // 发出广播 
    sendBroadcast(shortcut); 
    // 将第一次启动的标识设置为false 
    Editor editor = setting.edit(); 
    editor.putBoolean("FIRST_START", false); 
    // 提交设置 
    editor.commit(); 


    然后在onCreate()方法里加上上面方法名称就行了: 
    复制代码代码如下:

    // 安装后第一次启动时创建桌面快捷方式 
    createShortcut(); 

    最后在AndroidManifest.xml里加上创建快捷方式的权限就行了: 
    复制代码代码如下:

    <!-- 创建桌面快捷方式的权限 --> 
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> 
  • 相关阅读:
    [代码]比较XML文件差异[cl_proxy_ui_utils=>show_xml_diff]
    查看日期属性(休息日、节假日、星期几)[DAY_ATTRIBUTES_GET]
    ABAP中字符串处理方法小结(一)
    ABAP中字符串处理方法小结(二)
    [问题解决]ALV可输入状态下输入金额/数量字段小数位数提前的问题
    自适应背景图
    js原型继承
    仿留言功能
    鼠标画图效果
    照片切换,24小块分散效果
  • 原文地址:https://www.cnblogs.com/tdalcn/p/3522498.html
Copyright © 2011-2022 走看看