• Android 编程下快捷图标的创建


    创建快捷图标的方式

    • 在 Launcher 界面手工添加快捷图标
    • 通过代码向 Launcher 中的广播接收者发送广播来创建快捷图标

    创建快捷图标的原理

    • 采用了 Android 系统的广播机制,发送一个广播,Android 系统的 Launcher 中的 InstallShortcutReceiver 接收到这个广播之后,快捷图标就会被创建。

    创建快捷图标的权限

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

    桌面快捷图标的数据保存在手机的 /data/data/com.android.launcher/databases/launcher.db 中,同时系统对外提供了一个 LauncherProvider 供外界进行访问。在 launcher.dbfavorites 表中保存了具体的数据,可以通过 LauncherProvider 中的 authorities 加 favorites 的形式来访问 favorites 表;考虑到程序的兼容性 authorities 的值要根据不同的 SDK 版本来进行设置,在 SDK 版本 2.0 之前 authorities 的值为:com.android.launcher.settings,而在 SDK 2.0 之后 authorities 的值为:com.android.launcher2.settings;具体参考不同 SDK 版本下 Laucher 中的 AndroidManifest.xml,本文中仅提供 2.0 版本之前的 LauncherProvider 配置信息:

    <!-- The settings provider contains Home's data, like the workspace favorites -->
    <provider
        android:name="LauncherProvider"
        android:authorities="com.android.launcher.settings"
        android:writePermission="com.android.launcher.permission.WRITE_SETTINGS"
        android:readPermission="com.android.launcher.permission.READ_SETTINGS" />

    创建快捷图标的实现

    文件:MainActivity

    package cn.sunzn.cshortcut;
    
    import android.app.Activity;
    import android.content.ComponentName;
    import android.content.Intent;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Parcelable;
    import android.view.Menu;
    
    public class MainActivity extends Activity {
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            createShortCut();
        }
    
        /**
         * 创建快捷图标
         */
        private void createShortCut() {
            // 先判断该快捷是否存在
            if (!isExist()) {
                Intent intent = new Intent();
                // 指定动作名称
                intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
                // 指定快捷方式的图标
                Parcelable icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.youtube);
                intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
                // 指定快捷方式的名称
                intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "YouTube");
                // 指定快捷图标激活哪个activity
                Intent i = new Intent();
                i.setAction(Intent.ACTION_MAIN);
                i.addCategory(Intent.CATEGORY_LAUNCHER);
                ComponentName component = new ComponentName(this, MainActivity.class);
                i.setComponent(component);
                intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, i);
                sendBroadcast(intent);
            }
        }
    
        /**
         * 判断快捷图标是否在数据库中已存在
         */
        private boolean isExist() {
            boolean isExist = false;
            int version = getSdkVersion();
            Uri uri = null;
            if (version < 2.0) {
                uri = Uri.parse("content://com.android.launcher.settings/favorites");
            } else {
                uri = Uri.parse("content://com.android.launcher2.settings/favorites");
            }
            String selection = " title = ?";
            String[] selectionArgs = new String[] { "YouTube" };
            Cursor c = getContentResolver().query(uri, null, selection, selectionArgs, null);
    
            if (c != null && c.getCount() > 0) {
                isExist = true;
            }
    
            if (c != null) {
                c.close();
            }
    
            return isExist;
        }
    
        /**
         * 得到当前系统SDK版本
         */
        private int getSdkVersion() {
            return android.os.Build.VERSION.SDK_INT;
        }
    
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    
    }

    文件:AndroidManifest.xml

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="cn.sunzn.cshortcut"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="15" />
        <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
        <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
        
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/title_activity_main" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
  • 相关阅读:
    怎么获取当前页面的URL
    asp.net设置元素css的属性
    跨页面传值
    html怎么添加背景图片
    web.config配置数据库连接
    SQL Server游标的使用【转】
    动态生成表格呈现还是将表格直接绑定gridview等控件呈现的开发方式选择依据
    T-SQL查询进阶--变量
    sql server 中引號嵌套
    SQLServer中临时表与表变量的区别分析
  • 原文地址:https://www.cnblogs.com/sunzn/p/2910350.html
Copyright © 2020-2023  润新知