android實現qq郵箱多個表徵圖效果

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   ar   io   color   os   

前幾天,蛋疼的技術主管非要實作類別似裝一個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" />


android實現qq郵箱多個表徵圖效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.