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 shortcutfinal 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(){@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);switch (msg.what){case 1:break;}}};@Overrideprotected 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 rebuildingshortcut.putExtra("duplicate", false);// shortcut.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);// setting nameshortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, lable);// setting iconif (iconRes!=0) {shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(this, iconRes));}// create a broadcast intentIntent intent = new Intent(this, bootupClass);intent.putExtra("Class", destClassName);intent.setAction(Intent.ACTION_MAIN);// setting intentshortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);// send broadcastsendBroadcast(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并且和主要的相同。
總結
以上是生活随笔為你收集整理的android实现qq邮箱多个图标效果的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 云端数据库更新到本地
- 下一篇: 浙江杭州1040阳光工程叫家里人来投资违