Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to develop the auxiliary Activity example of Android online and simulator testing Activity separately

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to carry out Android online and simulator separate test Activity auxiliary Activity sample development, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.

The reason I want to write a secondary Activity for testing Activity separately is that the specified running Activity function in IDEA or Android Studio is not available during online testing.

The following is a screenshot of the interface specified to run Activity in IDE:

What the secondary Activity does is use the secondary Activity as the default Activity to launch, display all the Activity in the form of ListView in its main interface, and start the Activity to be tested by clicking the Item tab of the ListView.

1. Example of secondary Activity code:

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 mAllActivities; @ 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); / / launch the specified Activity startActivity (new Intent (this, activity)) } / * get all 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;}}

In AndroidManifest.xml, the secondary Activity is set to the Activity that starts by default, and then you can test it by selecting the Activity you want to start. The example can also be enhanced by adding some code, such as when starting Activity, you can bring data to the launched Activity by entering a dialog box. Delete or have enough Activity started by default after the test is completed.

two。 Configure AndroidManifest.xml:

3. Debug log:

11-15 20 activity in AndroidManifest.xml.11 07 10665/secondriver.app D/GET_ACTIVITY 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 20 activity in AndroidManifest.xml.11 07 list is 31.899 10665-10665/secondriver.app D/GET_ACTIVITY: Last activity have 2 list is: [class secondriver.app.OneActivity, class secondriver.app.TwoActivity]

You can see from the log that a total of 3 Activity classes have been found, removing the auxiliary Activity, and showing 2 Item entries in the ListView.

Additional content in later stage:

4. Result effect diagram

For new source code, please see attachment: TestActivitiesActivity.java.txt

The contents of the Item status change Drawable file are as follows:

The above content is how to carry out Android online and simulator separate test Activity auxiliary Activity sample development, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report