It is very practical to create a large number of test project management methods for Android

Source: Internet
Author: User

It is very practical to create a large number of test project management methods for Android

 

 

When learning Android, you may create a large number of test projects to test each component and learn it better.

 

However, in order to verify the learning effect, it is unnecessary and cumbersome.

Two management methods are introduced:

Manually add Activity information to an ActivityManager, and then fill the registered Activity information in a ListView when the Activity is started.

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 liAct;    private ActivityManager() {        liAct = new ArrayList();        register();            }    public static ActivityManager getInstance() {        return 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 act;        public ActivityInfo(Class 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;        }    }}


 

 

2. This method is visible in android apidemos and is easy to implement again.

When declaring an activity in the manifest file, add an android: label to each activity, use the label as the path, and add the category to the listview.

For example:

There are four acitables. The labels are a/B/c, a/d, e, f/g, h/I/j/k.

When we enable Acitvity, the page will display

A

E

F

H

,

When you click item a, only B is displayed. Then, click B to show c. Click c to open an Activity.

The retrieved activity is obtained by adding an intent-filter to each activity to be started.

The code for the main class is as follows:

 

/** * 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
 
  >();        Intent intent = new Intent(IntentAction.DEMO_ACTION, null);        intent.addCategory(Intent.CATEGORY_SAMPLE_CODE);        PackageManager pm = getPackageManager();        List
  
    liInfo = pm.queryIntentActivities(intent, 0);        Log.i(TAG, getData() +mCurPath);        if (null == liInfo) {            return datas;        }        Map entries = new HashMap
   
    ();        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.activityInfo.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
    
     ();        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
     
      > 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
      
        map = (Map
       
        )parent.getItemAtPosition(position); Intent intent = (Intent)map.get(intent); startActivity(intent); } };}
       
      
     
    
   
  
 

 

 

 

 

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.