`
007007jing
  • 浏览: 42337 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

android2.3 api demo 学习系列(1)--apidemo主列表的实现

阅读更多

android最有价值的参考资料莫过于sdk提供的apidemos,现在我们就开始一点一点的学习,本人水平有限,文章中出现错误请您指正。

sdk中得apidemos接近200个,设计到android的各个方面,首先先学习下android的开发人员如何将这200多个demo分类的。

1、首先在AndroidManifest.xml注册activity时附件以下intent-filter 例如

 

<activity android:name=".app.Animation" android:label="@string/activity_animation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.angie.apidemos.SAMPLE_CODE" />
            </intent-filter>
        </activity>

  2、在values/Strings.xml 声明该activity的label值 (这样声明是为了下面的list分类)

 

 

<string name="activity_animation">App/Activity/Animation</string>

  3、最后在自己的activity里面处理list

 

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //获取传递进来的附加信息
        Intent intent = getIntent();
        String path = intent.getStringExtra("com.angie.apidemos.Path");
        
        if (path == null) {
            path = "";
        }
        //create list
        setListAdapter(new SimpleAdapter(this, getData(path),
                R.layout.mainlist, new String[] { "image", "title" },
                new int[] { R.id.rowImage, R.id.rowTitle }));
        getListView().setTextFilterEnabled(true);
    }
    protected List getData(String prefix) {
        List<Map> myData = new ArrayList<Map>();

        //获取符合条件的activity  (包含其他应用程序符合条件的activity) 
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(MyApplication.CATEGORY_SAMPLE_CODE);

        PackageManager pm = getPackageManager();
        List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);

        if (null == list)
            return myData;

        String[] prefixPath;
        
        if (prefix.equals("")) {
            prefixPath = null;
        } else {
            prefixPath = prefix.split("/");
        }
        
        int len = list.size();
        
        Map<String, Boolean> entries = new HashMap<String, Boolean>();

        for (int i = 0; i < len; i++) {
            ResolveInfo info = list.get(i);
            CharSequence labelSeq = info.loadLabel(pm); 
            String label = labelSeq != null
                    ? labelSeq.toString()
                    : info.activityInfo.name;

            if (prefix.length() == 0 || label.startsWith(prefix)) {
                
                String[] labelPath = label.split("/");

                String nextLabel = prefixPath == null ? labelPath[0] : labelPath[prefixPath.length];

                if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) {
                    addItem(myData, nextLabel, activityIntent(
                            info.activityInfo.applicationInfo.packageName,
                            info.activityInfo.name),
                            R.drawable.launcher);
                } else {
                    if (entries.get(nextLabel) == null) {
                        addItem(myData, nextLabel, browseIntent(prefix.equals("") ? nextLabel : prefix + "/" + nextLabel),R.drawable.list);
                        entries.put(nextLabel, true);
                    }
                }
            }
        }

        Collections.sort(myData, sDisplayNameComparator);
        
        return myData;
    }

    private final static Comparator<Map> sDisplayNameComparator = new Comparator<Map>() {
        private final Collator   collator = Collator.getInstance();

        public int compare(Map map1, Map map2) {
            return collator.compare(map1.get("title"), map2.get("title"));
        }
    };

    protected Intent activityIntent(String pkg, String componentName) {
        Intent result = new Intent();
        result.setClassName(pkg, componentName);
        return result;
    }
    
    protected Intent browseIntent(String path) {
        Intent result = new Intent();
        result.setClass(this, ApiDemosStudyActivity.class);
        result.putExtra("com.angie.apidemos.Path", path);
        return result;
    }

    protected void addItem(List<Map> data, String name, Intent intent, int image) {
        Map<String, Object> temp = new HashMap<String, Object>();
        temp.put("image", image);
        temp.put("title", name);
        temp.put("intent", intent);
        data.add(temp);
    }

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

 4、上面的代码没有特别难理解的地方,故不再一一注释。其中用到的mainlist定义如下

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
  	android:layout_height="wrap_content"
  	android:gravity="center_vertical"
	android:orientation="horizontal">
	<ImageView
		android:id="@+id/rowImage"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:gravity="center_vertical"
		android:paddingLeft="6dip"
		android:src="@drawable/list" />
	<TextView
		android:id="@+id/rowTitle"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:textAppearance="?android:attr/textAppearanceLarge"
		android:gravity="center_vertical"
		android:paddingLeft="6dip"
		android:minHeight="?android:attr/listPreferredItemHeight"/>	
</LinearLayout>
 

 

 注:代码中用到的drawable资源请自行导入,不再赘述。

其中用到的MyApplication类主要是想定义一个CATEGORY_SAMPLE_CODE,你也可以放弃这种方式 直接使用intent内部定义的CATEGORY。代码示例如下

 

public class MyApplication extends Application {

	public static final String CATEGORY_SAMPLE_CODE = "com.angie.apidemos.SAMPLE_CODE";
	/* (non-Javadoc)
	 * @see android.app.Application#onCreate()
	 */
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
	}

}

效果图:

 

  • 大小: 23 KB
分享到:
评论

相关推荐

    Google官方API(Android-12 ApiDemo)

    **Android API 深度解析:Google 官方 API(Android-12 ApiDemo)** 在 Android 开发中,Google 官方API扮演着至关重要的角色。这些API为开发者提供了丰富的功能,使得应用程序能够实现从基础操作到复杂交互的一切...

    Google官方API(Android-15 ApiDemo)

    1. **UI与动画**:Android-15引入了新的UI设计元素和动画效果,API Demo展示了如何创建和控制自定义动画,以及如何利用Holo主题和Action Bar来提升应用的用户体验。 2. **硬件加速**:在ICS中,硬件加速成为默认...

    Android2.3ApiDemo

    **Android 2.3 API Demo 知识点详解** Android 2.3,也被称为 Gingerbread(姜饼),是Google在2010年推出的Android操作系统的一个重要版本。这个版本引入了许多新特性和改进,旨在优化用户体验和开发者工具。`...

    Google官方API(Android-10 ApiDemo)

    总的来说,Google官方API(Android-10 ApiDemo)为开发者提供了一个全面的学习资源,帮助他们掌握Android 2.3.3的关键功能和API使用。通过深入研究这个项目,开发者可以提升自己的技能,创造出更加高效和用户友好的...

    Google官方API(Android-14 ApiDemo)

    这个API Demo是学习和理解Android API如何工作的绝佳资源,对于新手和经验丰富的开发者来说都非常有价值。 首先,`Android-14`代表的是Android操作系统的第14个版本,即Android 4.0.x (Ice Cream Sandwich),这是一...

    Google官方API(Android-7 ApiDemo)

    通过深入研究和实践Google官方API(Android-7 ApiDemo),开发者不仅可以了解到Android 7.0的新特性,还能学习到如何在实际项目中应用这些特性,提升应用的质量和用户体验。这个API Demo是Android开发者不可或缺的...

    Google官方API(Android-13 ApiDemo)

    总结起来,Google官方API(Android-13 ApiDemo)是一个重要的学习资源,它涵盖了Android-13中的关键更新和API用法。通过深入研究ApiDemo,开发者不仅可以了解Android系统的最新特性,还能提高编程技能,创建出更优质...

    demo-0.0.1-SNAPSHOT.jar

    8080端口,http://localhost:8080/employee/save?id=1&name=""&sex=""&major=""的访问接口 数据库名必须为yunserver,表名employe,存在账号root admin

    android_api_demo

    通过学习和实践这个Android_API_Demo项目,开发者可以对Android开发有更深入的理解,并能熟练运用这些基础知识来构建自己的应用。这个项目不仅适合初学者,也适合有一定经验的开发者作为参考和复习之用。

    android 2.3 apidemo

    通过研究Android 2.3的ApiDemo项目,开发者不仅可以学习到Android的基本概念,还能深入理解各个组件的工作原理和交互方式。虽然现在Android版本已经更新到了12,但ApiDemo中的很多基础知识和原理仍然适用,对于初学...

    android apidemo

    《Android API Demo详解》 Android API Demo是一款专为开发者设计的示例应用,它详尽地展示了Android SDK中的各种API用法,涵盖了Android开发的各个方面,包括用户界面、网络通信、多媒体处理、传感器操作等。这个...

    Android官方apidemo

    Android官方ApiDemo是Android开发者学习和掌握Android API的重要资源,它包含了Android系统各种API的功能示例,涵盖了从基础到高级的各种功能,为开发者提供了直观的代码实例。这个项目可以直接在Android Studio中...

    学习Android Apidemo从这开始

    在Android开发领域,Apidemo是一个非常重要的学习资源,它包含了大量的示例代码,帮助开发者深入理解Android API的各种功能和用法。标题“学习Android Apidemo从这开始”表明我们将从基础开始,逐步深入地探索这个...

    Google官方API(Android ApiDemo)

    通过深入研究Android ApiDemo,开发者不仅可以理解Android API的工作原理,还能学习到最佳实践,这对于构建高质量的Android应用程序至关重要。对于初学者来说,这是一个非常宝贵的教育资源,可以帮助他们快速掌握...

    学习android 的sample apidemo

    "学习android 的sample apidemo"这个项目提供了一系列实例,旨在帮助开发者更好地理解和应用Android SDK中的各种API。这些示例覆盖了从基本的UI组件到复杂的网络通信、多媒体处理等各个方面,是初学者入门和资深...

    android api demo讲解

    ### Android API Demo详解 #### 一、概述 本篇文章旨在为初学者提供一套全面而深入的Android API Demo解析,帮助大家更好地理解Android开发中的各种基础知识和技术细节。文章将按照给出的目录顺序,逐一分析每个...

    Motoroal android BT 4.0 Demo

    Motorola的Android BT 4.0 Demo是一款展示如何在Android系统上利用BLE API进行应用开发的示例程序。该Demo主要目标是让开发者了解并掌握BLE通信的基本步骤,以便在自己的项目中实现类似功能。 首先,我们来解析...

    APIdemo源码

    API Demo 是一个专门用于展示 Android SDK 中各种 API 使用方法的示例项目。它包含了丰富的实例代码,帮助开发者更好地理解和学习 Android 的核心功能和组件。通过对 API Demo 的深入研究,我们可以掌握 Android ...

    android api demo

    本文档是关于基于Android 2.3版本的API Demo解析,旨在帮助开发者更好地理解Android应用开发中的各种控件和技术点。通过一系列实例,详细介绍了如何使用Android SDK中的各个功能模块。 #### 二、主要内容概述 文档...

Global site tag (gtag.js) - Google Analytics