Android builds a lot of test project management methods, very practical

Source: Internet
Author: User


As you learn about Android, you might build a lot of test project that tests each component to better learn it.

But in order to verify the learning of each effect, it is very unnecessary, and particularly cumbersome.

Here are two ways to manage it:

One fills in a ListView by manually adding activity information to a activitymanager, and then starting the activity information to be registered.

There are two main classes.

Activitymanager, Activitymanager$activityinfo.

Code:

/** * Created by MJZ on 14-12-20.    */public class Activitymanager {private static Activitymanager single = new Activitymanager ();    Private list<activityinfo> liact;        Private Activitymanager () {liact = new arraylist<activityinfo> ();            Register ();    } public static Activitymanager getinstance () {return a single; } private void Register () {Add (com.majunzhe.demos.te_and1. Mainactivity.class, Com.majunzhe.demos.te_and1.        MainActivity.class.getName ());        Add (Mainactivity.class, MainActivity.class.getName ());        Add (Mytabactivity.class, "Bottom tabs");        Add (Animmainactivity.class, "Animation");        Add (Arcs.class, "Arcs");        Add (Sweep.class, "Sweep");        Add (Game.class, "Game");    Add (Tedemos.class, "Entry Manager");        } public void Add (Class Act, String tag) {Activityinfo actinfo = new Activityinfo (act, TAG);    Liact.add (Actinfo); } public Activityinfo get (int inDex) {return liact.get (index);    } public void Remove (Activityinfo Act) {liact.remove (ACT);    } public List GetAll () {return liact;        } public static class Activityinfo {String mtag;        Class<activity> Act;            Public Activityinfo (Class<activity> Act, String tag) {this.act = act;        This.mtag = tag;        Public Activity getactivity () {return null;        } public String Gettag () {return mtag;            } public void Start (Activity home) {Intent Intent = new Intent ();            Intent.setclass (home, ACT);        Home.startactivity (Intent);        } @Override Public String toString () {return mtag; }    }}



Two this method is to see the Android Apidemos is to see, feel more convenient, on re-realized again.

When the activity is declared in the manifest file, add a android:label to each activity, make the label a path, and add the category to the ListView.

For example:

There are four Acitvity,label, namely A/B/C, A/D, E, f/g, h/i/j/k.

So when we open the boot acitvity, the interface will show

A

E

F

H

When you click Item A, only B is displayed, then B is clicked, C is displayed, click C, and an activity is opened.

The activity we traverse is obtained by adding a intent-filter to each activity to be started.

The following is the code for the main class:

/** * Created by MJZ on 15-1-10.    */public class Tedemos extends Activity {final static String TAG = "Tedemos";    Final static String Package_path = "Com.majunzhe.Path";    Private String Mcurpath;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_home);        Mcurpath = Getintent (). Getstringextra (Package_path); Mcurpath = Mcurpath = = null?        "": Mcurpath;        ListView Liview = (ListView) Findviewbyid (R.id.entry_container);  Simpleadapter adapter = new Myadapter (this, GetData (), R.layout.entry_list_item, new string[]{"title"}, New        INT[]{R.ID.ENTRY_TV});        Liview.setadapter (adapter);    Liview.setonitemclicklistener (listener);        } private list GetData () {list datas = new arraylist<map<string, object>> ();        Intent Intent = new Intent (intentaction.demo_action, NULL); Intent.addcategory (Intent.category_sample_CODE);        Packagemanager pm = Getpackagemanager ();        list<resolveinfo> Liinfo = pm.queryintentactivities (Intent, 0);        LOG.I (TAG, "GetData ()" +mcurpath);        if (null = = Liinfo) {return datas;        } MAP entries = new hashmap<string, object> ();            for (int i = 0; i < liinfo.size (); ++i) {ResolveInfo info = liinfo.get (i);            String label = Info.loadlabel (PM). toString ();            if (false = = Label.startswith (Mcurpath)) {continue;            } String paths[] = Label.replace (Mcurpath, ""). Split ("/");            String Nextlabel = paths[0]; if (paths.length = = 1) {AddItem (datas, Nextlabel, Activityintent (Info.activityInfo.packageName, Info.activ            Ityinfo.name));                } else if (Entries.get (nextlabel) = = null) {Entries.put (Nextlabel, true);            AddItem (Datas, Nextlabel, browseintent (Mcurpath + "/" + nextlabel+ "/"));   }     } return datas;        } private void AddItem (List datas, String name, Intent Intent) {map map = new hashmap<string, object> ();        Map.put ("title", name);        Map.put ("intent", intent);    Datas.add (map);        } Private Intent Activityintent (String pkg, string cls) {Intent Intent = new Intent ();        Intent.setclassname (pkg, CLS);    return intent;        Private Intent browseintent (String path) {if (Path.startswith ("/")) {path = path.substring (1);        } Intent Intent = new Intent ();        Intent.setclass (this, tedemos.class);        Intent.putextra (Package_path, PATH);    return intent; } class Myadapter extends Simpleadapter {public Myadapter (context context, list<map<string, Object>&gt ;        data, int resource, string[] from, int[] to) {Super (context, data, resource, from, to); }} private Adapterview.onitemclicklistener listener = new ADAPTERVIEW.ONITEMCLICklistener () {@Override public void Onitemclick (adapterview<?> parent, view view, int position, long            ID) {map<string, object> Map = (map<string, object>) parent.getitematposition (position);            Intent Intent = (Intent) map.get ("Intent");        StartActivity (Intent); }    };}

Here's my full project path:




Android builds a lot of test project management methods, very practical

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.