Android Launcher开发(五)添加应用程序桌面快捷方常见问题及解决方案
来源:互联网 发布:新浪邮箱端口号 编辑:程序博客网 时间:2024/05/22 14:30
最近做到的应用做刚好需要添加快捷方式的功能, 在参考了源代码和网上一些其他资料后做了出来.
在做的时候遇到两个问题,
一. 程序卸载后桌面快捷方式仍然存在:
关于此问题, 网上的资料和实际中很多应用程序的老版本或者当前版本仍存在. 参考源代码后,我找出了解决方案: 创建shortcut时需要设置 Extre_ShortCut_Intent 的action.和category,使创建的shortcut与自己的应用产生绑定的关系:
二. 需要判断快捷方式是否存在,快捷方式的信息,在系统应用launcher中以contentprovider的形式提供数据.
需要从com.android.launcher的launcher.db的favorites表中读取快捷方式的信息
对于lancher 的provider节点中的AUTHORITY属性,在 Android属性中变成了"com.android.launcher2.settings". 而2.2之前的版本为"com.android.launcher.settings"
如果应用程序需要兼容2.2以下的机器,需要获取当前手机系统的版本号,更改Uri中的AUTHORITY.
解决了如上两个问题,该功能就完善了,需要的朋友可以直接加到要上线的应用中.
效果如图;
全部代码如下,已经加入了详细的注解:
1. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.autoLancher" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/> <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/> <!-- 快捷方式信息需要从setting中读取 --> <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".LauncherDemo2Activity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter><!-如果想让应用出现在长按Menu键选择添加快捷方式中,则需要加入以下代码-> <intent-filter><action android:name="android.intent.action.CREATE_SHORTCUT"></action> </intent-filter> </activity> </application></manifest>2. LauncherDemo2Activity:package com.android.autoLancher;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.content.ContentResolver;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.content.Intent;import android.database.Cursor;import android.net.Uri;import android.os.Bundle;public class LauncherDemo2Activity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); boolean flag =isShortcutInstalled();//如果已经创建,则不需要在创建 if(flag==false){ AlertDialog.Builder builder = new Builder(LauncherDemo2Activity.this); builder.setTitle("是否为此应用创建桌面快捷方式"); builder.setPositiveButton("是", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { addShortcutToDesktop(); } }); builder.setNegativeButton("否", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }); builder.create().show(); } } private void addShortcutToDesktop() { Intent shortcut = new Intent( "com.android.launcher.action.INSTALL_SHORTCUT"); // 不允许重建 shortcut.putExtra("duplicate", false); // 设置名字 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "加菲快捷"); // 设置图标 shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.cat)); // 设置意图和快捷方式关联程序 Intent intent = new Intent(this, this.getClass()); // 桌面图标和应用绑定,卸载应用后系统会同时自动删除图标 intent.setAction("android.intent.action.MAIN"); intent.addCategory("android.intent.category.LAUNCHER"); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); // 发送广播 sendBroadcast(shortcut); } /** * 快捷方式信息是保存在com.android.launcher的launcher.db的favorites表中 * * @return */ public boolean isShortcutInstalled() { boolean isInstallShortcut = false; final ContentResolver cr = LauncherDemo2Activity.this .getContentResolver(); // 2.2系统是”com.android.launcher2.settings”,网上见其他的为"com.android.launcher.settings" String AUTHORITY = null; /* * 根据版本号设置Uri的AUTHORITY */ if(getSystemVersion()>=8){ AUTHORITY = "com.android.launcher2.settings"; }else{ AUTHORITY = "com.android.launcher.settings"; } 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);// XXX表示应用名称。 if (c != null && c.getCount() > 0) { isInstallShortcut = true; System.out.println("已创建"); } return isInstallShortcut; } /** * 获取系统的SDK版本号 * @return */ private int getSystemVersion(){ return Build.VERSION.SDK_INT; } }
- Android Launcher开发(五)添加应用程序桌面快捷方常见问题及解决方案
- Android Launcher开发(五)添加应用程序桌面快捷方常见问题及解决方案
- Android Launcher开发——添加应用程序桌面快捷方常见问题及解决方案
- Android Launcher开发(四)Launcher概述及简单添加和删除应用程序的快捷方式
- Android Launcher开发(四)Launcher概述及简单添加和删除应用程序的快捷方式
- Android Launcher开发(四)Launcher概述及简单添加和删除应用程序的快捷方式
- Android Launcher开发添加应用程序的快捷方式
- Android更改桌面应用程序launcher
- Android 开发之系统应用Launcher详解,简单添加和删除快捷方式及常见问题
- android添加桌面快捷图库
- Android 如何在Launcher桌面创建或者删除快捷图标
- 说说Android桌面(Launcher应用)背后的故事(二)——应用程序的添加
- 说说Android桌面(Launcher应用)背后的故事(二)——应用程序的添加
- 说说Android桌面(Launcher应用)背后的故事(二)——应用程序的添加
- 说说Android桌面(Launcher应用)背后的故事(二)——应用程序的添加
- 说说Android桌面(Launcher应用)背后的故事(二)——应用程序的添加
- android-需求(五):桌面快捷
- 说说Android桌面(Launcher应用)背后的故事(五)——桌面壁纸的添加
- js获取简单类型的json对象键名及值:动态、静态
- MFC 动态曲线 支持缩放 显示图例(CStatic派生类)(续)
- 读书笔记:SQL SERVER 2008高级编程:批处理和脚本
- 如何照顾宝宝
- objective-c 关键字和概念
- Android Launcher开发(五)添加应用程序桌面快捷方常见问题及解决方案
- linux网络高级编程之 fcntl和select使用
- poj 1631 最长上升子序列 nlogn
- Sobel算子边缘检测(vc实现)
- hdu 3714 Error Curves
- 局域网远程桌面使用(基于win7平台)
- 编程需要学习多少数学知识
- linux sar用法
- Linux VIM 搭建自己的IDE 一