Android launcher相关的一些知识

Launcher就是我们Android系统的手机桌面,这里一直是纷繁手机应用世界的必争之地,几乎每个应用都希望在这里有一席之地。

一个Launcher一般由以下几个部分组成:


今天我们先来做一个最简单的手机应用快捷方式添加。手机应用桌面快捷方式的基本原理是:

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

手机应用快捷方式的添加总共分成三大步:

  1. 创建一个添加快捷方式的Intent,该Intent的action属性值是“com.android.launcher.action.INSTALL_SHORTCUT”;
  2. 通过该Intent添加Extra属性来设置快捷方式的标题、图标以及快捷方式要启动的应用程序;
  3. 调用sendBroadcast()方法发送广播即可添加快捷方式
另外,创建快捷方式是需要相应权限的,不要忘了
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>  
  2. <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>  
以下程序实现了对某一应用程序快捷方式的添加,并添加了判断应用程序图标是否已经存在于桌面的判断
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package farsight.com.cn.addicon;  
  2.   
  3. import android.content.Intent;  
  4. import android.database.Cursor;  
  5. import android.net.Uri;  
  6. import android.os.Bundle;  
  7. import android.os.Parcelable;  
  8. import android.support.v7.app.ActionBarActivity;  
  9. import android.view.Menu;  
  10. import android.view.MenuItem;  
  11. import android.view.View;  
  12. import android.widget.Button;  
  13.   
  14. public class MainActivity extends ActionBarActivity {  
  15.   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.   
  21.         Button button = (Button) findViewById(R.id.button);  
  22.         button.setOnClickListener(new View.OnClickListener() {  
  23.             @Override  
  24.             public void onClick(View v) {  
  25.   
  26.                 if(!isExist()) {  
  27.                     // 创建添加快捷方式的Intent  
  28.                     Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");  
  29.                     // 加载快捷方式的应用程序名称  
  30.                     String tile = getResources().getString(R.string.app_name);  
  31.                     // 加载快捷方式的图标  
  32.                     Parcelable icon = Intent.ShortcutIconResource.fromContext(MainActivity.this, R.drawable.ic_launcher);  
  33.                     // 创建点击快捷方式后操作Intent,该处当点击创建的快捷方式后,再次启动该程序  
  34.                     Intent myIntent = new Intent(MainActivity.this,  
  35.                             MainActivity.class);  
  36.                     // 设置快捷方式的标题  
  37.                     intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, tile);  
  38.                     // 设置快捷方式的图标  
  39.                     intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);  
  40.                     // 设置快捷方式对应的Intent  
  41.                     intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);  
  42.                     // 发送广播添加快捷方式  
  43.                     sendBroadcast(intent);  
  44.   
  45.                 }  
  46.             }  
  47.         });  
  48.     }  
  49.   
  50.   
  51.     private boolean isExist() {  
  52.         boolean isExist = false;  
  53.         int version = getSdkVersion();  
  54.         Uri uri = null;  
  55.         //桌面快捷图标的数据保存在手机的 /data/data/com.android.launcher/databases/launcher.db 中,  
  56.         // 同时系统对外提供了一个 LauncherProvider 供外界进行访问。  
  57.         // 在 launcher.db 的 favorites 表中保存了具体的数据,  
  58.         // 可以通过 LauncherProvider 中的 authorities 加 favorites 的形式来访问 favorites 表;  
  59.         // 考虑到程序的兼容性 authorities 的值要根据不同的 SDK 版本来进行设置,  
  60.         // 在 SDK 版本 2.0 之前 authorities 的值为:com.android.launcher.settings,  
  61.         // 而在 SDK 2.0 之后 authorities 的值为:com.android.launcher2.settings;  
  62.         if (version < 2.0) {  
  63.             uri = Uri.parse("content://com.android.launcher.settings/favorites");  
  64.         } else {  
  65.             uri = Uri.parse("content://com.android.launcher2.settings/favorites");  
  66.         }  
  67.         String selection = " title = ?";  
  68.         String[] selectionArgs = new String[] { getResources().getString(R.string.app_name).toString() };  
  69.         Cursor cursor = getContentResolver().query(uri, null, selection, selectionArgs, null);  
  70.   
  71.         if (cursor != null && cursor.getCount() > 0) {  
  72.             isExist = true;  
  73.         }  
  74.   
  75.         if (cursor != null) {  
  76.             cursor.close();  
  77.         }  
  78.   
  79.         return isExist;  
  80.     }  
  81.   
  82.     /** 
  83.      * 得到当前系统SDK版本 
  84.      */  
  85.     private int getSdkVersion() {  
  86.         return android.os.Build.VERSION.SDK_INT;  
  87.     }  
  88.   
  89.   
  90.   
  91. }  

AndroidManifest.xml
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="farsight.com.cn.addicon">  
  4.   
  5.     <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />  
  6.     <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>  
  7.     <application  
  8.         android:allowBackup="true"  
  9.         android:icon="@mipmap/ic_launcher"  
  10.         android:label="@string/app_name"  
  11.         android:supportsRtl="true"  
  12.         android:theme="@style/AppTheme">  
  13.         <activity  
  14.             android:name=".MainActivity"  
  15.             android:label="@string/app_name">  
  16.             <intent-filter>  
  17.                 <action android:name="android.intent.action.MAIN" />  
  18.   
  19.                 <category android:name="android.intent.category.LAUNCHER" />  
  20.             </intent-filter>  
  21.         </activity>  
  22.     </application>  
  23.   
  24. </manifest></span><span style="font-size:12px;">  
  25. </span>  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值