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);
        }
    };
}

下面是我的完整工程路径下载地址:

时间: 2024-08-07 08:38:37

Android 建立大量的test project 的管理方法,非常实用的相关文章

Android 之 Window、WindowManager 与窗口管理

其实在android中真正展示给用户的是window和view,activity在android中所其的作用主要是处理一些逻辑问题,比如生命周期的管理.建立窗口等.在android中,窗口的管理还是比较重要的一块,因为他直接负责把内容展示给用户,并和用户进行交互.响应用户的输入等. 在讲窗口管理时,有必要先说下ViewManager这个接口,这个接口主要有以下的实现子接口和实现类,分别是:WindowManager和ViewGroup里面还有三个重要的方法: * addView(); * upd

Android开发时提示Your project contains error(s),please fix them be

有次在使用eclipse写好Android的代码,代码没有报错.然后 想在AVD中运行测试时,总是会弹出错误框,提示信息为: “Your project contains error(s),please fix them before running your application.” 不管是重启AVD 重启eclipse 甚至创建一个新的Android工程项目都不能运行 有几种原因: 1.在不同的电脑下开发,而且文件存放路径不同,错误的主要原因是  “.classpath”  文件的载三方类

Android提供的系统服务之--WindowManager(窗口管理服务)

Android提供的系统服务之--WindowManager(窗口管理服务) --转载请注明出处:coder-pig 本节引言: 本节我们来探讨下这个Android系统服务中的WindowManager(窗口管理服务), 他是显示View的最底层,好像我们的Actviity和Dialog,以及Toast的底层实现都用到 这个WindowManager,他是全局的!核心其实就是WindowManager调用addView, removeView,updateViewLayout这几个方法来显示Vi

Android 建立手机与手表数据同步机制总结

Android Wear 数据同步机制总结 当手机与手表建立蓝牙连接之后,数据就可以通过Google Play Service进行传输. 同步数据对象Data Item DataItem提供手机与手表数据存储的自动同步,一个DataItem对象由其创建者与路径组成的URI所确定.一个DataItem对象为手机和手表提供了一个数据通路,开发者通过改变指定的DataItem实现手机和手表的数据自动同步. 访问数据层API DataItem可以提供手机和手表数据的保存,改变该对象的操作则依赖数据层AP

Android导入工程提示Invalid project description

在eclipse里导入的时候报错,提示 Invalid project description. 解决办法: 在eclipse的workspace中,找到.metadata文件夹,依次打开------->.plugins文件夹------->org.eclipse.core.resources文件夹------->.projects文件夹 找到你的项目包名一样的文件夹,删除它. 然后从你的workspace中把你的整个项目文件剪切到别的文件夹下,最后重新导入项目,ok 完事. Andro

Android ActivityManagerService(AMS)的Activity管理

对于AMS来讲,Activity管理是它的核心工作,前面两篇文章都是讲AMS的启动流程和进程的管理,这两篇文章其实是为本文做铺垫的,只有理解了前面两篇文章才能更好地理解AMS的activity管理.在谈到Activity的管理的时候,就不得不说一下Activity的启动流程,说道activity的启动流程就要说一下进程启动的问题了,前面一片文章中我们已经分析了AMS的进程管理,这里需要补充的一点就是在android中并没有针对app开放进程的启动接口,只是在启动activity或者service

Android提供的系统服务之--AudioManager(音频管理器)

Android提供的系统服务之--AudioManager(音频管理器) ----转载请注明出处:coder-pig AudioManager相关简介与常用方法图: 简单的使用例子: 使用Mediaplayer播放音乐,通过AudioManager调节音量大小与静音: 这里,我们需要把要播放的音频文件放到res下的raw文件夹,这个文件夹默认是没有的,需要自己创建哦! 用来放原生资源的,就是打包编译的时候不会把他变成二进制文件!!! 先来看看效果图吧: 就是播放音乐,然后调高音量的时候可以看到滑

Android提供的系统服务之--TelephonyManager(电话管理器)

Android提供的系统服务之--TelephonyManager(电话管理器) 转载请注明出处--coder-pig TelephonyManager的作用: 用于管理手机通话状态,获取电话信息(设备信息.sim卡信息以及网络信息), 侦听电话状态(呼叫状态服务状态.信号强度状态等)以及可以调用电话拨号器拨打电话! 如何获得TelephonyManager的服务对象: TelephonyManager tManager = (TelephonyManager)getSystemService(

Android Market google play store帐号注册方法流程 及发布应用注意事项【转载】

[转载]http://www.cnblogs.com/zdz8207/archive/2012/07/09/google-play-store-registered.html Android Market google play store帐号注册方法流程 及发布应用注意事项 Android Market google play store帐号申请 注册方法流程 在 Google Play 中发布软件之前,您需要完成以下三项工作: 创建开发人员个人资料 接受开发人员分发协议 通过信用卡支付注册费