最近把Android基础控件用法,以及基本的工作原理掌握的还行,就步入了APIDemos的学习中,一来可以巩固基础,二来可以学习新的技巧。效果还行!下面就把我学习的理解贴在下面,主要在代码的注释中!
APIDemos.java代码
- /*
- * 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;
- /*
- * 此段代码主要是为了将程序名用列表展示出来,当然,在程序名之外,进行了以label为标准的分类。
- */
- public class ApiDemos extends ListActivity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Intent intent = getIntent();
- String path = intent.getStringExtra("com.example.android.apis.Path");
- if (path == null) {
- path = "";
- }
- 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 getData(String prefix) {
- List<Map> myData = new ArrayList<Map>();
- Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
- mainIntent.addCategory(Intent.CATEGORY_SAMPLE_CODE);
- PackageManager pm = getPackageManager();
- /*
- * PackageManager类的解释:一个用于检索目前在设备上的各种软件包的信息,
- * 可以通过getPackageManager()实例化一个对象出来。
- */
- List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
- /*
- * 通过mainIntent参数来检索出可以通过mainIntent的动作Intent.ACTION_MAIN启动的Activity,并放在List列表中返回。
- */
- if (null == list)
- return myData;
- String[] prefixPath;
- if (prefix.equals("")) {
- prefixPath = null;
- } else {
- prefixPath = prefix.split("/");
- }
- int len = list.size();
- Map<String, Boolean> entries = new HashMap<String, Boolean>();
- for (int i = 0; i < len; i++) {
- ResolveInfo info = list.get(i);//返回列表中第i项的信息
- CharSequence labelSeq = info.loadLabel(pm);//该方法会在PackageManager下载Application的标签的时候回调。
- String label = labelSeq != null
- ? labelSeq.toString()
- : info.activityInfo.name;
- if (prefix.length() == 0 || label.startsWith(prefix)) {
- String[] labelPath = label.split("/");//如果label是以prefix(路径)开头的,则将该label以“/”分割。
- 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);
- }
- }
- }
- }
- Collections.sort(myData, sDisplayNameComparator);//以指定的比较对指定的列表进行排序,这个方法我以前没用过,感觉这个挺实用的!
- return myData;
- }
- //此为指定的比较,主要是将对象转换为String类型进行比较!
- private final static Comparator<Map> sDisplayNameComparator = new Comparator<Map>() {
- private final Collator collator = Collator.getInstance();
- public int compare(Map map1, Map map2) {
- return collator.compare(map1.get("title"), map2.get("title"));
- }
- };
- protected Intent activityIntent(String pkg, String componentName) {
- Intent result = new Intent();
- result.setClassName(pkg, componentName);
- return result;
- }
- protected Intent browseIntent(String path) {
- Intent result = new Intent();
- result.setClass(this, ApiDemos.class);
- result.putExtra("com.example.android.apis.Path", path);
- return result;
- }
- protected void addItem(List<Map> data, String name, Intent intent) {
- Map<String, Object> temp = new HashMap<String, Object>();
- temp.put("title", name);
- temp.put("intent", intent);
- data.add(temp);
- }
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
- Map map = (Map) l.getItemAtPosition(position);
- Intent intent = (Intent) map.get("intent");
- startActivity(intent);
- }
- }
只注释了某些类的和方法的用法。逻辑算法段并未注释,不过,逻辑算法部分对我还是有点难度,呵呵……我看逻辑算法片段代码的时候经常用断点调试作为辅助,这样能更好理解程序的执行过程和某些值的变化过程。
我这里有一个别人将的Android1.5版本APIDemos梳理过的文件,方便大家查询某些功能的效果和源代码!
为了让大家透明下载,先把文件部分内容截图:
以下是连接地址:http://download.csdn.net/source/2704411