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

来源:互联网 发布:游戏辅助编程 编辑:程序博客网 时间:2024/05/29 20:03
  • 本文标签: Android activity-alias
  • 新需求我的应用将有多个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);   }

写在最后:FOR Freedom 看看外边的世界,以及IT这一行,少不了去Google查资料,最后,安利一些速器代理。

加速器推荐免费方案付费方案官方网站一枝红杏加速器免费方案暂无,稳定高速输入8折优惠码wh80,年付只需80元/年官网直达安云加速器最好用的外贸VPN最低¥30/月官网直达LoCo加速器每天免费2小时最低¥15/月官网直达

本文标签: Android activity-alias

转自 SUN'S BLOG - 专注互联网知识,分享互联网精神!

原文地址Android一个应用多个图标的几种实现方式

相关阅读Chrome 扩展 Stylish :给不喜欢某个网站一键「换肤」

相关阅读将 QQ 音乐、网易云音乐和虾米音乐资源「整合」一起的Chrome 扩展Listen 1

相关阅读8 个「新标签页」Chrome 扩展: 教你把 New Tab 页面玩的溜溜溜

相关阅读7 款实用 Chrome 扩展推荐:帮你提升 Chrome 使用体验

相关阅读无扩展就不是 Chrome 了:15 款优质的Chrome 扩展推荐给大家

相关阅读12 款不能少的使网页浏览获得的最佳体验Chrome 扩展

相关阅读5 款可以带来幸福感的 Chrome 扩展

相关阅读关于 Android 中的 Palette 类的使用案例:色彩自适应的 Toolbar

相关阅读:GIT能做什么、它和SVN在深层次上究竟有什么不同

相关阅读:分享一些对开发者最有用的、用户友好和功能丰富的Google Chrome扩展工具

相关阅读:分享一些实际Android开发过程中很多相见恨晚的工具或网站

相关阅读:我是 G 粉,一直关注 Google,最近 Google 有一些小动作,可能很多人不太了解

相关阅读:机器学习引领认知领域的技术创新,那么SaaS行业会被机器学习如何改变?

相关阅读:VPS 教程系列:Dnsmasq + DNSCrypt + SNI Proxy 顺畅访问 Google 配置教程

相关阅读: 最有用:2017最新能上Google、Facebook的方法

相关BLOG:SUN’S BLOG- 专注互联网知识,分享互联网精神!去看看:www.whosmall.com

原文地址:http://whosmall.com/?post=446

本文作者:Anderson/Jerey_Jobs

博客地址 : http://jerey.cn/
简书地址 : Anderson大码渣
github地址 : https://github.com/Jerey-Jobs

0 0
原创粉丝点击