Intent 之系统Intent<一>

转发请注明出处:http://blog.csdn.net/qq_28055429/article/details/51958946

前言:Android中的Intent中主要有两种:即系统Intent和自定义Intent

系统Intent:

一,基本知识:

(1)解释:意图,意图机制,是连接组件的管理者。

(2)Intent对象的基本构成:如:在下雨天,我喜欢吃肉,特别是鸡肉

Action:表达动作,可通过Intent.setAction()设置,如例句中的:谓语--吃

常用的有: 常用于:

Intent.ACTION_WEB_SEARCH ----浏览器搜索;

 new Intent(Intent.ACTION_VIEW , uri);  //通过Intent.ACTION_VIEW来显示此Uri的内容

Intent.ACTION_DIAL   ----   拨号界面

Intent.ACTION_CALL  ---- 直接拨号

Intent.ACTION_SEND   ---   发送,如短信,邮件等

Intent.ACTION_DELETE----  删除数据

Intent.ACTION_GET_CONTENT ----获取内容,如图库

Intent.ACTION_VIEW:这个用的最多,功能很多,,具体略,

Data:做特指的宾语,如鸡肉

设置方式:Intent.setData或Intent.setDataAndType

读取方式:Intent.getData

常用URI表示:

file:///sdcard/a.data  代表本地sdcard下的a.data文件

content://com.duguhome.providers.a/1   代表数据源组件com.duguhome.providers.a中id为1的数据

http://baidu.com/a.data  --  代表baidu.com下的a.data文件

Type:泛指的宾语,如,肉

      设置 :Intent.setType或Intent.setDataAndType

      获取 :Intent.getType()

   可以用通配符*表示,如,
	image/*----图片;
	 image/jpg ---- jpg格式的图片
	 text/plain  : 纯文本
	 text/html  :  HTML文档
	 text/*  :  text类型
	 video/mpeg  :  MPEG动画
	 还有:(*/*)

注意:Data,Type谁后设置谁做主,若需一起做主人,则,需用setDataAndType()方法,参见博文:

http://blog.csdn.net/qq_28055429/article/details/51442643

Category :约束条件,如在下雨天,Intent.addCategory可为intent添加多个Category项,Intent.getCategories获取

Component :目标组件,一旦使用了,则,intent对象变成只是负责传递信息的载体

Extras:Intent中数据传输的载体,按键值对存取,getExtras,setExtra方法

Flags:标志,如,设置为

Intent.FLAG_ACTIVITY_VIEW_NEW_TASK:组件将会尝试去构造新的任务目标组件,而不是默认的原有组件,

Intent.setFlags ,Intent.addFlags方法

总之,一系列set方法和get方法,还有少数的add方法


二,使用:

例子(1):google搜索,根据URL调用浏览器,电话拨打调用,发送短信彩信和邮件

(1)google搜索:

关键代码:

//设置Intent对象的action为ACTION_WEB_SEARCH:代表通过Google浏览器搜索
                    intent.setAction(Intent.ACTION_WEB_SEARCH);
                    //设置Intent对象的附加内容为SearchManager.QUERY :代表搜索关键字
                    intent.putExtra(SearchManager.QUERY , Et.getText().toString());
                    startActivity(intent);

(2)根据URL网址使用浏览器:

   Uri uri = Uri.parse("http://" + url);       
   intent = new Intent(Intent.ACTION_VIEW , uri);  //通过Intent.ACTION_VIEW来显示此Uri的内容
   startActivity(intent);
(3)电话拨打:

                        Uri uri = Uri.parse("tel:" + phonenum);
                        //intent  = new Intent(Intent.ACTION_CALL , uri);直接拨打
                        //下面是调到拨号界面
                        intent = new Intent(Intent.ACTION_DIAL,uri);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);

(4)发送短信和彩信和邮件:

短信:

                        intent  =   new Intent(Intent.ACTION_VIEW) ;
                        intent.putExtra("sms_body" , msg);
                        intent.setType("vnd.android-dir/mms-sms");
                        startActivity(intent);


彩信:

              Uri uri =  Uri.parse("content://media/external/images/media/01");
                    Intent  intent  =   new Intent(Intent.ACTION_SEND);     //设置Action
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);         //添加Flags

                    intent.putExtra(Intent.EXTRA_STREAM , uri );       //设置intent的附件参数为之前的uri图片
                    intent.putExtra("subject" , sub);           //分别设置主题,号码,内容
                    intent.putExtra("address" , number);
                    intent.putExtra("sms_body" , msg);
                    intent.putExtra(Intent.EXTRA_TEXT , "通过彩信向你问好!");        //设置intent的彩信附加文字
                    intent.setType("image/*");      //彩信附件类型
                    //设置intent的彩信发送activity
                    intent.setClassName("com.android.mms" , "com.android.mms.ui.ComposeMessageActivity");
                    startActivity(intent);

邮件:

                   Intent  intent  =   new Intent(Intent.ACTION_SEND);     //设置Action
                   // intent.setType("text/plain"); 模拟器使用用这行
                    intent.setType("message/rfc822");
                    intent.putExtra(Intent.EXTRA_EMAIL , new String[]{email});  //email为邮箱地址
                    intent.putExtra(Intent.EXTRA_SUBJECT , sub);    //主题
                    intent.putExtra(Intent.EXTRA_TEXT , msg);       //内容
                    startActivity(Intent.createChooser(intent , "请选择发送邮件的客户端"));

完整代码如下:

google搜索,根据URL调用浏览器,电话拨打调用,发送短信代码放在一起了:

布局文件:activity_first:4个EditText,4个按钮,分别用于输入和启动

<?xml version="1.0" encoding="utf-8"?>
<!--定义基本布局:LinearLayout-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <!--定义用户输入搜索关键字的输入控件-->
    <EditText
        android:id="@+id/Et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/et_first"/>
    <!--定义用户开始搜索按钮控件-->
    <Button
        android:id="@+id/Btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_first"/>
    <!--定义用户输入URL网址的输入控件-->
    <EditText
        android:id="@+id/Et2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/et2_first"/>
    <!--定义用户打开浏览器按钮控件-->
    <Button
        android:id="@+id/Btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn2_first"/>
    <!--定义用户输入电话号码的输入控件-->
    <EditText
        android:id="@+id/Et3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/et3_first"/>
    <!--定义用户开始拨号按钮控件-->
    <Button
        android:id="@+id/Btn3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn3_first"/>
    <!--定义用户输入分享短信内容的输入控件-->
    <EditText
        android:id="@+id/Et4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/et4_first"/>
    <!--定义用户打开短信界面的按钮控件-->
    <Button
        android:id="@+id/Btn4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn4_first"/>

</LinearLayout>
主类:FirstActivity,调用系统Intent去实现,代码如下:

package testsdcard.com.maiyu.s05_092.activity;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import testsdcard.com.maiyu.s05_092.R;

/**
 * Created by maiyu on 2016/7/19.
 */
public class FirstActivity extends Activity {

    private EditText Et , Et2 , Et3 , Et4;
    private Button   Btn , Btn2 , Btn3 , Btn4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        findView();     //为控件绑定id
        setListener();  //设置监听
    }

    //设置监听器
    private void setListener() {
        Btn.setOnClickListener(mylistener);
        Btn2.setOnClickListener(mylistener);
        Btn3.setOnClickListener(mylistener);
        Btn4.setOnClickListener(mylistener);
    }

    //自定义监听器
    View.OnClickListener  mylistener    =   new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent  intent  =   new Intent() ;
            switch (view.getId()){
                case R.id.Btn : //google搜索内容
                    //设置Intent对象的action为ACTION_WEB_SEARCH:代表通过Google浏览器搜索
                    intent.setAction(Intent.ACTION_WEB_SEARCH);
                    //设置Intent对象的附加内容为SearchManager.QUERY :代表搜索关键字
                    intent.putExtra(SearchManager.QUERY , Et.getText().toString());
                    startActivity(intent);
                    break;
                case R.id.Btn2 :        //URL网址搜索
                    String  url  =  Et2.getText().toString();
                    if(!"".equals(url)){      //判断输入是否为空
                        Uri uri = Uri.parse("http://" + url);       //一般用户输入时不会加http://,然后通过处理网址生成Uri对象
                        intent = new Intent(Intent.ACTION_VIEW , uri);  //通过Intent.ACTION_VIEW来显示此Uri的内容
                        startActivity(intent);
                    }else {
                        Toast.makeText(FirstActivity.this , "请输入正确的URL网址" , Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.Btn3 :    //电话拨打
                    String phonenum =   Et3.getText().toString();
                    if(!"".equals(phonenum)){
                        Uri uri = Uri.parse("tel:" + phonenum);
                        //intent  = new Intent(Intent.ACTION_CALL , uri);直接拨打
                        //下面是调到拨号界面
                        intent = new Intent(Intent.ACTION_DIAL,uri);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    }else {
                        Toast.makeText(FirstActivity.this , "请输入电话号码",Toast.LENGTH_SHORT).show();
                    }
                    break;
                //短信分享内容
                case R.id.Btn4 :
                    String msg = Et4.getText().toString();
                    if(!"".equals(msg)){
                        intent  =   new Intent(Intent.ACTION_VIEW) ;
                        intent.putExtra("sms_body" , msg);
                        intent.setType("vnd.android-dir/mms-sms");
                        startActivity(intent);
                    }else{
                        Toast.makeText(FirstActivity.this, "请输入要分享的短信内容...", Toast.LENGTH_SHORT).show();
                    }
                    break;
                default:
                    break;
            }
        }
    };

    //为各个控件绑定id
    private void findView() {
        Et      =   (EditText)findViewById(R.id.Et);
        Et2     =   (EditText)findViewById(R.id.Et2);
        Et3     =   (EditText)findViewById(R.id.Et3);
        Et4     =   (EditText)findViewById(R.id.Et4);
        Btn     =   (Button)findViewById(R.id.Btn);
        Btn2    =   (Button)findViewById(R.id.Btn2);
        Btn3    =   (Button)findViewById(R.id.Btn3);
        Btn4    =   (Button)findViewById(R.id.Btn4);
    }
}

添加权限:分别是:访问网络,拨打电话,发送短信,写和创建和删除sdcard文件,相机

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <uses-permission android:name="android.permission.CAMERA"></uses-permission>

结果:略,,,,

彩信和邮件完整代码:

布局:三个EditText接受(彩信:电话号码,主题,内容;邮件:邮箱,主题,内容,)一个启动按钮,内容相似,这里

只给出彩信的布局:

<?xml version="1.0" encoding="utf-8"?>
<!--定义基本布局:LinearLayout-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <!--彩信号码-->
    <EditText
        android:id="@+id/EtNum"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/et_num_second"/>
    <!--彩信主题-->
    <EditText
        android:id="@+id/EtSub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/et_sub_second"/>
    <!--彩信内容-->
    <EditText
        android:id="@+id/EtMsg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/et_sms_second"/>
    <!--发送彩信-->
    <Button
        android:id="@+id/Btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_second"/>

</LinearLayout>

彩信主类:

package testsdcard.com.maiyu.s05_092.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import testsdcard.com.maiyu.s05_092.R;

/**
 * Created by maiyu on 2016/7/19.
 */
public class SecondActivity extends Activity {

    private Button btn ;        //定义按钮控件
    private EditText EtMsg , EtNum , EtSub ;    //定义内容,号码,主题
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        findView();     //为控件绑定id
        setListener();  //为控件设置监听
    }

    //为控件设置监听
    private void setListener() {
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //分别得到要发送的彩信的:内容,号码,主题
                String  msg =   EtMsg.getText().toString();
                String  number  =   EtNum.getText().toString();
                String  sub =   EtSub.getText().toString();
                //判空
                if(!"".equals(msg) && !"".equals(number) && !"".equals(sub)){
                    //定义了用户系统的1号图片的uri,uri格式: "content://"、数据的路径、标示ID(可选)
                    //通过DDMS,我们在/data/data/com.android.providers.media下找到数据库文件
                    //打开external.db文件进一步查看:在media表格下,可以看到文件路径(_data)和Uri的标示ID(_id)的对应关系。
                    Uri uri =  Uri.parse("content://media/external/images/media/01");
                    Intent  intent  =   new Intent(Intent.ACTION_SEND);     //设置Action
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);         //添加Flags

                    intent.putExtra(Intent.EXTRA_STREAM , uri );       //设置intent的附件参数为之前的uri图片
                    intent.putExtra("subject" , sub);           //分别设置主题,号码,内容
                    intent.putExtra("address" , number);
                    intent.putExtra("sms_body" , msg);
                    intent.putExtra(Intent.EXTRA_TEXT , "通过彩信向你问好!");        //设置intent的彩信附加文字
                    intent.setType("image/*");      //彩信附件类型
                    //设置intent的彩信发送activity
                    intent.setClassName("com.android.mms" , "com.android.mms.ui.ComposeMessageActivity");
                    startActivity(intent);
                }else {
                    Toast.makeText(SecondActivity.this , "彩信号码或内容为空,,," ,Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    //为控件绑定id
    private void findView() {
        btn     =   (Button)findViewById(R.id.Btn);
        EtMsg   =   (EditText)findViewById(R.id.EtMsg);
        EtSub   =   (EditText)findViewById(R.id.EtSub);
        EtNum   =   (EditText)findViewById(R.id.EtNum);

    }
}

邮件主类:

package testsdcard.com.maiyu.s05_092.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import testsdcard.com.maiyu.s05_092.R;

/**
 * Created by maiyu on 2016/7/19.
 */
public class ThirdActivity extends Activity {

    private Button btn ;        //定义按钮控件
    private EditText EtMsg , EtEmail , EtSub ;    //定义内容,号码,主题
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
        findView();     //为控件绑定id
        setListener();  //为控件设置监听
    }

    //为控件设置监听
    private void setListener() {
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //分别得到要发送的彩信的:内容,号码,主题
                String  msg =   EtMsg.getText().toString();
                String  email  =   EtEmail.getText().toString();
                String  sub =   EtSub.getText().toString();
                //判空
                if(!"".equals(msg) && !"".equals(email) && !"".equals(sub)){

                    Intent  intent  =   new Intent(Intent.ACTION_SEND);     //设置Action
                   // intent.setType("text/plain"); 模拟器使用用这行
                    intent.setType("message/rfc822");
                    intent.putExtra(Intent.EXTRA_EMAIL , new String[]{email});  //email为邮箱地址
                    intent.putExtra(Intent.EXTRA_SUBJECT , sub);    //主题
                    intent.putExtra(Intent.EXTRA_TEXT , msg);       //内容
                    startActivity(Intent.createChooser(intent , "请选择发送邮件的客户端"));
                }else {
                    Toast.makeText(ThirdActivity.this , "彩信号码或内容或主题为空,,," ,Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    //为控件绑定id
    private void findView() {
        btn     =   (Button)findViewById(R.id.Btn);
        EtMsg   =   (EditText)findViewById(R.id.EtMsg);
        EtSub   =   (EditText)findViewById(R.id.EtSub);
        EtEmail   =   (EditText)findViewById(R.id.EtEmail);

    }
}

记得添加权限,和上面类似,,,


三,另一类例子:

(1)启动播放音乐

                    intent    =   new Intent(Intent.ACTION_VIEW);   //设置action属性为Intent.ACTION_VIEW
                    Uri uri = Uri.parse("file:///sdcard/五月天-温柔.mp3");   //定义sdcard下的song.mp3文件的uri
                    intent.setDataAndType(uri , "audio/mp3");  //设置intent的数据类型为audio/mp3,
                    startActivity(intent);

(2)安装卸载软件:

                    //安装应用
                    intent    =   new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.parse("file://" + "/sdcard/XMNotes.apk") ,
                            "application/vnd.android.package-archive");
                    startActivity(intent);
                    //卸载应用
//                    Uri packageURI  = Uri.parse("package:指定的uri"); //要卸载的软件的包名uri
//                    Intent uninstallIntent  =  new Intent(Intent.ACTION_DELETE);
//                    startActivity(uninstallIntent);

其他代码都类似,就不列出,完整代码在末尾可以下载,,,

(3)调用摄像机和打开图库和打开录音:这里用startActivityForResult(意图对象,请求码);

请求码----requestCode:自己设置

请求结果----- resultCode :如若正确返回,则返回RESULT_OK

打开摄像机:

                    intent  =   new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(intent , 1);
在onActivityResult方法中:

        if(requestCode == 1){       //若果requestCode为1,
            if(resultCode == RESULT_OK){        //得到返回的处理状态,成功得到照片返回RESULT_OK
                //取出data对象中的数据data的值,并化为Bitmap设置在Iv上
                Bitmap  bmPhoto  = (Bitmap)data.getExtras().get("data");
                Iv.setImageBitmap(bmPhoto);
            }
        }


打开图库:

                    intent  =   new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(intent , 2);

在onActivityResult方法中:

        if(requestCode == 2){      //如果requestCode为2
            if(resultCode == RESULT_OK){    //返回处理结果成功
                Uri  uri  =  data.getData();    ///取得data对象中的数据
                //获取当前activity的ContentResolver对象
                ContentResolver cr = this.getContentResolver();
                try{
                    //通过ContentResolver得到相应图片并显示出来
                    Bitmap bitmap  = BitmapFactory.decodeStream(cr.openInputStream(uri));
                    Iv.setImageBitmap(bitmap);
                }catch(FileNotFoundException e){
                    e.printStackTrace();
                }
            }
        }

打开录音:

                    intent  =   new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
                    startActivityForResult(intent , 1);
在onActivityResult方法中:

 if(requestCode == 1){
            if(resultCode == RESULT_OK){
                //得到录音的音频文件及路径
                String  dataFile = data.getDataString();
                String  dataUri  = getIntent().getDataString();
                Toast.makeText(FourActivity.this , "dataFile is "+dataFile + "dataUri is "+ dataUri,
                        Toast.LENGTH_SHORT).show();
            }
        }

打开录音的完整代码就忽略了,

打开相机和图库的完整代码如下:

布局:

activity_five:一个imageView用于显示,两个按钮用于启动,即打开相机,打开图库

<?xml version="1.0" encoding="utf-8"?>
<!--定义基本布局为:LinearLayout-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <!--定义一个TextView-->
    <TextView
        android:id="@+id/Tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/tv_five"/>
    <!--定义一个用于显示图片的ImageView-->
    <ImageView
        android:id="@+id/Iv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/bg"/>
    <!--打开摄像机按钮-->
    <Button
        android:id="@+id/Btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_five"/>
    <!--打开图库的按钮-->
    <Button
        android:id="@+id/Btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn2_five"/>

</LinearLayout>


主类:FiveActivity,代码如下:

package testsdcard.com.maiyu.s05_092.activity;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.FileNotFoundException;

import testsdcard.com.maiyu.s05_092.R;

/**
 * Created by maiyu on 2016/7/19.
 */
public class FiveActivity extends Activity {
    private Button  btn , btn2;        //按钮,
    private TextView  Tv ;       //textView
    private ImageView Iv ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_five);
        findView();     //为控件绑定id
        setListener();  //为控件设置监听
    }

    //为控件设置监听
    private void setListener() {
        btn.setOnClickListener(mylistener);
        btn2.setOnClickListener(mylistener);
    }

    //自定义监听器mylistener
    View.OnClickListener    mylistener  =   new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent  intent ;
            switch (view.getId()){
                case R.id.Btn :     //打开摄像机
                    intent  =   new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(intent , 1);
                    break;
                case R.id.Btn2 :    //打开图库
                    intent  =   new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(intent , 2);
                    break;
                default:
                    break;
            }
        }
    };
    //为各个控件绑定id
    private void findView() {
        btn     =    (Button)findViewById(R.id.Btn);
        btn2    =    (Button)findViewById(R.id.Btn2);
        Tv      =    (TextView)findViewById(R.id.Tv);
        Iv      =    (ImageView)findViewById(R.id.Iv);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1){       //若果requestCode为1,
            if(resultCode == RESULT_OK){        //得到返回的处理状态,成功得到照片返回RESULT_OK
                //取出data对象中的数据data的值,并化为Bitmap设置在Iv上
                Bitmap  bmPhoto  = (Bitmap)data.getExtras().get("data");
                Iv.setImageBitmap(bmPhoto);
            }
        }
        else if(requestCode == 2){      //如果requestCode为2
            if(resultCode == RESULT_OK){    //返回处理结果成功
                Uri  uri  =  data.getData();    ///取得data对象中的数据
                //获取当前activity的ContentResolver对象
                ContentResolver cr = this.getContentResolver();
                try{
                    //通过ContentResolver得到相应图片并显示出来
                    Bitmap bitmap  = BitmapFactory.decodeStream(cr.openInputStream(uri));
                    Iv.setImageBitmap(bitmap);
                }catch(FileNotFoundException e){
                    e.printStackTrace();
                }
            }
        }
    }
}
添加权限:

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <uses-permission android:name="android.permission.CAMERA"></uses-permission>


当然,系统intent作用还有很多,如打开联系人,等等,,,

本文所有例子的代码下载地址:http://download.csdn.net/detail/qq_28055429/9580623




  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值