Android API Demo程序框架

前言

       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";

	@Override
	public 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 {

	@Override
	public 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 {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.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。


 


 

 

 

 

转载于:https://my.oschina.net/fuyajun1983cn/blog/263880

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android_tv_metro是一款安卓版TV Metro框架和服务器APIAPI和数据结构专辑和显示项目:Metro风格是由两个元素构成专辑可以包含多张专辑和显示项目显示项目可以被定义为视频,游戏,应用程序,音乐等您可以从显示项目中删除你自己的游戏/应用/视频详细条目主页也被定义为专辑。API风格API描述http://host/v1/ns/type/?id=res_id NS:命名空间,资源类型类型:项目或项目列表ID:后端服务器系统的资源ID详细信息http://host/game(video/app)/item?id=12346 return item list专辑http://host/game(video/app)/album?id=6464 return album类别http://host/game(video/app)/category?id=123456 return album注意:专辑和类别接近同一概念。选项卡“应用程序/游戏”选项卡“视频”选项卡“视频类别”首页JSON定义首页JSON示例服务器API定义请看:https://github.com/XiaoMi/android_tv_metro/raw/master/server/TVMarketAPI.md首页显示数据{    "data": [        {            "items": [display items],            "images": {},            "name": "TAB 1",            "id": "recommend",            "type": "album",            "ns": "video"        },        {            "items": [display item],            "images": {},            "name": "TAB 2",            "id": "recommend",            "type": "album",            "ns": "video"        }    ] }显示项目:{    "target": {        "type": "item"    },    "images": {        "back": {            "url": "",            "ani": {},            "pos": {}        }    },    "name": "Display Name)",    "times": {        "updated": 1409202939,        "created": 1409202939    },    "_ui": {        "layout": {            "y": 2,            "x": 3,            "w": 1,            "h": 1        },        "type": "metro_cell_banner"    },    "id": "987722",    "type": "item",    "ns": "video" }专辑{ "data": [     {         "items": [display items],         "images": { },         "name":"game tab name",         "times": {             "updated": 0,             "created": 0         },         "_ui": {             "type": "metro"         },         "id": "recommend",         "type": "album",         "ns": "game"     },     {         "items": [display items],         "images": { },         "name": "game tab Name",         "times": {             "updated": 0,             "created": 0         },         "_ui": {             "type": "metro"         },         "id": "categories",         "type": "album",         "ns": "game"     },     {         "items": [dispay items],         "images": { },         "name": "video tab name",         "times": {             "updated": 0,             "created": 0         },         "_ui": {             "type": "metro"         },         "id": "recommend",         "type": "album",         "ns": "video"     },     {         "items": [display items],         "images": { },         "name": "video tab name",         "times": {             "updated": 0,             "created": 0         },         "_ui": {             "type": "metro"         },         "id": "categories",         "type": "album",         "ns": "video"     } ], "preload": {     "images": [] }, "update_time": 0 }显示项目{ "target": {     "type": "item" }, "images": {     "text": {         "url": "",         "ani": {},         "pos": {}     },     "icon": {         "url": "",         "ani": {},         "pos": {}     },     "back": {         "url": "http://xxx/fffff.png",         "ani": {},         "pos": {}     },     "spirit": {         "url": "",         "ani": {},         "pos": {}     } }, "name": "name", "times": {     "updated": 1404466152,     "created": 1404454443 }, "_ui": {     "type": "metro_cell_banner",     "layout": {         "y": 1,         "x": 1,         "w": 1,         "h": 2     } }, "id": "180", "type": "item", "ns": "game" }TV Metro库和APIandroid库:提供一个建立sw540dp metro布局的框架API:服务器API和数据结构。该框架能够帮助您轻松构建一个TV metro UI风格的应用程序。至于具体的业务数据定义,你需要自己处理。android库:用于专辑的RecommendCardView Card浏览GenericSubjectLoader Loader(选项卡是专辑的一个实例)如何集成Android库?你只需要继承MainActivity并执行选项卡装载。请参阅TVMetroSample应用如何运行自己的服务器?1.定义你的主页数据2.执行您的详细资料/列表API下载测试APK下载测试APK,你可以在Android平板或电视运行点击下载设计文档:https://github.com/XiaoMi/android_tv_metro/raw/master/design/app_api.ppt 标签:Android

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值