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

    Android中,应用程序快捷方式是桌面最基本的组件。用于直接启动应用程序,本文将对应用程序创建进行分析讲解。
    创建应用程序快捷方式主要有以下几种:
    • 在launcher的应用程序列表上,长按某一应用程序图标创建快捷方式到桌面
    • 在桌面上长按在弹出框中选择快捷方式->应用程序->将添加快捷方式的程序
    • 通过程序运行时自动创建
    下面将对第三种进行详细介绍:
    实现思路是:我们都知道我们的桌面都是通过launcher来控制的,所以我们可以通过下面两种方式来实现快捷方式的自动创建:
    • 通过向launcher发送Broadcast让launcher创建快捷方式
    • 为应用程序的组件注册某一个符合特定条件的IntentFilter,然后可以直接在Launcher的桌面添加启动该组件的快捷方式。
    下面是这两种创建方式的核心代码:

    首先第一种方式:

        private void createShortCut()
    
                // First, set up the shortcut intent.  For this example, we simply create an intent that
                // will bring us directly back to this activity.  A more typical implementation would use a 
                // data Uri in order to display a more specific result, or a custom action in order to 
                // launch a specific operation.
                Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
                shortcutIntent.setClassName(this, this.getClass().getName());
              
    
               // Then, set up the container intent (the response to the caller)
    
                Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
                Parcelable icon = Intent.ShortcutIconResource.fromContext(this,R.drawable.icon);
                intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
                intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
    
                intent .putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
        //duplicate created
               intent  .putExtra("duplicate", false);
                sendBroadcast(intent );
        }
    
    

    运行上述代码,当快捷方式创建成功后,launcher将通过toast的方式提示快捷方式创建成功,其中通过
    shortCutIntent.putExtra("duplicate", false);设置不能重复创建,如果快捷方式已经创建则提示快捷方式已经创建,当然这种方式不是很好,因为每次运行程序的时候都会提示快捷方式已经创建。
    注意如果要让上述代码能成功运行,我们还需要设置Uses permission

    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

    device.png device1.png                                                  
    第二种方式和第一种有些类似,不过我们不用广播的方式让给launcher创建,而是通过注册IntentFilter,由于“添加快捷方式”Action是 由Launcher通过startActivity-ForResult这一方法发出的,在Activity启动后把初始化的快捷方式 Intent返回给Launcher应用程序,设置结果值为RESULT_OK表示正常返回。
    主要代码如下:
    首先在xml中设置IntentFilter

        <intent-filter>
        <action android:name="android.intent.action.CREATE_SHORTCUT" />
        </intent-filter>
    
    
    创建核心代码:
        // Resolve the intent
    
        final Intent intent = getIntent();
        final String action = intent.getAction();
    
        // If the intent is a request to create a shortcut, we'll do that and exit
    
        if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
        // First, set up the shortcut intent. For this example, we simply create an intent that
        // will bring us directly back to this activity. A more typical implementation would use a
        // data Uri in order to display a more specific result, or a custom action in order to
        // launch a specific operation.
        Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
        shortcutIntent.setClassName(this, this.getClass().getName());
    
    
        // Then, set up the container intent (the response to the caller)
    
        Intent intent = new Intent();
        Parcelable icon = Intent.ShortcutIconResource.fromContext(this,R.drawable.icon);
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
    
        shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
        //duplicate created
        shortCutIntent.putExtra("duplicate", false);
    
        setResult(RESULT_OK, intent);
    
        }
    
    

    在launcher中我们运行程序就可以将快捷方式创建在桌面上。
    device2.png                                             

    通过上述方式可以自动将快捷方式创建到桌面上,但是每次运行程序时都会将快捷方式创建到桌面上,下面我们将通过程序判断快捷方式是否已经创建到桌面上了,基本思路是:由于快捷方式launcher管理的,我们可以通过查看launcher中是否已经有此快捷方式数据,如果有就不在创建。
    主要代码如下:

       private boolean shortCutInstalled() {
                    boolean isInstallShortcut = false;
                    final ContentResolver cr = this.getContentResolver();
                    final String AUTHORITY = "com.android.launcher.settings";
                    final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
                                    + "/favorites?notify=true");
    
                    Cursor c = cr.query(CONTENT_URI,
                                    new String[] { "title", "iconResource" }, "title=?",
                                    new String[] { getString(R.string.app_name) }, null);
                    if (c != null && c.getCount() > 0) {
                            isInstallShortcut = true;
                    }
                    return isInstallShortcut;
            }

    如果要进行上述查询操作,需要具有以下权限:

    <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"></uses-permission>

    这样通过上述方法就能创建快捷方式到桌面上了。此时我们发现一个问题,通过程序创建的快捷方式不会随着程序卸载而自动删除。

    声明:以上内容皆为自己的粗浅认识,难免会有遗漏或者错误的地方,还请大家多多指点和导论。


    未知原文地址,如是作者,请留言,定将附上原文链接。

  • 相关阅读:
    JavaWeb学习总结(一)——使用Session防止表单重复提交
    java_socket套接字网络编程_实现多线程聊天
    服务器响应的生成:HTTP状态代码——HttpServletResponse接口应用
    服务器响应的生成:HTTP响应报头——HttpServletResponse接口的应用
    java_socket套接字网络编程
    JavaWeb学习总结(二)——JSP中的九个内置对象
    Servlet客户请求的处理:HTTP请求报头HttpServletRequest接口应用
    文件上传利器SWFUpload使用指南
    JavaScript高级特性创建对象的九种方式
    JavaScript高级特性实现继承的七种方式
  • 原文地址:https://www.cnblogs.com/xieyuan/p/3787395.html
Copyright © 2011-2022 走看看