APIDemos(2.2)学习笔记(一)

最近把Android基础控件用法,以及基本的工作原理掌握的还行,就步入了APIDemos的学习中,一来可以巩固基础,二来可以学习新的技巧。效果还行!下面就把我学习的理解贴在下面,主要在代码的注释中!

APIDemos.java代码

Code:
  1. /* 
  2.  * Copyright (C) 2007 The Android Open Source Project 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */  
  16.   
  17. package com.example.android.apis;  
  18.   
  19. import android.app.ListActivity;  
  20. import android.content.Intent;  
  21. import android.content.pm.PackageManager;  
  22. import android.content.pm.ResolveInfo;  
  23. import android.os.Bundle;  
  24. import android.view.View;  
  25. import android.widget.ListView;  
  26. import android.widget.SimpleAdapter;  
  27.   
  28. import java.text.Collator;  
  29. import java.util.ArrayList;  
  30. import java.util.Collections;  
  31. import java.util.Comparator;  
  32. import java.util.HashMap;  
  33. import java.util.List;  
  34. import java.util.Map;  
  35. /* 
  36.  * 此段代码主要是为了将程序名用列表展示出来,当然,在程序名之外,进行了以label为标准的分类。 
  37.  */  
  38. public class ApiDemos extends ListActivity {  
  39.   
  40.     @Override  
  41.     public void onCreate(Bundle savedInstanceState) {  
  42.         super.onCreate(savedInstanceState);  
  43.           
  44.         Intent intent = getIntent();  
  45.         String path = intent.getStringExtra("com.example.android.apis.Path");  
  46.           
  47.         if (path == null) {  
  48.             path = "";  
  49.         }  
  50.   
  51.         setListAdapter(new SimpleAdapter(this, getData(path),  
  52.                 android.R.layout.simple_list_item_1, new String[] { "title" },  
  53.                 new int[] { android.R.id.text1 }));  
  54.         getListView().setTextFilterEnabled(true);//不懂这个方法的用处  
  55.     }  
  56.   
  57.     protected List getData(String prefix) {  
  58.         List<Map> myData = new ArrayList<Map>();  
  59.   
  60.         Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);  
  61.         mainIntent.addCategory(Intent.CATEGORY_SAMPLE_CODE);  
  62.   
  63.         PackageManager pm = getPackageManager();  
  64.         /* 
  65.          * PackageManager类的解释:一个用于检索目前在设备上的各种软件包的信息, 
  66.          * 可以通过getPackageManager()实例化一个对象出来。 
  67.          */  
  68.         List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);  
  69.         /* 
  70.          * 通过mainIntent参数来检索出可以通过mainIntent的动作Intent.ACTION_MAIN启动的Activity,并放在List列表中返回。 
  71.          */  
  72.   
  73.         if (null == list)  
  74.             return myData;  
  75.   
  76.         String[] prefixPath;  
  77.           
  78.         if (prefix.equals("")) {  
  79.             prefixPath = null;  
  80.         } else {  
  81.             prefixPath = prefix.split("/");  
  82.         }  
  83.           
  84.         int len = list.size();  
  85.           
  86.         Map<String, Boolean> entries = new HashMap<String, Boolean>();  
  87.   
  88.         for (int i = 0; i < len; i++) {  
  89.             ResolveInfo info = list.get(i);//返回列表中第i项的信息  
  90.             CharSequence labelSeq = info.loadLabel(pm);//该方法会在PackageManager下载Application的标签的时候回调。  
  91.             String label = labelSeq != null  
  92.                     ? labelSeq.toString()  
  93.                     : info.activityInfo.name;  
  94.               
  95.             if (prefix.length() == 0 || label.startsWith(prefix)) {  
  96.                   
  97.                 String[] labelPath = label.split("/");//如果label是以prefix(路径)开头的,则将该label以“/”分割。  
  98.   
  99.                 String nextLabel = prefixPath == null ? labelPath[0] : labelPath[prefixPath.length];  
  100.   
  101.                 if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) {  
  102.                     addItem(myData, nextLabel, activityIntent(  
  103.                             info.activityInfo.applicationInfo.packageName,  
  104.                             info.activityInfo.name));  
  105.                 } else {  
  106.                     if (entries.get(nextLabel) == null) {  
  107.                         addItem(myData, nextLabel, browseIntent(prefix.equals("") ? nextLabel : prefix + "/" + nextLabel));  
  108.                         entries.put(nextLabel, true);  
  109.                     }  
  110.                 }  
  111.             }  
  112.         }  
  113.   
  114.         Collections.sort(myData, sDisplayNameComparator);//以指定的比较对指定的列表进行排序,这个方法我以前没用过,感觉这个挺实用的!  
  115.           
  116.         return myData;  
  117.     }  
  118. //此为指定的比较,主要是将对象转换为String类型进行比较!  
  119.     private final static Comparator<Map> sDisplayNameComparator = new Comparator<Map>() {  
  120.         private final Collator   collator = Collator.getInstance();  
  121.   
  122.         public int compare(Map map1, Map map2) {  
  123.             return collator.compare(map1.get("title"), map2.get("title"));  
  124.         }  
  125.     };  
  126.   
  127.     protected Intent activityIntent(String pkg, String componentName) {  
  128.         Intent result = new Intent();  
  129.         result.setClassName(pkg, componentName);  
  130.         return result;  
  131.     }  
  132.       
  133.     protected Intent browseIntent(String path) {  
  134.         Intent result = new Intent();  
  135.         result.setClass(this, ApiDemos.class);  
  136.         result.putExtra("com.example.android.apis.Path", path);  
  137.         return result;  
  138.     }  
  139.   
  140.     protected void addItem(List<Map> data, String name, Intent intent) {  
  141.         Map<String, Object> temp = new HashMap<String, Object>();  
  142.         temp.put("title", name);  
  143.         temp.put("intent", intent);  
  144.         data.add(temp);  
  145.     }  
  146.   
  147.     @Override  
  148.     protected void onListItemClick(ListView l, View v, int position, long id) {  
  149.         Map map = (Map) l.getItemAtPosition(position);  
  150.   
  151.         Intent intent = (Intent) map.get("intent");  
  152.         startActivity(intent);  
  153.     }  
  154.   
  155. }  

只注释了某些类的和方法的用法。逻辑算法段并未注释,不过,逻辑算法部分对我还是有点难度,呵呵……我看逻辑算法片段代码的时候经常用断点调试作为辅助,这样能更好理解程序的执行过程和某些值的变化过程。

我这里有一个别人将的Android1.5版本APIDemos梳理过的文件,方便大家查询某些功能的效果和源代码!

为了让大家透明下载,先把文件部分内容截图:

以下是连接地址:http://download.csdn.net/source/2704411

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值