前言

android API Demo程序主要演示了Android常见API的使用方法, 本文将该程序的框架提取出来,以作为自己研究和学习Android编程的一个示例收集框架,即将所有自己研究过的例子按照分类收集起来, 方便日后查找。

正文

下面只贴出框架相关的文件内容, 最后说明如果将自己的例子加入到框架中。

框架的工程的源码目录结构如下:

核心包

com.fyj.demo包含三个文件, Globals.java MyAppDemoActivity.java MyAppDemoApplication.java, 文件的内容如下:

Globals.java

package com.fyj.demo;public class Globals {public static final String CATEGORY_MYAPP_DEMO = "android.intent.category.MYAPP_DEMO";}

该文件主要定义一些全局变量,如上所示,我们定义了自己的CATEGORY。

MyAppDemoApplication.java

package com.fyj.demo;import android.app.Application;import android.util.Log;public class MyAppDemoApplication extends Application {private static final String TAG = "MyAppDemoApplication";@Overridepublic void onCreate() {Log.i(TAG, "My App Demo is running...");}}


此类可加入一些全局初始化的代码。

MyAppDemoActivity.java

package com.fyj.demo;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;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;public class MyAppDemoActivity extends ListActivity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Intent intent = getIntent();String path = intent.getStringExtra("com.fyj.demo.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<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(Globals.CATEGORY_MYAPP_DEMO);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);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;}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"));}};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, MyAppDemoActivity.class);result.putExtra("com.fyj.demo.Path", path);return result;}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);}@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);}}


该类是框架的主活动类,通过它来启动我们之后添加到框架中的一些示例。

扩展包

除核心包之后的代码都是按照示例的分类放置在不同的扩展包中,作为例子,本文创建了一个扩展包为com.fyj.demo.app

在此包中,我们创建了一个例子,名为HelloWorld.java, 它的内容如下:

package com.fyj.demo.app;import android.app.Activity;import android.os.Bundle;import com.fyj.demo.R;public class HelloWorld extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.hello);}}


下面进入res目录下,这里我们主要关注的是layout, values子目录,其中layout是一些布局文件,values目录下的strings.xml定义了示例应用程序中的一些字符串, 这里我们主要看下strings.xml文件的内容:

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="hello">Hello World, MyAppDemoActivity!</string>    <string name="app_name">MyAppDemo</string>            <string name="activity_hello_world">App/Activity/<b>Hello <i>World</i></b></string></resources>

其中,在定义示例程序的标题字符串的时候,要按照上述的格式定义, 上述名称代表HelloWorld示例程序被归在App分类下的Activity子分类中,多少个'/'代表有多少个分类层次。

最后,我们看下AndroidManifest.xml文件的内容:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.fyj.demo"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="15" />    <application        android:name="MyAppDemoApplication"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <activity            android:name=".MyAppDemoActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.DEFAULT" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <!-- Activity Samples -->        <activity            android:name=".app.HelloWorld"            android:label="@string/activity_hello_world" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.MYAPP_DEMO" />            </intent-filter>        </activity>    </application></manifest>


注意,在示例的Activity中, category都要设置为android.intent.category.MYAPP_DEMO。



更多相关文章

  1. Google Espresso:一种用于云中Android(安卓)UI的快速自动化测试框
  2. 如何分析和研究Log文件(转)
  3. Android(安卓)ApiClound 自定义模块未绑定解决方案
  4. Kotlin For Android简介——有趣实用的功能
  5. 按下电源键到启动Home应用过程详解(一)
  6. iOS 开发者的 Android(安卓)第一课
  7. Android用无线调试真机及在Eclipse的File Explorer中访问真机的D
  8. android 如何动态的加载类----app插件技术
  9. Android、iOS和Java通用的AES128加密解密示例代码

随机推荐

  1. 采用XMPP协议实现Android推送
  2. 系出名门Android(1) - 在 Windows 下搭建
  3. Android应用程序与SurfaceFlinger服务的
  4. androidのEditTex详细使用
  5. Android的系统架构
  6. Android开发屏幕适配
  7. Android联系人数据库全解析(2)
  8. android ndk log
  9. Android绘制优化----系统显示原理
  10. Android学习笔记:Android消息处理机制之Ha