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

  有不少初学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了!

 

  

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蒋会全

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值