1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
**
* Intents测试列表类
* @author 飞雪无情
* @since 2011-3-14
*/
ublic class IntentsTestList extends ListActivity {
private String ACTION_VIEW="com.flysnow.intent.ACTION_VIEW";
private String CATEGORY_MAN="com.flysnow.intent.CATEGORY_MAN";
private String CATEGORY_SHOP="com.flysnow.intent.CATEGORY_SHOP";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//定义ListAdapter
setListAdapter(new SimpleAdapter(this, getData(),
android.R.layout.simple_list_item_1, new String[] { "title" },new int[] {android.R.id.text1}));
getListView().setTextFilterEnabled(true);
filter.addCategory(CATEGORY_MAN);
//注册一个广播
super.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "该Broadcast的Intent Filter设置了Category和Action", Toast.LENGTH_SHORT).show();
}
},filter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent intent=(Intent)getData().get(position).get("intent");
Boolean isActivity=(Boolean)getData().get(position).get("isActivity");
if(isActivity){
startActivity(intent);
}else{
sendBroadcast(intent);
}
}
/**
* 返回ListView需要的数据
* @return ListView需要的数据
*/
private List<map<string,object>> getData() {
List<map<string,object>> data=new ArrayList<map<string,object>>();
addItem(data, "1个Category检测-Activity", new Intent(ACTION_VIEW).addCategory(CATEGORY_MAN),true);
addItem(data, "1个Category检测-Broadcast", new Intent(ACTION_VIEW).addCategory(CATEGORY_MAN),false);
addItem(data, "2个Category检测-Activity", new Intent(ACTION_VIEW).addCategory(CATEGORY_MAN).addCategory(CATEGORY_SHOP),true);
return data;
}
/**
* 给ListView添加数据
* @param data 存储数据的List
* @param name 要显示的Title
* @param intent 单击某一项时要启动的Activity
* @param isActivity 启动的是否是Activity,true是,false为广播
*/
private void addItem(List<map<string,object>> data, String name, Intent intent,boolean isActivity) {
Map<string, object=""> temp = new HashMap<string, object="">();
temp.put("title", name);
temp.put("intent", intent);
temp.put("isActivity", isActivity);
data.add(temp);
}
private IntentFilter filter=new IntentFilter(ACTION_VIEW);
}
|