您好,登錄后才能下訂單哦!
本篇文章為大家展示了如何進行Android聯機及模擬器單獨測試Activity的輔助Activity示例開發,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
想起寫個單獨測試Activity的輔助Activity的原因是在聯機測試的時候IDEA或者Android Studio中的指定運行Activity功能沒法使用了。
下面付IDE中指定運行Activity的界面截圖:
輔助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. 結果效果圖
新源碼參見附件:TestActivitiesActivity.java.txt
Item狀態變化Drawable文件內容如下:
上述內容就是如何進行Android聯機及模擬器單獨測試Activity的輔助Activity示例開發,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。