本篇文章為大家展示了如何進(jìn)行Android聯(lián)機(jī)及模擬器單獨(dú)測(cè)試Activity的輔助Activity示例開發(fā),內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
創(chuàng)新互聯(lián)建站從2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元開魯做網(wǎng)站,已為上家服務(wù),為開魯各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575
想起寫個(gè)單獨(dú)測(cè)試Activity的輔助Activity的原因是在聯(lián)機(jī)測(cè)試的時(shí)候IDEA或者Android Studio中的指定運(yùn)行Activity功能沒法使用了。
下面付IDE中指定運(yùn)行Activity的界面截圖:
輔助Activity要做的事情是將輔助Activity作為啟動(dòng)的默認(rèn)Activity,其主界面中以ListView的形式展示所有Activity,通過單擊ListView的Item項(xiàng)來啟動(dòng)要測(cè)試的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 ListmAllActivities; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initView(); mAllActivities = getActivities(this, this.getPackageName(), Arrays. 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); //啟動(dòng)指定的Activity startActivity(new Intent(this, activity)); } /** * 獲取所有要測(cè)試的Activity Class * * @param context * @param packageName * @param excludeActivities * @return */ public static List getActivities(Context context, String packageName, List excludeActivities) { final String TAG = "GET_ACTIVITY"; List 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設(shè)置為默認(rèn)啟動(dòng)的Activity,之后就可以通過選擇點(diǎn)擊要啟動(dòng)的Activity來進(jìn)行測(cè)試。該示例還可以通過添加一些代碼來增強(qiáng),比如啟動(dòng)Activity的時(shí)候可以通過輸入對(duì)話框的方式攜帶數(shù)據(jù)至啟動(dòng)的Activity。測(cè)試完成后刪除或者充足默認(rèn)啟動(dòng)的Activity即可。
2. 配置AndroidManifest.xml :
3. 調(diào)試日志:
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]
從日志中可以看到一共發(fā)現(xiàn)了3個(gè)Activity類,去除輔助的Activity,在ListView中講顯示2個(gè)Item項(xiàng)。
后期追加內(nèi)容:
4. 結(jié)果效果圖
新源碼參見附件:TestActivitiesActivity.java.txt
Item狀態(tài)變化Drawable文件內(nèi)容如下:
上述內(nèi)容就是如何進(jìn)行Android聯(lián)機(jī)及模擬器單獨(dú)測(cè)試Activity的輔助Activity示例開發(fā),你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。