想起写个单独测试Activity的辅助Activity的原因是在联机测试的时候IDEA或者Android Studio中的指定运行Activity功能没法使用了。
下面付IDE中指定运行Activity的界面截图:
辅助Activity要做的事情是将辅助Activity作为启动的默认Activity,其主界面中以ListView的形式展示所有Activity,通过单击ListView的Item项来启动要测试的Activity。
1. 辅助Activity代码示例:
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
package
secondriver.app;
import
android.app.Activity;
import
android.content.Context;
import
android.content.Intent;
import
android.content.pm.ActivityInfo;
import
android.content.pm.PackageInfo;
import
android.content.pm.PackageManager;
import
android.os.Bundle;
import
android.util.Log;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.AdapterView;
import
android.widget.ArrayAdapter;
import
android.widget.LinearLayout;
import
android.widget.ListView;
import
java.util.ArrayList;
import
java.util.Arrays;
import
java.util.List;
/**
* Created by Broncho on 2015/11/15.
*/
public
class
TestActivitiesActivity
extends
Activity
implements
AdapterView.OnItemClickListener {
private
LinearLayout mRootView;
private
ListView mListView;
private
ArrayAdapter mAdapter;
private
List<Class> mAllActivities;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
initView();
mAllActivities = getActivities(
this
,
this
.getPackageName(), Arrays.<Class>asList(
this
.getClass()));
mAdapter =
new
ArrayAdapter(
this
, android.R.layout.simple_list_item_1, android.R.id.text1, mAllActivities);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(
this
);
}
private
void
initView() {
mRootView =
new
LinearLayout(
this
);
mListView =
new
ListView(
this
);
mListView.setId(android.R.id.list);
mRootView.addView(mListView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
setContentView(mRootView);
}
@Override
public
void
onItemClick(AdapterView<?> parent, View view,
int
position,
long
id) {
Class activity = mAllActivities.get(position);
//启动指定的Activity
startActivity(
new
Intent(
this
, activity));
}
/**
* 获取所有要测试的Activity Class
*
* @param context
* @param packageName
* @param excludeActivities
* @return
*/
public
static
List<Class> getActivities(Context context, String packageName, List<Class> excludeActivities) {
final
String TAG =
"GET_ACTIVITY"
;
List<Class> includeActivities =
new
ArrayList<>();
try
{
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
if
(packageInfo.activities !=
null
) {
ActivityInfo[] activityInfos = packageInfo.activities;
Log.d(TAG,
"Find "
+ activityInfos.length +
" activity in AndroidManifest.xml."
);
for
(ActivityInfo activityInfo : activityInfos) {
Class activityClass;
String activityName = activityInfo.name;
try
{
activityClass = Class.forName(activityName);
if
(Activity.
class
.isAssignableFrom(activityClass)) {
includeActivities.add(activityClass);
}
}
catch
(ClassNotFoundException e) {
Log.d(TAG,
"Class not found activity "
+ activityName +
" in package "
+ packageName);
}
}
}
Log.d(TAG,
"Found "
+ includeActivities.size() +
" activity list is :"
+ Arrays.toString(includeActivities.toArray()));
if
(
null
!= excludeActivities) {
includeActivities.removeAll(excludeActivities);
}
Log.d(TAG,
"Last "
+ includeActivities.size() +
" activity list is :"
+ Arrays.toString(includeActivities.toArray()));
}
catch
(PackageManager.NameNotFoundException e) {
Log.d(TAG,
"Android system not found package "
+ packageName);
}
return
includeActivities;
}
}
|
在AndroidManifest.xml中讲辅助Activity设置为默认启动的Activity,之后就可以通过选择点击要启动的Activity来进行测试。该示例还可以通过添加一些代码来增强,比如启动Activity的时候可以通过输入对话框的方式携带数据至启动的Activity。测试完成后删除或者充足默认启动的Activity即可。
2. 配置AndroidManifest.xml :
1
2
3
4
5
6
7
8
|
<
activity
android:name
=
".TestActivitiesActivity"
>
<
intent-filter
>
<
action
android:name
=
"android.intent.action.MAIN"
/>
<
category
android:name
=
"android.intent.category.LAUNCHER"
/>
</
intent-filter
>
</
activity
>
<
activity
android:name
=
".OneActivity"
/>
<
activity
android:name
=
".TwoActivity"
/>
|
3. 调试日志:
1
2
3
|
11
-
15
20
:
07
:
31.899
10665
-
10665
/secondriver.app D/GET_ACTIVITY: Find
3
activity in AndroidManifest.xml.
11
-
15
20
:
07
:
31.899
10665
-
10665
/secondriver.app D/GET_ACTIVITY: Found activity have
3
list is :[
class
secondriver.app.TestActivitiesActivity,
class
secondriver.app.OneActivity,
class
secondriver.app.TwoActivity]
11
-
15
20
:
07
:
31.899
10665
-
10665
/secondriver.app D/GET_ACTIVITY: Last activity have
2
list is :[
class
secondriver.app.OneActivity,
class
secondriver.app.TwoActivity]
|
从日志中可以看到一共发现了3个Activity类,去除辅助的Activity,在ListView中讲显示2个Item项。
后期追加内容:
4. 结果效果图
新源码参见附件:TestActivitiesActivity.java.txt
Item状态变化Drawable文件内容如下:
本文转自 secondriver 51CTO博客,原文链接:http://blog.51cto.com/aiilive/1712943,如需转载请自行联系原作者