狂刷Android範例之5:ApiDemos主程序框架分析

狂刷Android範例之5:ApiDemos主程序框架分析

說明

狂刷Android範例系列文章開張了。每篇學習一個Android範例,將一個範例單獨生成一個可運行的app,並對重點源代碼進行簡要分析。然後提供打包好的源代碼下載。

功能

這篇文章分析ApiDemos的主程序框架,ApiDemos是Google提供的Android範例,也許是最著名的Android範例,本系列文章都是分析ApiDemos中的各個demo。它的主程序框架也是一篇很好的源代碼範例,值得仔細分析。
本文沒有代碼包,代碼包可直接下載Android提供的Apidemos。

來源

本文例子來自於Android-20的com.example.android.apis.ApiDemos。

環境

代碼運行環境:
1.ADT2014版本;
2.android:minSdkVersion=”8”;android:targetSdkVersion=”20”
3.workspace中已經生成了appcompatv7,它的版本是android-22;

代碼

本文有如下幾個知識點,詳細的解釋在代碼註釋中:
第一個知識點,在ApiDemos中可以通過點擊列表項再次啓動下一個層次的ApiDemos Activity;
第二個知識點,使用PackageManager的queryIntentActivities方法可以過濾出過濾出AndroidManifest.xml配置文件中,類似下面描述的Activity

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.SAMPLE_CODE" />
            </intent-filter>

第三個知識點,構建一個多層次的data數據。
對所有使用queryIntentActivities方法過濾出來的Activity進行檢查,可以啓動的demo通過activityIntent方法生成intent,並加入data。含有下一層級的Activity,通過browseIntent方法生成一個可以啓動ApiDemos的intent,並裝入data。這樣就形成了一個多層次的data文件,對於葉子節點,點擊即可啓動相應demo程序,例如app/HelloWorld;對於其他節點,點擊則再啓動一個下一層級的ApiDemos,並將nextLabel作爲path參數傳入。

第四個知識點,使用java的Comparator類對任意Collection對象進行泛型排序。

具體分析請看例子代碼及註釋

/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.apis;

import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ApiDemos extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //在ApiDemos中可以通過點擊列表項再次啓動下一個層次的ApiDemos Activity
        //此處path參數就是用來指示啓動的Intent,若path爲null,則是直接啓動ApiDemos
        //若path爲app/accessibility這種形式,則是在列表中點擊某項再次啓動的ApiDemos Activity
        Intent intent = getIntent();
        String path = intent.getStringExtra("com.example.android.apis.Path");

        if (path == null) {
            path = "";
        }

        //創建一個SimpleAdapter來適配ListActivity,使用getData方法得到一個List<Map<String, Object>>對象
        //使用該對象來初始化列表項
        setListAdapter(new SimpleAdapter(this, getData(path),
                android.R.layout.simple_list_item_1, new String[] { "title" },
                new int[] { android.R.id.text1 }));
        getListView().setTextFilterEnabled(true);
    }

    protected List<Map<String, Object>> getData(String prefix) {
        List<Map<String, Object>> myData = new ArrayList<Map<String, Object>>();

        //mainIntent是一個用來過濾的Intent,它將過濾出AndroidManifest.xml配置文件中,類似下面描述的Activity
//        <intent-filter>
//        <action android:name="android.intent.action.MAIN" />
//        <category android:name="android.intent.category.SAMPLE_CODE" />
//        </intent-filter>

        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_SAMPLE_CODE);

        PackageManager pm = getPackageManager();
        List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);

        if (null == list)
            return myData;

        //定義上層前綴prefix,對於app/accessibility,它的前綴就是app,對於app,它的前綴就是null
        String[] prefixPath;
        String prefixWithSlash = prefix;

        if (prefix.equals("")) {
            prefixPath = null;
        } else {
            prefixPath = prefix.split("/");
            prefixWithSlash = prefix + "/";
        }

        int len = list.size();

        //用來存儲已經掃描過的demo的Map
        Map<String, Boolean> entries = new HashMap<String, Boolean>();

        //對所有使用queryIntentActivities方法過濾出來的Activity進行檢查
        //可以啓動的demo通過activityIntent方法生成intent,並加入data
        //含有下一層級的Activity,通過browseIntent方法生成一個可以啓動ApiDemos的intent,並裝入data
        //這樣就形成了一個多層次的data文件,對於葉子節點,點擊即可啓動相應demo程序,例如app/HelloWorld
        //對於其他節點,點擊則再啓動一個下一層級的ApiDemos,並將nextLabel作爲path參數傳入
        for (int i = 0; i < len; i++) {
            ResolveInfo info = list.get(i);
            CharSequence labelSeq = info.loadLabel(pm);
            String label = labelSeq != null
                    ? labelSeq.toString()
                    : info.activityInfo.name;

            if (prefixWithSlash.length() == 0 || label.startsWith(prefixWithSlash)) {

                String[] labelPath = label.split("/");

                String nextLabel = prefixPath == null ? labelPath[0] : labelPath[prefixPath.length];

                if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) {
                    addItem(myData, nextLabel, activityIntent(
                            info.activityInfo.applicationInfo.packageName,
                            info.activityInfo.name));
                } else {
                    if (entries.get(nextLabel) == null) {
                        addItem(myData, nextLabel, browseIntent(prefix.equals("") ? nextLabel : prefix + "/" + nextLabel));
                        entries.put(nextLabel, true);
                    }
                }
            }
        }

        //對生成的myData對象進行排序,其中使用到了Comparator<Map<String, Object>>的實例
        Collections.sort(myData, sDisplayNameComparator);

        return myData;
    }

    //該變量用來支持Collections.sort方法的排序
    private final static Comparator<Map<String, Object>> sDisplayNameComparator =
        new Comparator<Map<String, Object>>() {
        private final Collator   collator = Collator.getInstance();

        public int compare(Map<String, Object> map1, Map<String, Object> map2) {
            return collator.compare(map1.get("title"), map2.get("title"));
        }
    };

    //將可以啓動的Demo生成一個intent
    protected Intent activityIntent(String pkg, String componentName) {
        Intent result = new Intent();
        result.setClassName(pkg, componentName);
        return result;
    }

    //生成一個Intent,用來在內部跳轉到另一個ApiDemos Activity,帶有path參數
    protected Intent browseIntent(String path) {
        Intent result = new Intent();
        result.setClass(this, ApiDemos.class);
        result.putExtra("com.example.android.apis.Path", path);
        return result;
    }

    //將一個map對象裝入data
    protected void addItem(List<Map<String, Object>> data, String name, Intent intent) {
        Map<String, Object> temp = new HashMap<String, Object>();
        temp.put("title", name);
        temp.put("intent", intent);
        data.add(temp);
    }

    //當點擊list中的項時,從map對象中取出一個intent,並根據此intent進行跳轉
    @Override
    @SuppressWarnings("unchecked")
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Map<String, Object> map = (Map<String, Object>)l.getItemAtPosition(position);

        Intent intent = (Intent) map.get("intent");
        startActivity(intent);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章