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

本文详细解析Android系统应用Launcher的功能与构成,包括四大组件的区别,并提供了一个通过按钮实现添加和删除应用程序快捷方式的示例。此外,文章还强调了在设置EXTRA_SHORTCUT_INTENT时确保ACTION属性一致的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自: http://blog.youkuaiyun.com/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

[java] view plain copy
  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


[java] view plain copy
  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;


[java] view plain copy
  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

[java] view plain copy
  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


[java] view plain copy
  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;


[java] view plain copy
  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属性必须设置为相同内容,才能声明删除和创建的快捷方式是一个,进行绑定,

否则删除无法生效

### Unity生成的AndroidManifest.xml代码解析 #### 1. PrivacyActivity `PrivacyActivity` 是一个自定义的 Activity,通常用于展示隐私政策页面。在引用中提到的配置中,`PrivacyActivity` 被设置为应用程序的主入口点[^2]。它通过 `intent-filter` 中的 `MAIN` 动作 `LAUNCHER` 类别来指定为应用启动时的第一个 Activity。 以下是关键配置: - **`meta-data`**: - `useLocalHtml`: 表示是否使用本地 HTML 文件来显示隐私政策内容。 - `privacyUrl`: 指定隐私政策的具体 URL 地址。 ```xml <activity android:name="com.unity3d.player.PrivacyActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="useLocalHtml" android:value="true" /> <meta-data android:name="privacyUrl" android:value="你的隐私政策地址" /> </activity> ``` #### 2. UnityPlayerActivity `UnityPlayerActivity` 是 Unity 提供的标准 Activity,用于加载运行 Unity 游戏或应用的核心逻辑。然而,在引用中提到,直接使用 `UnityPlayerActivity` 可能会导致闪退问题[^1]。因此,推荐使用 `UnityPlayerGameActivity` 作为替代方案。 以下是 `UnityPlayerActivity` 的关键配置: - **`theme`**: 使用 `@style/UnityThemeSelector` 来定义主题样式。 - **`meta-data`**: - `unityplayer.UnityActivity`: 标记该 Activity 是否为 Unity 的主 Activity。 ```xml <activity android:name="com.unity3d.player.UnityPlayerActivity" android:theme="@style/UnityThemeSelector"> <meta-data android:name="unityplayer.UnityActivity" android:value="true" /> </activity> ``` #### 3. 权限配置 权限配置是 Android 应用中非常重要的一部分,用于声明应用需要访问的系统资源。在引用中提到的权限配置如下: - **`INTERNET`**: 允许应用访问互联网资源。这对于 Unity 应用来说是常见的需求,因为许多 Unity 游戏或应用需要从网络加载资源、连接服务器或进行广告展示等操作。 ```xml <uses-permission android:name="android.permission.INTERNET"/> ``` #### 4. 综合作用解析 - **PrivacyActivity** 作为应用的入口点,主要用于展示隐私政策页面。这是为了符合全球范围内的隐私政策合规要求(如 GDPR 或 CCPA)。 - **UnityPlayerActivity** 是 Unity 游戏或应用的核心运行容器。尽管存在闪退问题,但通过使用 `UnityPlayerGameActivity`,可以有效避免这一问题[^1]。 - **权限配置** 确保应用能够正常访问必要的系统资源,例如互联网连接。 ### 示例代码:修改后的 AndroidManifest.xml 以下是一个基于引用内容的完整 `AndroidManifest.xml` 示例: ```xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.unity3d.player" xmlns:tools="http://schemas.android.com/tools"> <application> <!-- PrivacyActivity --> <activity android:name="com.unity3d.player.PrivacyActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="useLocalHtml" android:value="true" /> <meta-data android:name="privacyUrl" android:value="你的隐私政策地址" /> </activity> <!-- UnityPlayerActivity --> <activity android:name="com.unity3d.player.UnityPlayerGameActivity" android:theme="@style/UnityThemeSelector"> <meta-data android:name="unityplayer.UnityActivity" android:value="true" /> </activity> </application> <!-- 权限配置 --> <uses-permission android:name="android.permission.INTERNET"/> </manifest> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值