第77章、再识Intent-创建选择器(从零开始学Android)

来源:互联网 发布:张学友 陈奕迅 知乎 编辑:程序博客网 时间:2024/06/05 03:04

  有不少初学Android的朋友问我,选择器怎么那么不好理解呢?
  实际上一点也不难,在讲Intent-Chooser之前,我们先看一个Windows中的常见例子:我们选择一张图片,单击“右键”,弹出如下对话框。
  

  Android中Intent-Chooser就是要实现上面两个效果:(1)如何产生右键打开方式效果;(2)如何把自己的Android App添加到列表中。

  (1)如何产生右键打开方式效果:

    Intent intent=new Intent();    intent.setAction(Intent.ACTION_GET_CONTENT);    intent.setType("image/*");    MainActivity.this.startActivity(intent.createChooser(intent, "选择图片查看APP"));

  核心代码就上面四行。
  (2)如何把自己的Android App添加到列表中:

   <intent-filter>                <action android:name="android.intent.action.GET_CONTENT" />                <category android:name="android.intent.category.DEFAULT" />                <category android:name="android.intent.category.OPENABLE" />                <data android:mimeType="image/jpeg" />            </intent-filter>


  下面我们具体来学习一下如何实现之。

一、创建PictureViewer应用程序

  本APP目的是为了实现把自己添加到列表中。

1、程序文件

  打开“src/com.genwoxue.pictureviewer/MainActivity.java”文件。
  然后输入以下代码:

package com.genwoxue.pictureviewer;import android.os.Bundle;import android.app.Activity;import android.widget.ImageView;public class MainActivity extends Activity {@Override public void onCreate(Bundle savedInstanceState)       {           super.onCreate(savedInstanceState);        ImageView ivPicture=new ImageView(this);        ivPicture.setImageResource(R.drawable.ic_launcher);        setContentView(ivPicture);    }}     


2、配置文件

  打开“AndroidManifest.xml”文件。

  然后输入以下代码:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.genwoxue.pictureviewer"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="10"        android:targetSdkVersion="15" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.genwoxue.pictureviewer.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.GET_CONTENT" />                <category android:name="android.intent.category.DEFAULT" />                <category android:name="android.intent.category.OPENABLE" />                <data android:mimeType="image/jpeg" />            </intent-filter>                    </activity>    </application></manifest>


注意:AndroidManifest.xml文件中以下内容决定把本APP加入到打开图像列表中。

  <intent-filter>
                <action android:name="android.intent.action.GET_CONTENT" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.OPENABLE" />
                <data android:mimeType="image/jpeg" />
            </intent-filter>

二、创建IntentChooser应用程序

  本APP目的是为了实现打开列表功能(即windows中的右键之打开方式)。

1、设计界面

  1、布局文件

  打开res/layout/activity_main.xml文件。
  输入以下代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Button        android:id="@+id/choose"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="选择查看图片工具" /></LinearLayout>


2、程序文件

  打开“src/com.genwoxue.intentchooser/MainActivity.java”文件。
  然后输入以下代码:

package com.genwoxue.intentchooser;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {private Button btnChoose=null;@Override public void onCreate(Bundle savedInstanceState)       {           super.onCreate(savedInstanceState);                      setContentView(R.layout.activity_main);        btnChoose=(Button)super.findViewById(R.id.choose);        btnChoose.setOnClickListener(new OnClickListener(){        public void onClick(View v)        {           //系统会把所有查看Image类型的应用显示在列表中        Intent intent=new Intent();        intent.setAction(Intent.ACTION_GET_CONTENT);        intent.setType("image/*");        MainActivity.this.startActivity(intent.createChooser(intent, "选择图片查看APP"));        }        });    }}            


3、配置文件

  打开“AndroidManifest.xml”文件。

  然后输入以下代码:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.genwoxue.intentchooser"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="10"        android:targetSdkVersion="15" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.genwoxue.intentchooser.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>


注意:用默认的AndroidManifest.xml即可,没有什么可说的。

 

三、运行结果

  1、首先运行PictureViewer

  虽然我们没有看到运行的界面,但已经运行了。

  2、然后,运行Intentchooser

   

  注意:效果实现了,但在PictureViewer中并没有真正实现浏览图片功能。

 

  如果你想做一个类似的应用,譬如创建一个显示音乐列表的选择器,那么只需:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);intent.setType("audio/*");startActivity(Intent.createChooser(intent, "Select music"));

  当然,想显示视频或者别的列表,都是依次类推,举一反三就Ok了!