`
hyshucom
  • 浏览: 808229 次
文章分类
社区版块
存档分类
最新评论

Android 开发之系统应用Launcher详解,简单添加和删除快捷方式及常见问题 http://blog.csdn.net/t12x3456/article/details/7857925Android 开发之系统应用Launcher详解,简单添加和删除快捷方式及常见问题

 
阅读更多
转载自:http://blog.csdn.net/t12x3456/article/details/7857925

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


1..Launcher是什么?


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

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


2.Launcher的构成:


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

ShortCut:应用程序的快捷方式

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

LiveFolder:文件夹

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

WallPaper:壁纸



预览图如下:



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


Intent.EXTRA_SHORTCUT_INTENT,

布局文件:

Main.xml

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. <TextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="@string/hello"/>
  10. <Button
  11. android:id="@+id/BT_InstallShortCurt"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:text="InstallShortCurt"/>
  15. <Button
  16. android:id="@+id/BT_UnInstallShortCurt"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:text="UnInstallShortCurt"/>
  20. </LinearLayout>


清单文件Mainifest.xml


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

LancherDemoActivity;


  1. importandroid.app.Activity;
  2. importandroid.content.ComponentName;
  3. importandroid.content.Intent;
  4. importandroid.os.Bundle;
  5. importandroid.view.View;
  6. importandroid.view.View.OnClickListener;
  7. importandroid.widget.Button;
  8. publicclassLancherDemoActivityextendsActivity{
  9. privateButtonbutton1;
  10. privateButtonbutton2;
  11. publicvoidonCreate(BundlesavedInstanceState){
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. button1=(Button)findViewById(R.id.BT_InstallShortCurt);
  15. button2=(Button)findViewById(R.id.BT_UnInstallShortCurt);
  16. button1.setOnClickListener(newOnClickListener(){
  17. publicvoidonClick(Viewv){
  18. addShortCurt2DeskTop();
  19. }
  20. /**
  21. *添加桌面快捷方式
  22. */
  23. privatevoidaddShortCurt2DeskTop(){
  24. Intentshortcut=newIntent("com.android.launcher.action.INSTALL_SHORTCUT");
  25. //快捷方式的名称
  26. shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,"TonyAutoShortCut");
  27. shortcut.putExtra("duplicate",false);//不允许重复创建
  28. shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(LancherDemoActivity.this,R.drawable.beauty));
  29. shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,newIntent(LancherDemoActivity.this,LancherDemoActivity.class).setAction(<spanstyle="color:#FF0000;">"com.android.action.test"</span>));
  30. //发送广播
  31. sendBroadcast(shortcut);
  32. }
  33. });
  34. /**
  35. *删除桌面快捷方式
  36. */
  37. button2.setOnClickListener(newOnClickListener(){
  38. publicvoidonClick(Viewv){
  39. deleteShortCurt2DeskTop();
  40. }
  41. privatevoiddeleteShortCurt2DeskTop(){
  42. Intentshortcut=newIntent("com.android.launcher.action.UNINSTALL_SHORTCUT");
  43. //快捷方式的名称
  44. shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,"TonyAutoShortCut");
  45. shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,newIntent(LancherDemoActivity.this,LancherDemoActivity.class).setAction(<spanstyle="color:#FF0000;">"com.android.action.test"</span>));
  46. sendBroadcast(shortcut);
  47. }
  48. });
  49. }
  50. }


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

否则删除无法生效

1..Launcher是什么?


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

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


2.Launcher的构成:


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

ShortCut:应用程序的快捷方式

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

LiveFolder:文件夹

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

WallPaper:壁纸



预览图如下:



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


Intent.EXTRA_SHORTCUT_INTENT,

布局文件:

Main.xml

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. <TextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="@string/hello"/>
  10. <Button
  11. android:id="@+id/BT_InstallShortCurt"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:text="InstallShortCurt"/>
  15. <Button
  16. android:id="@+id/BT_UnInstallShortCurt"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:text="UnInstallShortCurt"/>
  20. </LinearLayout>


清单文件Mainifest.xml


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

LancherDemoActivity;


  1. importandroid.app.Activity;
  2. importandroid.content.ComponentName;
  3. importandroid.content.Intent;
  4. importandroid.os.Bundle;
  5. importandroid.view.View;
  6. importandroid.view.View.OnClickListener;
  7. importandroid.widget.Button;
  8. publicclassLancherDemoActivityextendsActivity{
  9. privateButtonbutton1;
  10. privateButtonbutton2;
  11. publicvoidonCreate(BundlesavedInstanceState){
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. button1=(Button)findViewById(R.id.BT_InstallShortCurt);
  15. button2=(Button)findViewById(R.id.BT_UnInstallShortCurt);
  16. button1.setOnClickListener(newOnClickListener(){
  17. publicvoidonClick(Viewv){
  18. addShortCurt2DeskTop();
  19. }
  20. /**
  21. *添加桌面快捷方式
  22. */
  23. privatevoidaddShortCurt2DeskTop(){
  24. Intentshortcut=newIntent("com.android.launcher.action.INSTALL_SHORTCUT");
  25. //快捷方式的名称
  26. shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,"TonyAutoShortCut");
  27. shortcut.putExtra("duplicate",false);//不允许重复创建
  28. shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(LancherDemoActivity.this,R.drawable.beauty));
  29. shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,newIntent(LancherDemoActivity.this,LancherDemoActivity.class).setAction(<spanstyle="color:#FF0000;">"com.android.action.test"</span>));
  30. //发送广播
  31. sendBroadcast(shortcut);
  32. }
  33. });
  34. /**
  35. *删除桌面快捷方式
  36. */
  37. button2.setOnClickListener(newOnClickListener(){
  38. publicvoidonClick(Viewv){
  39. deleteShortCurt2DeskTop();
  40. }
  41. privatevoiddeleteShortCurt2DeskTop(){
  42. Intentshortcut=newIntent("com.android.launcher.action.UNINSTALL_SHORTCUT");
  43. //快捷方式的名称
  44. shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,"TonyAutoShortCut");
  45. shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,newIntent(LancherDemoActivity.this,LancherDemoActivity.class).setAction(<spanstyle="color:#FF0000;">"com.android.action.test"</span>));
  46. sendBroadcast(shortcut);
  47. }
  48. });
  49. }
  50. }


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

否则删除无法生效

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics