在com.example.android.aips包里有两个类文件:ApiDemos和ApiDemosApplicatio,这两个类主要用来构建整个Demo。
ApiDemos继承自android.app.ListActivity,显然是列表视图
ApiDemosApplicatio继承自android.app.Application,是用来控制App整体的
-------------------------------分割线--------------------------------
ListActivity 就是用来展示绑定了例如数组和数据游标(Cursor)数据的列表项,并且可以给项绑定事件。详细参考
[url]http://developer.android.com/reference/android/app/ListActivity.html[/url]
Application 是一个用于获取全局应用状态的顶层类(Base Class) 复写此类可以定制application,本例用法如:<application android:name="ApiDemosApplication"
详情参考
[url]http://developer.android.com/reference/android/app/Application.html[/url]
-------------------------------分割线--------------------------------
下面来看com.example.android.aips.ApiDemos类:
首先重写onCreate方法,代码如下:
ApiDemos继承自android.app.ListActivity,显然是列表视图
ApiDemosApplicatio继承自android.app.Application,是用来控制App整体的
-------------------------------分割线--------------------------------
ListActivity 就是用来展示绑定了例如数组和数据游标(Cursor)数据的列表项,并且可以给项绑定事件。详细参考
[url]http://developer.android.com/reference/android/app/ListActivity.html[/url]
Application 是一个用于获取全局应用状态的顶层类(Base Class) 复写此类可以定制application,本例用法如:<application android:name="ApiDemosApplication"
详情参考
[url]http://developer.android.com/reference/android/app/Application.html[/url]
-------------------------------分割线--------------------------------
下面来看com.example.android.aips.ApiDemos类:
首先重写onCreate方法,代码如下:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* 获取启动Intent
* 启动Intent:任何Activity都是由Intent 激活,所以本人习惯叫‘启动 Intent’
*/
Intent intent = getIntent();
/**
* 获取Path 这个path在此项目中很重要,后面用它来填充Intent
* 此Demo设计之巧也在这里,此DEMO有几层,例如打开是(App|Content…Views)然后在
* 打开App是(Activity|Alarm…Menu) 在打开Activity是(Animation…Wallpaper)
* 这三层是依次连接的例如App—>ActivityAnimation 仔细看Animation这个
* Activity的android:label是App/Activity/Animation,到这里再结合代码大家估计
* 都明白此Demo怎么组织这三个层次的Activity的了:首次运行此Demo时path是空值,而 * 它的值则依次+“/…”而且最下面层一个是启动Activity 之前的是browseActivity 具* 体可细读getData方法
*/
String path = intent.getStringExtra("com.example.android.apis.Path");
if (path == null) {
path = "";
}
/**
* SimpleAdapter ListView的适配器
*/
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>>();
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;
}
String[] prefixPath;//前缀路径
String prefixWithSlash = prefix;//前缀带路径
if (prefix.equals("")) {
prefixPath = null;
} else {
prefixPath = prefix.split("/");
prefixWithSlash = prefix + "/";
}
int len = list.size();
Map<String, Boolean> entries = new HashMap<String, Boolean>();
for (int i = 0; i < len; i++) {
ResolveInfo info = list.get(i);
Log.i("TEST",info.toString());
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);
}
}
}
}
Collections.sort(myData, sDisplayNameComparator);
return myData;
}
/**
*
* @param data
* @param name
* @param intent
*/
protected void addItem(List<Map<String, Object>> data, String name,Intent intent) {
//name = 'App'
Map<String, Object> temp = new HashMap<String, Object>();
temp.put("title", name);
temp.put("intent", intent);
data.add(temp);
}
/**
*
* @param pkg 生成intent
* @param componentName
* @return
*/
protected Intent activityIntent(String pkg, String componentName) {
Intent result = new Intent();
result.setClassName(pkg, componentName);
return result;
}
/**
*
* @param path
* @return
*/
protected Intent browseIntent(String path) {
Intent result = new Intent();
result.setClass(this, ApiDemos.class);
result.putExtra("com.xunlei.android.apis.Path", path);
return result;
}
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"));
}
};
@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);
DialogPreference dp;
Dialog d;
}
本文介绍了Android的API Demos中com.example.android.aips包的ApiDemos和ApiDemosApplication类。ApiDemos作为ListActivity用于显示列表视图,ApiDemosApplication作为Application基类用于全局应用状态管理。内容包括ListActivity的作用及其参考链接,以及Application的使用和自定义方式。
518

被折叠的 条评论
为什么被折叠?



