android实现qq邮箱多个图标效果

前几天,蛋疼的技术主管非要实现类似装一个qq邮箱,然后可以使用qq邮箱日历的那么一个东西,相当于一个应用生成两个图标,但是不同的是点击不同的图标可以进入不同的应用,如下图的效果。

这效果百度了一天也不知道如何着手,只能自己搞,分享一下自己解决这个问题的过程,大概是这样的

1.首先分析来说整个桌面luncher是一个activity,所有的图标都是一个按钮而已,点击图标就是点击一个按钮然后去执行activity

2.查看launcher framework层的源代码,https://android.googlesource.com/platform/packages/apps/Launcher/+/master/src/com/android/launcher/Launcher.java  路径是这个,查看可通过翻墙。这类其实和咱自己写的类也没啥区别.  因为Launcher是继承了activity的

public final class Launcher extends Activity implements View.OnClickListener, OnLongClickListener

其次我们只需要找到click事件就行,在这里他会判断被点击view是文件夹还是应用程序,

public void onClick(View v) {
        Object tag = v.getTag();
        if (tag instanceof ApplicationInfo) {
            // Open shortcut
            final Intent intent = ((ApplicationInfo) tag).intent;
            startActivitySafely(intent);
        } else if (tag instanceof FolderInfo) {
            handleFolderClick((FolderInfo) tag);
        }
    }

接下来看看startActivitySafely,其实在这里就是处理了下异常和添加一些个flag,但是flag是重点。解析来会继续说flag

    void startActivitySafely(Intent intent) {
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
        } catch (SecurityException e) {
            Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
            e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
                    ". Make sure to create a MAIN intent-filter for the corresponding activity " +
                    "or use the exported attribute for this activity.", e);
        }
    }

这里其实都很简单,就是添加一个flag,这个flag作用很大,仔细讲一下

FLAG_ACTIVITY_NEW_TASK设置此状态,首先会查找是否存在和被启动的Activity具有相同的亲和性的任务栈(即taskAffinity)如果有直接把这

个栈整体移动到前台,并保持栈中的状态不变,即栈中的activity顺序不变,如果没有,则新建一个栈来存放被启动的activity.
这就是为什么我们点击home键之后然后再点击图标会恢复到原来的状态,而不是重新去创建一个activity。

通过以上的分析大概能实现这样的东西了,现在我只需要让他们运行在不同的任务栈里面即可,相互之间不能够影响。下面是大概实现的流程,仅供参考,因为这个只是基础的模型而已。实际上我们在里面加了很多业务。

大概的思路就这样一下是代码的实现。主要是放入了一个字段叫做class然后点击图标的时候获取这个字段,打开相应的activity即可

public class BootupActivity extends Activity {

    private Handler handler  = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what)
            {
                case 1:

                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.i("BootupActivity", "OnCreate");

        String className = getIntent().getStringExtra("Class");
        if (className==null) {
            addShortcutToDesktop(BootupActivity.this.getString(R.string.shopping_app_name), R.drawable.shopping_ic_launcher,
                    Activity1.class.getName(), Activity1.class);
            addShortcutToDesktop(BootupActivity.this.getString(R.string.xiaohua_app_name), R.drawable.xiaohua_ic_launcher,
                    Activity2.class.getName(), Activity2.class);
            startAppProcess(Activity1.class.getName());
        } else {
            startAppProcess(className);
        }

    }

    private void addShortcutToDesktop(String lable, int iconRes, String destClassName, Class<?> bootupClass) {

        Intent shortcut = new Intent(
                "com.android.launcher.action.INSTALL_SHORTCUT");

        // no rebuilding
        shortcut.putExtra("duplicate", false);
        // shortcut.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
        // setting name
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, lable);
        // setting icon
        if (iconRes!=0) {
            shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                    Intent.ShortcutIconResource.fromContext(this, iconRes));
        }

        // create a broadcast intent
        Intent intent = new Intent(this, bootupClass);
        intent.putExtra("Class", destClassName);
        intent.setAction(Intent.ACTION_MAIN);

        // setting intent
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);

        // send broadcast
        sendBroadcast(shortcut);

    }
    private void startAppProcess(String bootupClass) {
        ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);

        Intent i = new Intent();

        i.setComponent(new ComponentName(this.getPackageName(), bootupClass));
        i.putExtra("class", bootupClass);

        this.startActivity(i);

    }

}

以下是需要在配置文件里面配置的,需要注意到得时android:taskAffinity这个属性,不同的activity需要配置不同的。把主要的activity和默认打开的activity的亲和性配置成一样得。保证点击桌面图标和应用图标能够打开相同的任务栈。然后注意把主要的BootupActivity放在第一个位置。其他得都需要加上一个action并且和主要的相同。

<application
        android:icon="@drawable/ic_launcher"

        android:name="com.zlh.combined.MainApp"
        android:taskAffinity="com.p">
        <activity
            android:name=".BootupActivity"
            android:logo="@drawable/ic_action_search"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>
        </activity>
        <activity
            android:name=".Activity1"
            android:taskAffinity="com.p"
            android:process=":proxy2"
             >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

            </intent-filter>
        </activity>
        <activity
            android:name=".Activity2"
            android:taskAffinity="com.c"
            android:process=":proxy3"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

            </intent-filter>
        </activity>

       </application>
    <!-- 创建桌面快捷方式 -->
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
时间: 2024-08-27 23:15:23

android实现qq邮箱多个图标效果的相关文章

Android配置QQ邮箱问题

用Android系统自带邮箱客户端登录QQ邮箱失败: 解决办法: 从浏览器打开QQ邮箱--->>>设置--->>>邮箱设置--->>>账户--->>> POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务 --->>>勾选前两项即可(POP3/SMTP服务,IMAP/SMTP服务) --->>>保存更改 --->>>OK(再次从Android登录即可)

Android 仿QQ浏览器WebView,滑动隐藏显示ActionBar效果

Android 仿QQ浏览器,滑动隐藏显示ActionBar效果. 往上推,是一个ScrollView会将,actionbar以及内容往上推,当actionbar消失后,将滚动Webview的内容. 此效果是基于QuickReturnHeader源码,修改而来的,代码也不多,实现方法比较简单. 直接上demo:http://download.csdn.net/detail/xufeifandj/8388493 直接看效果图:

使用plupload做一个类似qq邮箱附件上传的效果

公司项目中使用的框架是springmvc+hibernate+spring,目前需要做一个类似qq邮箱附件上传的功能,暂时只是上传小类型的附件 处理过程和解决方案都需要添加附件,处理过程和解决方案都可以添加多个附件,也可一个都不添加 以其中一个为例:(文件保存到了数据库中),有关plupload的内容可参考:http://www.360doc.com/content/14/0714/03/552866_394228686.shtml 首先是po package cn.com.plupload.p

Android学QQ空间相册浏览类型横向滑动效果显示多图片MyHorizontalScrollView

Android学QQ空间相册浏览类型横向滑动效果显示多图片MyHorizontalScrollView 我们来定制一下吧 布局文件:activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="m

【Android】类似QQ风格的popupwindow弹窗效果

[Android]类似QQ风格的popupwindow弹窗效果 该源码主要是实现类似QQ风格的popupwindow弹出窗效果,出现时有遮挡图层,消失时无遮挡图层. 下载地址:http://www.devstore.cn/code/info/273.html

如何变相的绕过QQ邮箱订阅的繁琐核审

先看看正常流程:http://open.mail.qq.com/ 点击“接入订阅”==>申请接入==>登录一下 选择接入完全免费 大概流程就是这样: 下面我们说说快速接入的方法: 1.登录邮箱,选择左侧的“阅读空间”,点击右侧的“制定我的订阅图标” 2.复制代码到html里面 3.效果:(具体的你可以通过样式来美化) ====================================================================== 现在我站在客户或者网友的角度来演示一

【腾讯敏捷转型No.7】QQ邮箱如何通过敏捷成为行业第一

前几篇文章讲到2006年的腾讯是如何开始敏捷转型的,接下来这篇文章,我将向大家讲述,腾讯开始敏捷转型之后,QQ邮箱是如何通过敏捷成为行业第一. 众所周知,张小龙是"微信之父",对他熟悉的人,应该也知道他还是"QQ邮箱之父",但是谁又是"QQ邮箱之母"呢? QQ邮箱的崛起不管是对腾讯公司还是小龙团队都是意义重大而深远的,QQ邮箱能够成为行业第一与敏捷是密不可分的. 2007年,腾讯公司打算进行敏捷转型,但并没有一刀切让所有的产品都立即执行敏捷,而是

基于java mail实现简单的QQ邮箱发送邮件

刚学习到java邮件相关的知识,先写下这篇博客,方便以后翻阅学习. -----------------------------第一步 开启SMTP服务 在 QQ 邮箱里的 设置->账户里开启 SMTP 服务 完成验证 获取授权码(后面代码实现时使用) -----------------------------第二步 环境配置 即下载第三方库 https://github.com/javaee/javamail/releases -----------------------------第三步 代

Android 高仿 QQ5.0 侧滑菜单效果 自定义控件来袭【学习鸿洋_视频博客笔记总结】

学习鸿洋博客:http://blog.csdn.net/lmj623565791/article/details/39257409 学习鸿洋视频:慕课网视频 看看Android 高仿 QQ5.0 侧滑菜单效果 自定义控件实现效果: 技术上,继承HorizontalScrollView 加上自定义ViewGroup来实现: 1.onMeasure:决定内部View(子View)的宽和高,以及自己的宽和高 2.onLayout:决定子View的放置位置 3.onTouchEvent[监听动作] 自定