Android online or simulator test activity's auxiliary activity sample development alone

Source: Internet
Author: User

The reason for remembering to write an activity-specific helper activity is that when you test online, idea or the specified run activity function in Android Studio is not available.

The following interface is specified in the IDE to run 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 "/>



The thing to do with the auxiliary activity is to use the auxiliary activity as the default activity for startup, which displays all activity as a ListView in the main interface, and initiates the activity to be tested by clicking the item item of the ListView.


1. Helper Activity code Example:

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&nbsp 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 () &NBSP;{&NBSP;&NBSp;      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);         // Launches the specified activity        startactivity (new intent (this, activity ));     }    /**     *  get all the activity class you want to test      *     *  @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;          &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;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);                          }    &nBsp;               } catch   (classnotfoundexception e)  {            &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.D (TAG,  "Class not  found activity  " + activityName + "  in package  " +  packagename);                     }                 }            }    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.D (tag,  "found  "  +  Includeactivities.size ()  +  "&NBSP;ACTIVITY&NBSP;LIST&NBSP;IS&NBSP;:"  + arrays.tostring ( Includeactivities.toarray ())); &Nbsp;           if  (null !=  Excludeactivities)  {                 includeactivities.removeall (excludeactivities);         &NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.D (TAG,   "last  "  + includeactivities.size ()  +  " activity  list  is : " + arrays.tostring (Includeactivities.toarray ()));         } catch  (packagemanager.namenotfoundexception e)  {    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.D (tag,  "Android system not found  package  " + packagename);        }         return  includeactivities;    }} 


in Androidmanifest.xml, the secondary activity is set to the activity that is started by default and can then be tested by selecting the activity that you want to start. The example can also be enhanced by adding some code, such as when the activity is started by entering a dialog box to carry data to the activated activity. The activity can be deleted or sufficient by default when the test is completed.


2. Configure 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. Debug log:

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 has 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 has 2 list is: [Class Secondriver.app.OneActivity, class Secondriver.app.TwoActivity]


from the log you can see that 3 activity classes were found, the auxiliary activity was removed, and 2 item items were displayed in the ListView.

4. Results

Here is not the result, relatively simple, see the code should be able to imagine.

This article is from the "Red Horse Red" blog, please be sure to keep this source http://aiilive.blog.51cto.com/1925756/1712943

Android online or simulator test activity's auxiliary activity sample development alone

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.