Android应用多图标 Android中一个应用实现多个图标的几种方式

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android应用多图标 Android中一个应用实现多个图标的几种方式

Anderson/Jerey_Jobs   2021-03-24 我要评论
想了解Android中一个应用实现多个图标的几种方式的相关内容吗,Anderson/Jerey_Jobs在本文为您仔细讲解Android应用多图标的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:android,应用图标,android,设置应用图标,android,更多应用图标,下面大家一起来学习吧。

前言

最近因为新需求需要,我的应用将有多个ICON入口..最终选择了 activity-alias , 其实实现多图标有好几种方式,下面就给大家总结下,分享出来供大家参考学习:

1. 多Activity + intent-filter方式

因为launcher会扫描app中含有以下intent-filter属性的标签, 有的话就会将其添加到桌面.

所以只要在你想添加到桌面的activity下加上以下标签即可.

<intent-filter>
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

2. activity-alias方式

上面第一种方式对于一个activity的要求是没法做的, 只能通过多写几个入口Activity+ 跳转参数的方式来解决, 而 activity-alias方式就可以很好解决该问题.

activity-alias中, android:name 就是你定义这个activity为什么名字, 你通过点击这个图标进入的话, 在代码中

getIntent().getComponent().getClassName()

可以获取到该名字, targetActivity 就是你点击该图标后的目标activity. 上面代码是写在目标activity里面的,获取到的名字依然是我们定义的名字哦. 这样就可以通过这个判断是通过哪个入口进来的了.

<activity-alias
 android:name="@string/altman"
 android:exported="true"
 android:icon="@drawable/speech_01"
 android:label="@string/altman_app_name"
 android:screenOrientation="landscape"
 android:targetActivity="com.avatar.dialog.DialogActivity"
 android:theme="@style/DialogActivityTheme" >
 <intent-filter>
  <action android:name="android.intent.action.MAIN" />

  <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
</activity-alias>

3. 网页标签-添加快捷方式

这只是针对特殊情形, 比如UC浏览器创建一个网页标签在桌面上,是向桌面应用(launcher)发送相关action的广播,相关的action如下:

public static final String ACTION_ADD_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";

需要的权限:

<!-- 添加快捷方式 -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<!-- 移除快捷方式 -->
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
<!-- 查询快捷方式 -->
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />

添加图标

private void addShortcut(String name) {
  Intent addShortcutIntent = new Intent(ACTION_ADD_SHORTCUT);

  // 不允许重复创建
  addShortcutIntent.putExtra("duplicate", false);// 经测试不是根据快捷方式的名字判断重复的
  // 应该是根据快链的Intent来判断是否重复的,即Intent.EXTRA_SHORTCUT_INTENT字段的value
  // 但是名称不同时,虽然有的手机系统会显示Toast提示重复,仍然会建立快链
  // 屏幕上没有空间时会提示
  // 注意:重复创建的行为MIUI和三星手机上不太一样,小米上似乎不能重复创建快捷方式

  // 名字
  addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);

  // 图标
  addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
    Intent.ShortcutIconResource.fromContext(MainActivity.this,
      R.drawable.ic_launcher));

  // 设置关联程序
  Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
  launcherIntent.setClass(MainActivity.this, MainActivity.class);
  launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);

  addShortcutIntent
    .putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent);

  // 发送广播
  sendBroadcast(addShortcutIntent);
 }

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们