Android 建立大量的test project 的管理方法,非常實用

來源:互聯網
上載者:User

標籤:android   manager   demo   



在學習安卓時,可能會建立大量的test project 來測試各個組件,以更好的學習它。

但是為了驗證學習每一種效果,會非常沒必要,而且特別繁瑣。

現在介紹的是兩種管理方法:

一 通過手工的將 Activity 資訊添加到一個ActivityManager中,然後在啟動Activity將註冊的Activity資訊,填充到一個ListView中。

主要有兩個類。

ActivityManager, ActivityManager$ActivityInfo.

代碼:

/** * 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 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;        }    }}



二 這個方法是在看 android apidemos是看到的,覺得較為方便,就重新實現了一遍。

 在manifest檔案中聲明activity時,給每個activity添加一個android:label,將這個label做為路徑,分類添加到listview中。

例如:

有四個acitvity,label分別是 a/b/c, a/d, e, f/g, h/i/j/k。

那麼當我們開啟啟動Acitvity時,介面會顯示

a

e

f

h

當點擊 item a時,只會顯示b,然後點擊b,顯示c,點擊c,開啟一個Activity。

我們遍曆到的activity是通過給每個要啟動的activity添加一個intent-filter來擷取到的。

下面是主類的代碼:

/** * 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.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<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>> 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);        }    };}

下面是我的完整工程路徑:




Android 建立大量的test project 的管理方法,非常實用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.