Intent的数据传递
来源:互联网 发布:iptables 增加端口 编辑:程序博客网 时间:2024/04/29 16:30
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;@Overrideprotected 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);}@Overrideprotected 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)) }; // 返回IDCursor 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 { @Overrideprotected 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;@Overrideprotected 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 {@Overridepublic 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>
0 0
- Intent传递的数据
- Intent的数据传递
- Intent数据的传递
- Intent的数据传递
- Intent的数据传递
- Intent数据的传递
- Intent的数据传递
- Android Intent的数据传递
- Intent传递数据的方式
- Intent复杂数据的传递
- Intent传递数据和Bundle传递数据的区别
- android中intent传递数据的方式
- android笔记 Intent传递数据的方法
- Intent传递复杂数据的问题
- Activity之间的数据传递: Intent
- 使用Intent和Bundle的数据传递
- android intent 的数据传递两种
- Android之Intent传递数据的方式
- ubuntu14.04交叉编译环境搭建记录
- 「原」valueOf() //返回某个字符串对象的原始值 20140830 ①文本处理
- 「提」操作提示20140831 ⑧平台控制
- eclipse ADT在线安装 https://dl-ssl.google.com/android/eclipse无法访问
- 进程间通讯IPC随笔
- Intent的数据传递
- windows编译ffmpeg出现gcc is unable to create an executable file 的一般情况
- Linux下配置jdk1.7
- Java - 集合框架 - 整理
- [翻译+笔记]推荐算法之协作过滤
- mac Cocos2d-x中常用粒子编辑器ParticleDesigner测试例子
- 通 信 习 题 TD基本原理
- 手机接入检测及驱动安装判断
- 判断一个数是否是2的幂次方