Android Launcher开发——添加应用程序桌面快捷方常见问题及解决方案

原文地址:http://blog.csdn.net/t12x3456/article/details/7857835


最近做到的应用做刚好需要添加快捷方式的功能, 在参考了源代码和网上一些其他资料后做了出来. 

在做的时候遇到两个问题,

一.  程序卸载后桌面快捷方式仍然存在:

  关于此问题, 网上的资料和实际中很多应用程序的老版本或者当前版本仍存在. 参考源代码后,我找出了解决方案: 创建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


[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.android.autoLancher"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="8" />  
  8.     <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>  
  9.     <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>    
  10.     <!-- 快捷方式信息需要从setting中读取 -->  
  11.     <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>   
  12.     <application  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name" >  
  15.         <activity  
  16.             android:name=".LauncherDemo2Activity"  
  17.             android:label="@string/app_name" >  
  18.             <intent-filter>  
  19.                 <action android:name="android.intent.action.MAIN" />  
  20.   
  21.                 <category android:name="android.intent.category.LAUNCHER" />  
  22.             </intent-filter>  
  23.             <pre code_snippet_id="80023" snippet_file_name="blog_20131124_1_7564208" name="code" class="java">              <!-如果想让应用出现在长按Menu键选择添加快捷方式中,则需要加入以下代码->   
  24.             <intent-filter>  
  25. </pre> <action android:name="android.intent.action.CREATE_SHORTCUT"></action> </intent-filter> </activity> </application></manifest><pre></pre><p></p><p></p><p></p><p></p><pre></pre>2. LauncherDemo2Activity:<p></p><p></p><p></p><pre code_snippet_id="80023" snippet_file_name="blog_20131124_2_8437043" name="code" class="java">package com.android.autoLancher;  
  26.   
  27. import android.app.Activity;  
  28. import android.app.AlertDialog;  
  29. import android.app.AlertDialog.Builder;  
  30. import android.content.ContentResolver;  
  31. import android.content.DialogInterface;  
  32. import android.content.DialogInterface.OnClickListener;  
  33. import android.content.Intent;  
  34. import android.database.Cursor;  
  35. import android.net.Uri;  
  36. import android.os.Bundle;  
  37.   
  38. public class LauncherDemo2Activity extends Activity {  
  39.     public void onCreate(Bundle savedInstanceState) {  
  40.         super.onCreate(savedInstanceState);  
  41.         setContentView(R.layout.main);  
  42.           
  43.           
  44.         boolean  flag =isShortcutInstalled();//如果已经创建,则不需要在创建    
  45.         if(flag==false){    
  46.               
  47.             AlertDialog.Builder builder = new Builder(LauncherDemo2Activity.this);  
  48.             builder.setTitle("是否为此应用创建桌面快捷方式");  
  49.             builder.setPositiveButton("是"new OnClickListener() {  
  50.                   
  51.                 public void onClick(DialogInterface dialog, int which) {  
  52.                     addShortcutToDesktop();  
  53.                 }  
  54.             });  
  55.               
  56.             builder.setNegativeButton("否"new OnClickListener() {  
  57.                   
  58.                 public void onClick(DialogInterface dialog, int which) {  
  59.                       
  60.                 }  
  61.             });  
  62.               
  63.             builder.create().show();  
  64.               
  65.               
  66.         }    
  67.           
  68.     }  
  69.   
  70.     private void addShortcutToDesktop() {  
  71.   
  72.         Intent shortcut = new Intent(  
  73.                 "com.android.launcher.action.INSTALL_SHORTCUT");  
  74.         // 不允许重建  
  75.         shortcut.putExtra("duplicate"false);  
  76.         // 设置名字  
  77.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "加菲快捷");  
  78.         // 设置图标  
  79.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,  
  80.                 Intent.ShortcutIconResource.fromContext(this,  
  81.   
  82.                 R.drawable.cat));  
  83.         // 设置意图和快捷方式关联程序  
  84.         Intent intent = new Intent(thisthis.getClass());  
  85.         // 桌面图标和应用绑定,卸载应用后系统会同时自动删除图标  
  86.         intent.setAction("android.intent.action.MAIN");  
  87.         intent.addCategory("android.intent.category.LAUNCHER");  
  88.           
  89.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);  
  90.   
  91.         // 发送广播  
  92.         sendBroadcast(shortcut);  
  93.   
  94.           
  95.     }  
  96.   
  97.     /** 
  98.      * 快捷方式信息是保存在com.android.launcher的launcher.db的favorites表中 
  99.      *  
  100.      * @return 
  101.      */  
  102.     public boolean isShortcutInstalled() {  
  103.         boolean isInstallShortcut = false;  
  104.         final ContentResolver cr = LauncherDemo2Activity.this  
  105.                 .getContentResolver();  
  106.        // 2.2系统是”com.android.launcher2.settings”,网上见其他的为"com.android.launcher.settings"  
  107.         String AUTHORITY = null;  
  108.         /* 
  109.          * 根据版本号设置Uri的AUTHORITY 
  110.          */  
  111.         if(getSystemVersion()>=8){  
  112.             AUTHORITY = "com.android.launcher2.settings";  
  113.         }else{  
  114.             AUTHORITY = "com.android.launcher.settings";  
  115.         }  
  116.           
  117.         Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY  
  118.                 + "/favorites?notify=true");  
  119.         Cursor c = cr.query(CONTENT_URI,  
  120.                 new String[] { "title""iconResource" }, "title=?",  
  121.                 new String[] { getString(R.string.app_name) }, null);// XXX表示应用名称。  
  122.         if (c != null && c.getCount() > 0) {  
  123.             isInstallShortcut = true;  
  124.             System.out.println("已创建");  
  125.         }  
  126.         return isInstallShortcut;  
  127.     }  
  128.       
  129.      /** 
  130.      * 获取系统的SDK版本号 
  131.      * @return 
  132.      */  
  133.     private int getSystemVersion(){  
  134.         return Build.VERSION.SDK_INT;  
  135.     }  
  136.   
  137.  }  
  138. </pre><br><br><p></p><p><br></p><br><pre></pre><pre></pre><pre></pre><pre></pre><pre></pre><pre></pre>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值