Android 建立大量的test project 的管理方法,非常实用
在学习安卓时,可能会建立大量的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); } }; }
下面是我的完整工程路径下载地址:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。