大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本篇文章为大家展示了如何进行Android联机及模拟器单独测试Activity的辅助Activity示例开发,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
创新互联建站从2013年开始,是专业互联网技术服务公司,拥有项目成都网站设计、做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元开鲁做网站,已为上家服务,为开鲁各地企业和个人服务,联系电话:18980820575
想起写个单独测试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 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); //启动指定的Activity startActivity(new Intent(this, activity)); } /** * 获取所有要测试的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设置为默认启动的Activity,之后就可以通过选择点击要启动的Activity来进行测试。该示例还可以通过添加一些代码来增强,比如启动Activity的时候可以通过输入对话框的方式携带数据至启动的Activity。测试完成后删除或者充足默认启动的Activity即可。
2. 配置AndroidManifest.xml :
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示例开发,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。