Android API

来源:互联网 发布:汽车修理厂软件 编辑:程序博客网 时间:2024/05/18 03:40

【Android API Demos】API Demo 初探

2137人阅读 评论(0)收藏 举报
apiandroidlistviewstringpathnull

首先声明,该文章为自己学习笔记,仅供参考,不保证所有文字均描述得当,欢迎指出不足和错误之处,再此感谢您的关注和阅读。如果有需要代码的可以留言,和我联系。

1.准备工作

首先,导入API Demos。File->new->project->android project->Creat project from existing source 选择API Demos.

导入的文件可以在sdk的文件里面找到,如XXX/android-8/samples.也可以直接下载如http://dl-ssl.google.com/android/repository/samples-2.2_r01-linux.zip

2.开始

根据那个图我们可以看见,API Demos把常用的一些功能都集成在这个里面了,所以我们只要一步一步来,就可以掌握的差不多了。

先看下AndroidMainifest.xml

[java] view plaincopyprint?
  1. <activity android:name="ApiDemos">  
  2.             <intent-filter>  
  3.                 <action android:name="android.intent.action.MAIN" />  
  4.                 <category android:name="android.intent.category.DEFAULT" />  
  5.                 <category android:name="android.intent.category.LAUNCHER" />  
  6.             </intent-filter>  
  7.  </activity>  

发现首先启动的是ApiDemos,所以我们的目光集中在com.example.android.apis/ApiDemos.java里面

[java] view plaincopyprint?
  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. /* 
  37.  * 主要是通过对Activity的label信息进行分析,从而将Activity进行分类并通过ListView显示出来。 
  38.  * 例如<activity android:name=".app.HelloWorld" android:label="@string/activity_hello_world">的label为 
  39.  * App/Activity/Hello World,程序运行的结果是一级目录含有App选项,点击App出现的二级目录含有Activity选项, 
  40.  * 点击Activity出现的三级目录含有Hello World选项,点击Hello World会出现Hello World这个Activity运行的结果 
  41.  */  
  42. public class ApiDemos extends ListActivity {  
  43.   
  44.     @Override  
  45.     public void onCreate(Bundle savedInstanceState) {  
  46.         super.onCreate(savedInstanceState);  
  47.           
  48.         Intent intent = getIntent();  
  49.         /* 
  50.          * 当程序启动要显示一级目录(App, Content, ...)时,Intent对象里绑定的附加信息path为空 
  51.          * 当点击一级目录选项进入二级目录(例如App下的Activity, Alarm, ...)时,path就不为空了(例如为App) 
  52.          */  
  53.         String path = intent.getStringExtra("com.example.android.apis.Path");  
  54.           
  55.         if (path == null) {  
  56.             path = "";  
  57.         }  
  58.   
  59.         // 通过adapter为当前ListActivity传递数据  
  60.         setListAdapter(new SimpleAdapter(this, getData(path),  
  61.                 android.R.layout.simple_list_item_1, new String[] { "title" },  
  62.                 new int[] { android.R.id.text1 }));  
  63.         // 允许当前ListView可以根据用户输入的值进行过滤  
  64.         getListView().setTextFilterEnabled(true);  
  65.     }  
  66.   
  67.     /* 
  68.      * 获取应用列表,包括一级目录,二级目录,等等 
  69.      */  
  70.     protected List getData(String prefix) {  
  71.         List<Map> myData = new ArrayList<Map>();  
  72.   
  73.         /* 
  74.          * 获取在AndroidManifest.xml文件中的Intent Filter里配置了 
  75.          * Intent.ACTION_MAIN和Intent.CATEGORY_SAMPLE_CODE的所有Activity 
  76.          */  
  77.         Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);  
  78.         mainIntent.addCategory(Intent.CATEGORY_SAMPLE_CODE);  
  79.         PackageManager pm = getPackageManager();  
  80.         List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);  
  81.   
  82.         if (null == list)  
  83.             return myData;  
  84.   
  85.         String[] prefixPath;  
  86.           
  87.         // 当要显示一级目录时,prefix为空字符串;当点击App进入要显示二级目录时,prefix为App;  
  88.         if (prefix.equals("")) {  
  89.             prefixPath = null;  
  90.         } else {  
  91.             prefixPath = prefix.split("/");  
  92.         }  
  93.           
  94.         int len = list.size();  
  95.           
  96.         Map<String, Boolean> entries = new HashMap<String, Boolean>();  
  97.   
  98.         for (int i = 0; i < len; i++) {  
  99.             ResolveInfo info = list.get(i);  
  100.             // 获取Activity的label信息,例如App/Activity/Hello World  
  101.             CharSequence labelSeq = info.loadLabel(pm);  
  102.             String label = labelSeq != null  
  103.                     ? labelSeq.toString()  
  104.                     : info.activityInfo.name;  
  105.               
  106.             if (prefix.length() == 0 || label.startsWith(prefix)) {  
  107.                 // 例如labelPath = [App, Activity, Hello World]  
  108.                 String[] labelPath = label.split("/");  
  109.                 // 例如要显示一级目录时nextLabel = "App",要显示二级目录时,nextLabel = "Activity"  
  110.                 String nextLabel = prefixPath == null ? labelPath[0] : labelPath[prefixPath.length];  
  111.                 // 当要显示的label为Activity而非目录时,需要为label绑定跳转到相关Activity的Intent  
  112.                 if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) {  
  113.                     addItem(myData, nextLabel, activityIntent(  
  114.                             info.activityInfo.applicationInfo.packageName,  
  115.                             info.activityInfo.name));  
  116.                 } else {  
  117.                     if (entries.get(nextLabel) == null) {  
  118.                         addItem(myData, nextLabel, browseIntent(prefix.equals("") ? nextLabel : prefix + "/" + nextLabel));  
  119.                         entries.put(nextLabel, true);  
  120.                     }  
  121.                 }  
  122.             }  
  123.         }  
  124.   
  125.         // 排序   
  126.         Collections.sort(myData, sDisplayNameComparator);  
  127.           
  128.         return myData;  
  129.     }  
  130.   
  131.     private final static Comparator<Map> sDisplayNameComparator = new Comparator<Map>() {  
  132.         private final Collator   collator = Collator.getInstance();  
  133.   
  134.         public int compare(Map map1, Map map2) {  
  135.             return collator.compare(map1.get("title"), map2.get("title"));  
  136.         }  
  137.     };  
  138.   
  139.     /* 
  140.      * 为label绑定跳转到相关Activity的Intent 
  141.      */  
  142.     protected Intent activityIntent(String pkg, String componentName) {  
  143.         Intent result = new Intent();  
  144.         result.setClassName(pkg, componentName);  
  145.         return result;  
  146.     }  
  147.       
  148.     /* 
  149.      * 为label绑定跳转到当前Activity的Intent,附加信息path为当前目录 
  150.      * 例如为Activity这个label绑定的Intent中附加值为App/Activity 
  151.      */  
  152.     protected Intent browseIntent(String path) {  
  153.         Intent result = new Intent();  
  154.         result.setClass(this, ApiDemos.class);  
  155.         result.putExtra("com.example.android.apis.Path", path);  
  156.         return result;  
  157.     }  
  158.   
  159.     /* 
  160.      * 将符合条件的选项加入List中 
  161.      */  
  162.     protected void addItem(List<Map> data, String name, Intent intent) {  
  163.         Map<String, Object> temp = new HashMap<String, Object>();  
  164.         temp.put("title", name);  
  165.         temp.put("intent", intent);  
  166.         data.add(temp);  
  167.     }  
  168.   
  169.     @Override  
  170.     protected void onListItemClick(ListView l, View v, int position, long id) {  
  171.         Map map = (Map) l.getItemAtPosition(position);  
  172.   
  173.         Intent intent = (Intent) map.get("intent");  
  174.         startActivity(intent);  
  175.     }  
  176.   
  177. }  

上面代码内容参考 http://hunter2014.iteye.com/blog/778160

看了以后发现是不是真心的理解不了,没关系,继续往下看,我会从最简单的ListView开始一起学习,最后再回过头来看看这个程序,或许过上几天,几周,就很轻松了。下一天,准备从ListView开始学起。



0 0
原创粉丝点击