Android 开发之系统应用Launcher详解,简单添加和删除快捷方式及常见问题

转载自: http://blog.csdn.net/t12x3456/article/details/7857925

Android 开发之系统应用Launcher详解,简单添加和删除快捷方式及常见问题


1.. Launcher是什么?


  1.1  Launcher是系统启动后加载的第一个应用程序

  1.2  Launcher是其他应用程序的入口


2.Launcher的构成:


 

3. 主体四大组件的区别:

ShortCut: 应用程序的快捷方式

Appwidget:桌面小部件,图形不规则

LiveFolder: 文件夹

ContentProvider的形式展示应用中特定数据的集合

WallPaper: 壁纸



预览图如下:



4. 通过按钮添加或者删除应用程序的快捷方式Demo:


Intent.EXTRA_SHORTCUT_INTENT,

布局文件:

Main.xml

[java]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/hello" />  
  11.   
  12.     <Button  
  13.         android:id="@+id/BT_InstallShortCurt"  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="Install ShortCurt" />  
  17.   
  18.     <Button  
  19.         android:id="@+id/BT_UnInstallShortCurt"  
  20.         android:layout_width="match_parent"  
  21.         android:layout_height="wrap_content"  
  22.         android:text="UnInstall ShortCurt" />  
  23.   
  24. </LinearLayout>  


清单文件Mainifest.xml


[java]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.itheima.lancher"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="8" />  
  8.     <!-- 该权限为系统自定义权限 -->  
  9.     <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />    
  10.     <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />   
  11.     <application  
  12.         android:icon="@drawable/ic_launcher"  
  13.         android:label="@string/app_name" >  
  14.         <activity  
  15.             android:name=".LancherDemoActivity"  
  16.             android:label="@string/app_name" >  
  17.             <intent-filter>  
  18.                 <action android:name="android.intent.action.MAIN" />  
  19.   
  20.                 <category android:name="android.intent.category.LAUNCHER" />  
  21.             </intent-filter>  
  22.         </activity>  
  23.           
  24.         <activity android:name=".ShortCutActivity">  
  25.             <intent-filter >  
  26. <!-拦截添加快捷方式,显示土司->  
  27.                 <action android:name="android.intent.action.CREATE_SHORTCUT"/>                  
  28.             </intent-filter>  
  29.         </activity>  
  30.     </application>  
  31.   
  32. </manifest>  

LancherDemoActivity;


[java]  view plain copy
  1. import android.app.Activity;  
  2. import android.content.ComponentName;  
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8.   
  9. public class LancherDemoActivity extends Activity {  
  10.     private Button button1;  
  11.     private Button button2;  
  12.   
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         button1 = (Button) findViewById(R.id.BT_InstallShortCurt);  
  17.         button2 = (Button) findViewById(R.id.BT_UnInstallShortCurt);  
  18.         button1.setOnClickListener(new OnClickListener() {  
  19.               
  20. public void onClick(View v) {  
  21.     addShortCurt2DeskTop();  
  22.     }  
  23.   
  24. /** 
  25.  * 添加桌面快捷方式 
  26. */  
  27. private void addShortCurt2DeskTop() {  
  28.         Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");    
  29.         //快捷方式的名称   
  30.     shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "TonyAutoShortCut");   
  31.     shortcut.putExtra("duplicate"false); //不允许重复创建    
  32.     shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(LancherDemoActivity.this, R.drawable.beauty));  
  33.     shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(LancherDemoActivity.this,LancherDemoActivity.class).setAction(<span style="color:#FF0000;">"com.android.action.test"</span>));  
  34.   
  35. //发送广播  
  36. sendBroadcast(shortcut);  
  37.     }  
  38. });  
  39.           
  40. /** 
  41. * 删除桌面快捷方式 
  42. */  
  43. button2.setOnClickListener(new OnClickListener() {  
  44.    public void onClick(View v) {  
  45.     deleteShortCurt2DeskTop();    
  46.    }  
  47.      
  48.    private void deleteShortCurt2DeskTop() {  
  49.    Intent shortcut = new   Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");    
  50.                     
  51.         //快捷方式的名称    
  52.     shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "TonyAutoShortCut");    
  53.                         
  54.     shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(LancherDemoActivity.this,LancherDemoActivity.class).setAction(<span style="color:#FF0000;">"com.android.action.test"</span>));    
  55.                 sendBroadcast(shortcut);  
  56.             }  
  57.         });  
  58.           
  59.           
  60.     }  
  61. }  


注意事项: 在设置 EXTRA_SHORTCUT_INTENT时,添加和删除快捷方式的这个INTENT对象的ACTION属性必须设置为相同内容,才能声明删除和创建的快捷方式是一个,进行绑定,

否则删除无法生效

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值