標籤:androidactivity測試
想起寫個單獨測試Activity的輔助Activity的原因是在聯機測試的時候IDEA或者Android Studio中的指定運行Activity功能沒法使用了。
下面付IDE中指定運行Activity的介面:
650) this.width=650;" src="http://s1.51cto.com/wyfs02/M00/76/06/wKiom1ZId3ezyUiNAAEPbwkDMQM234.png" title="androidstudio_or_idea_android_debug.png" width="680" height="576" border="0" hspace="0" vspace="0" style="width:680px;height:576px;" alt="wKiom1ZId3ezyUiNAAEPbwkDMQM234.png" />
輔助Activity要做的事情是將輔助Activity作為啟動的預設Activity,其主介面中以ListView的形式展示所有Activity,通過單擊ListView的Item項來啟動要測試的Activity。
1. 輔助Activity程式碼範例:
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 :
<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. 調試日誌:
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. 結果
這裡就不做結果的,比較簡單,看代碼應該可以想象出。
本文出自 “野馬紅塵” 部落格,請務必保留此出處http://aiilive.blog.51cto.com/1925756/1712943
Android聯機或者模擬器單獨測試Activity的輔助Activity樣本開發