Android implements multiple icons in QQ mail, and android implements QQ mail.

Source: Internet
Author: User

Android implements multiple icons in QQ mail, and android implements QQ mail.

A few days ago, the technical director of egg had to install a QQ mailbox, and then use the QQ mailbox calendar to generate two icons, but the difference is that you can click different icons to enter different applications, such as the effect.





In this way, Baidu does not know how to proceed for a day. He can only do this by himself and share with him the process of solving this problem. This is probably the case.

1. for analysis, the entire desktop luncher is an activity, and all icons are a button. clicking the icon means clicking a button and executing the activity.

2. view the source code of the launcher framework layer, which is the https://android.googlesource.com/platform/packages/apps/Launcher/+/master/src/com/android/launcher/Launcher.java path, view can be through the wall. This class is actually no different from the class we write, because Launcher inherits the activity.

public final class Launcher extends Activity implements View.OnClickListener, OnLongClickListener
Next, we only need to find the click event. Here, he will judge whether the clicked view is a folder or an application,

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);        }    }

Next let's take a look at startActivitySafely. In fact, the exception is handled and some flags are added here, but flags are the focus. The flag will be added after parsing.

    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);        }    }
It is actually very simple here, that is, to add a flag, which is very useful. Let's take a closer look.

If FLAG_ACTIVITY_NEW_TASK is set to this status, the system first checks whether there is a job stack with the same affinity as the started Activity (that is, taskAffinity ).

Stack overall move to the front end, and keep the status in the stack unchanged, that is, the activity sequence in the stack remains unchanged. If not, create a new stack to store the started activity. this is why we click the home Key and then click the icon to restore to the original state, instead of re-creating an activity.

Through the above analysis, we can probably implement such a thing. Now I just need to let them run in different task stacks, and they cannot affect each other. The following is a general implementation process for your reference only, because this is only a basic model. In fact, we have added a lot of services to it.


The general idea is to implement the Code. It is mainly to put a field called class and then obtain this field when you click the icon, open the corresponding 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);    }}


You need to configure the following in the configuration file. You need to note that android: taskAffinity must be configured for different activities. Configure the affinity of the main activity and the default opened activity as the same. Ensure that you can click the desktop icon and application icon to open the same job stack. Then place the main BootupActivity in the first position. Add an action for all the other items, which is the same as the primary one.


<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" Ndroid: 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> <! -- Create a desktop shortcut --> <uses-permission android: name = "com. android. launcher. permission. INSTALL_SHORTCUT"/>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.