资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Cocos2D-Android-1之源码详解:1.Cocos2D

/*

创新互联公司长期为上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为龙川企业提供专业的做网站、网站建设龙川网站改版等技术服务。拥有十载丰富建站经验和众多成功案例,为您定制开发。

 * Copyright (C) 2007 The Android Open Source Project

 *

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *      http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

package org.cocos2d;

import java.text.Collator;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import android.app.ListActivity;

import android.content.Intent;

import android.content.pm.PackageManager;

import android.content.pm.ResolveInfo;

import android.os.Bundle;

import android.view.View;

import android.widget.ListView;

import android.widget.SimpleAdapter;

public class Cocos2D extends ListActivity {//这个类用了ListActivity 

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState); //继承 ListActivity 的 onCreate方法     //以下是设置list里的适配器

        setListAdapter(new SimpleAdapter(this,//新的简单适配器,运用了自己的上下文

                (List>)getData("org.cocos2d.tests"),//getData是个方法,getData(String)往下看有方法的声明

                android.R.layout.simple_list_item_1, new String[]{"title"},

                new int[]{android.R.id.text1}));

        getListView().setTextFilterEnabled(true);//启用过滤窗口

    }

    protected List getData(String prefix) {//即上述方法

        List> myData = new ArrayList>();//一个用来盛放map的list,map立盛放String

        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);//意图指定跳到系统桌面

        mainIntent.addCategory(Intent.CATEGORY_TEST);//供测试使用

        PackageManager pm = getPackageManager();//包控制器,用来获得现在有的进程

        List list = pm.queryIntentActivities(mainIntent, 0);//通过Intent查找相关的Activity,更准确

        if (null == list)//没找到那个测试意图

            return myData;//就结束

        /* 以下是个没有被使用的方法用来分割字符串,用/

        String[] prefixPath;

        if (prefix.equals("")) {

            prefixPath = null;

        } else {

            prefixPath = prefix.split("/");

        }*/

        int len = list.size();//得到list的长度

        // Map entries = new HashMap();//一个没有被使用的map,用了哈希类型的map

        for (int i = 0; i < len; i++) {

            ResolveInfo info = list.get(i);//用来循环刚才得到的那个list

            String activityName = info.activityInfo.name;//得到activity的名字

            if (prefix.length() == 0 || activityName.startsWith(prefix)) {//如果要查找的那个activity的包名长度为0或者与我们要找的那个名字是以这个开头的

                String[] labelPath = activityName.split("\\.");//把包名用.分开

                String nextLabel = labelPath[labelPath.length - 1];//得到最后一个.后面的名字,即类名

                addItem(myData, nextLabel, activityIntent(//现在把他添加到mydata的集合,这个方法在后面会出现,就是分别把各种信息用map区分了

                        info.activityInfo.applicationInfo.packageName,

                        info.activityInfo.name));

            }

        }

        Collections.sort(myData, sDisplayNameComparator);//Collections是个很有用的集合,其中有个sort是用来排序的,参数中sDisplayNameComparator是个比较方法

        return myData;

    }

//下面这个定义,乍一看是方法,其实是个字段,定义一个比较方法的引用

    private final static Comparator> sDisplayNameComparator = new Comparator>() {//对那个引用的实现

        private final Collator collator = Collator.getInstance();//新疆一个集合

        public int compare(Map map1, Map map2) {//两个的比较规则

            return collator.compare(map1.get("title"), map2.get("title"));//就是比较他们字符的那种方法,就是从左到右一个一个字母,比ascll码

        }

    };

    protected Intent activityIntent(String pkg, String componentName) {

        Intent result = new Intent();

        result.setClassName(pkg, componentName);//设置意图从哪个包到哪个类

        return result;

    }

    protected Intent browserIntent(String path) {

        Intent result = new Intent();

        result.setClass(this, Cocos2D.class);//设置从哪个class到哪

        result.putExtra("org.cocos2d.tests.Path", path);//加一个额外的变量

        return result;

    }

    protected void addItem(List> data, String name, Intent intent) {

        Map temp = new HashMap();

        temp.put("title", name);

        temp.put("intent", intent);

        data.add(temp);//对data添加项目

    }

    @Override

    protected void onListItemClick(ListView l, View v, int position, long id) {

        Map map = (Map) l.getItemAtPosition(position);//得到点击了哪个包

        Intent intent = (Intent) map.get("intent");//找到那个类全名

        startActivity(intent);//打开那个类

    }

}


网页名称:Cocos2D-Android-1之源码详解:1.Cocos2D
文章路径:http://cdkjz.cn/article/jjscco.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220