Android Launcher开发(四)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

  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


  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.         <pre code_snippet_id="80020" snippet_file_name="blog_20131124_2_5600989" name="code" class="java">    <!-若希望在长按Menu键,添加快捷方式中出现应用条目,则需要加入以下内容-></pre><br>  <intent-filter ><action android:name="android.intent.action.CREATE_SHORTCUT"/> </intent-filter> </activity> </application></manifest><pre></pre><p><span style="color:#000000;background:rgb(255,255,255)">LancherDemoActivity;</span></p><p></p><p><span style="color:#000000;background:rgb(255,255,255)"></span></p><pre code_snippet_id="80020" snippet_file_name="blog_20131124_3_9679407" name="code" class="java">import android.app.Activity;  
  26. import android.content.ComponentName;  
  27. import android.content.Intent;  
  28. import android.os.Bundle;  
  29. import android.view.View;  
  30. import android.view.View.OnClickListener;  
  31. import android.widget.Button;  
  32.   
  33. public class LancherDemoActivity extends Activity {  
  34.     private Button button1;  
  35.     private Button button2;  
  36.   
  37.     public void onCreate(Bundle savedInstanceState) {  
  38.         super.onCreate(savedInstanceState);  
  39.         setContentView(R.layout.main);  
  40.         button1 = (Button) findViewById(R.id.BT_InstallShortCurt);  
  41.         button2 = (Button) findViewById(R.id.BT_UnInstallShortCurt);  
  42.         button1.setOnClickListener(new OnClickListener() {  
  43.               
  44. public void onClick(View v) {  
  45.     addShortCurt2DeskTop();  
  46.     }  
  47.   
  48. /** 
  49.  * 添加桌面快捷方式 
  50. */  
  51. private void addShortCurt2DeskTop() {  
  52.         Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");    
  53.         //快捷方式的名称   
  54.     shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "TonyAutoShortCut");   
  55.     shortcut.putExtra("duplicate"false); //不允许重复创建    
  56.     shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(LancherDemoActivity.this, R.drawable.beauty));  
  57.     shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(LancherDemoActivity.this,LancherDemoActivity.class).setAction(<span style="color:#FF0000;">"com.android.action.test"</span>));  
  58.   
  59. //发送广播  
  60. sendBroadcast(shortcut);  
  61.     }  
  62. });  
  63.           
  64. /** 
  65. * 删除桌面快捷方式 
  66. */  
  67. button2.setOnClickListener(new OnClickListener() {  
  68.    public void onClick(View v) {  
  69.     deleteShortCurt2DeskTop();    
  70.    }  
  71.      
  72.    private void deleteShortCurt2DeskTop() {  
  73.    Intent shortcut = new   Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");    
  74.                     
  75.         //快捷方式的名称    
  76.     shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "TonyAutoShortCut");    
  77.                         
  78.     shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(LancherDemoActivity.this,LancherDemoActivity.class).setAction(<span style="color:#FF0000;">"com.android.action.test"</span>));    
  79.                 sendBroadcast(shortcut);  
  80.             }  
  81.         });  
  82.           
  83.           
  84.     }  
  85. }</pre><br><br><p></p><span style="color:#FF0000;">注意事项: 在设置 EXTRA_SHORTCUT_INTENT时,添加和删除快捷方式的这个INTENT对象的ACTION属性必须设置为相同内容,才能声明删除和创建的快捷方式是一个,进行绑定,</span><p><span style="font-family:宋体;"><span style="color:#FF0000;">否则删除无法生效</span><br></span></p><p><br></p><p><br><span style="font-family:宋体;"></span></p><p><span style="font-family:宋体;"><br></span></p><p><span style="font-family:宋体;"><br></span></p><br><pre></pre><pre></pre><pre></pre> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值