从AndroidManifest.xml中获取已注册Activity的方法

契机:
在学习并积累自定义控件,为了看效果,需要放到Activity中来显示。
每次写新的自定义控件,不可能都放到同一个Activity中,为了更好观看效果,每个Activity中只放少数控件。
如此,随着自定义控件增多,Activity也逐渐增多,相应的需要在AndroidManifest中配置,还需要想办法(例如写个按钮)能够访问每个Activity。
于是,有没有办法自动从AndroidManifest.xml中获取所有已经注册过的Activity,放到ListView中显示呢。

写了以下工具类:
public final class ClassUtils {
        private final static String TAG = "ClassUtils";
        private ClassUtils(){}
        
        /**
         * 返回AndroidManifest.xml中注册的所有Activity的class
         * @param context                环境
         * @param packageName  		包名
         * @param excludeList        排除class列表
         * @return
         */
        public final static List<Class> getActivitiesClass(Context context, String packageName, List<Class> excludeList){
                
                List<Class> returnClassList = new ArrayList<Class>();
                try {
                        //Get all activity classes in the AndroidManifest.xml
                        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
                        if(packageInfo.activities!=null){
                                Log.d(TAG, "Found "+packageInfo.activities.length +" activity in the AndrodiManifest.xml");
                                for(ActivityInfo ai: packageInfo.activities){
                                        Class c;
                                        try {
                                                c = Class.forName(ai.name);
                                                // Maybe isAssignableFrom is unnecessary
                                                if(Activity.class.isAssignableFrom(c)){
                                                        returnClassList.add(c);
                                                        Log.d(TAG, ai.name +"...OK");
                                                }
                                        } catch (ClassNotFoundException e) {
                                                Log.d(TAG, "Class Not Found:"+ai.name);
//                                                e.printStackTrace();
                                        }
                                }
                                Log.d(TAG, "Filter out, left "+ returnClassList.size()  +" activity," + Arrays.toString(returnClassList.toArray()));
                                
                                //Exclude some activity classes
                                if(excludeList!=null){
                                        returnClassList.removeAll(excludeList);
                                        Log.d(TAG, "Exclude " + excludeList.size() +" activity,"+ Arrays.toString(excludeList.toArray()));
                                }
                                Log.d(TAG, "Return "+ returnClassList.size()  +" activity," + Arrays.toString(returnClassList.toArray()));
                        }
                } catch (NameNotFoundException e) {
                        e.printStackTrace();
                }

                return returnClassList;
        }

}


调用:
                List<Class> excludeList = new ArrayList<Class>();
                excludeList.add(this.getClass());
                List<Class> activities = ClassUtils.getActivitiesClass(this, getPackageName(), excludeList);



activities作为数据源与ListView绑定在一起,点击的时候跳转

            startActivity(new Intent(MainActivity.this, activities.get(position)));



展开阅读全文

没有更多推荐了,返回首页