Intent的数据传递实际上分为以下六种数据:操作(Action)、数据(Data)、数据类型(Type)、操作类别(Category)、附加信息(Extras)、组件(Component)、标志(Flags)。
操作、数据类型、附加信息常用。下面为操作实例
1、打开网页
Uri uri=Uri.parse("www.baidu.com"); //设置操作的路劲
Intent it=new Intent();
it.setAction(Intent.ACTION_VIEW); //设置要操作的Action
it.setData(uri); //要设置的数据
MyDemo.this.startActivity(it); //执行跳转
2、拨打电话
String editStr=MyDemo.this.edit.getText().toString();
Uri uri=Uri.parse("tel:"+editStr); //设置操作的路劲
Intent it=new Intent();
it.setAction(Intent.ACTION_CALL); //设置要操作的Action 改为ACTION_DIAL时,会进入拨号界面,再拨打电话
it.setData(uri); //要设置的数据
MyDemo.this.startActivity(it); //执行跳转
使用ACTION_CALL直接拨打电话,需要授权<uses-permission android:name="android.permission.CALL_PHONE"/>
3、发短信
String editStr=MyDemo.this.edit.getText().toString();
Uri uri=Uri.parse("smsto:"+editStr); //设置操作的路劲
Intent it=new Intent();
it.setAction(Intent.ACTION_SENDTO); //设置要操作的Action
it.putExtra("sms_body",MyDemo.this.content.getText().toString());
it.setType("vnd.android-dir/mms-sms"); //短信的MIME类型
it.setData(uri); //要设置的数据
MyDemo.this.startActivity(it); //执行跳转
4、发送彩信
Uri uri=Uri.parse("file:///sdcard/mm.jpg"); //设置操作的路劲
Intent it=new Intent();
it.setAction(Intent.ACTION_SEND); //设置要操作的Action
it.putExtra("address","13168750594");
it.putExtra("sms_body","深圳百度科技股份有限公司"); //设置短信内容
it.putExtra(Intent.EXTRA_STREAM,uri); //设置短信内容 流文件的形式加上去的
it.setType("image/png"); //短信的MIME类型
MyDemo.this.startActivity(it); //执行跳转
5、发送邮件
Intent it=new Intent(Intent.ACTION_SEND);
String address[]=new String[]{"291866706@qq.com"};
String subject="百度科技股份公司";
String content="今天召开招标会议,请务必赶到";
it.putExtra(Intent.EXTRA_EMAIL,address);
it.putExtra(Intent.EXTRA_SUBJECT,subject); //设置短信内容
it.putExtra(Intent.EXTRA_TEXT,content); //设置短信内容 流文件的形式加上去的
it.setType("plain/text"); //短信的MIME类型
MyDemo.this.startActivity(it); //执行跳转
6、取得电话联系人
public class MyDemo extends Activity {
private static final int PICK_CONTACT_SUBACTIVITY = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
Uri uri = Uri.parse("content://contacts/people");
Intent it = new Intent(Intent.ACTION_PICK, uri);
super.startActivityForResult(it, PICK_CONTACT_SUBACTIVITY);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICK_CONTACT_SUBACTIVITY:
Uri ret = data.getData(); // 接收返回的数据
String phoneSelection = ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ "=?"; // 设置查询条件
String[] phoneSelectionArgs = { String.valueOf(ContentUris
.parseId(ret)) }; // 返回ID
Cursor c = super.managedQuery(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
phoneSelection, phoneSelectionArgs, null); //查询的结果
StringBuffer buf = new StringBuffer();
buf.append("电话号码是:");
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
buf.append(
c.getString(c
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)))
.append("、");
}
Toast.makeText(MyDemo.this, buf, Toast.LENGTH_LONG).show(); //飘字提示电话
}
}
}
7、创建操作Intent的选择器
如果用户希望把自己的程序加入到开发的选择器中,则就需要使用createChooser()方法完成,使用createChooser()方法会自动创建一个Intent,其Action的名称为“ACTION_CHOOSER” public static Intent createChooser(Intent target,CharSequence title)
ImageViewActivity.java
package com.jackie.intentcaseproject;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
public class ImageViewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setTitle("查看图片");
ImageView img=new ImageView(this); //图片浏览器
img.setImageResource(R.drawable.pic_ad);
super.setContentView(img);
}
}
MyDemo.java
package com.jackie.intentcaseproject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class MyDemo extends Activity {
private ImageButton mybut = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.mybut = (ImageButton) super.findViewById(R.id.mybut);
this.mybut.setOnClickListener(new OnClickListenerImpl());
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View arg0) {
Intent it = new Intent();
it.setAction(Intent.ACTION_GET_CONTENT);
it.setType("image/*");
//MyDemo.this.startActivity(Intent.createChooser(it, "选择图片浏览工具"));
MyDemo.this.startActivity(it);
}
}
}
manifest.xml 声明Activity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jackie.intentcaseproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:screenOrientation="landscape"
android:name="com.jackie.intentcaseproject.MyDemo"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:screenOrientation="landscape"
android:name="com.jackie.intentcaseproject.ImageViewActivity">
<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/png"/>
</intent-filter>
</activity>
</application>
</manifest>