android之Intent的七大属性

发布时间:2020-06-01 17:09:54 作者:墨宇hz
来源:网络 阅读:672

    Intent用于封装程序的“调用意图”。两个Activity之间,可以把需要交换的数据,封装成Bundle对象,然后使用Intent对象,携带数据到另一个Activity中。实现两个Activity的数据交换。

    Intent还是各种应用程序组件之间通信的重要媒介。启动ActivityServiceBroadcastReceiver,都需要使用Intent

    Intent还有一个好处,如果应用程序只是想启动具有某种特征的组件,并不想和某个具体的组件耦合,则可以通过在int ent - filt er 中配置相应的属性进行处理,与s t uc t s 2 中的MVC框架思路类似

    Intent对象大致包括7大属性:1Component Name2Action3Catory4data5Type6Extra7Flag

    ·Action作为标识符,代表一个Intent ,当一个Activity 需要外部协助处理时,就会发出一个Intent,如果一个程序能完成相应功能,只要在int ent - filt er 加上这个这个intent 就可以了。

    ·Data保存需要传递的数据格式,比如:tel://

    ·Extras保存需要传递的额外数据。

    ·Category 表示Intent的种类,从android上启动Activity 有多种方式,比如 程序列表、桌面图标、点击Home激活的桌面等等,Category则用来标识这些Activity的图标会出现在哪些启动的上下文环境里。

(一)  ComponentName:明确指定Intent将要启动哪个组件,因此这种Intent被称为显示Intent,没有指定ComponentName属性的Intent被称为隐式Intent。隐式Intent没有明确要启动哪个组件,应用汇根据Intent指定的规则去启动符合条件的组件。ComponentName不仅可以启动本程序中的activity,还可以启动其它程序的activity

启动方式:

Intent intent =new Intent();

ComponentNamecomponent= new ComponentName(this, TwoActivity.class);

intent.setComponent(component);

等价于:

Intent intent =new Intent(this, TwoActivity.class);

startActivity(intent);

等价于:

Intent intent =new Intent();

intent.setClass(this,TwoActivity.class);

startActivity(intent);

等价于:

Intent intent =new Intent();

intent.setClassName(this,"com.zzh.day06_intent.TwoActivity");

startActivity(intent);

本程序中启动其它程序:

 

(二) ActionCategory 属性与intent-filter配置:

通常,Action, Category属性结合使用。定义这两个属性都是在主配置文件的<intent-filter>节点中。Intent通过定义Action属性(其实就是定义一段自定义的字符串),这样就可以把Intent与具体的某个Activity分离,实现了解耦。否则,每次跳转都有写成,

(三) data

1、 用于添加数据。通常是启动某个系统程序或其他程序,带给此程序的信息。Data属性通常用于向Action属性提供操作的数据。Data属性的值是个Uri对象。

Uri的格式如下scheme://host:port /path

2、 系统内置的属性常量

tel:是固定写法,是系统内置的属性常量。

系统内置的几个Dat a属性常量:

·tel: //:号码数据格式,后跟电话号码。

·mailto: //:邮件数据格式,后跟邮件收件人地址。

·smsto: //:短息数据格式,后跟短信接收号码。

·content : //:内容数据格式,后跟需要读取的内容。

·file://:文件数据格式,后跟文件路径。

·market://search?q=pname:pkgname:市场数据格式,在Google Market里搜索包名为pkgname的应用。

·geo: //latitude, longitude:经纬数据格式,在地图上显示经纬度所指定的位置。

四、Intent利用Action属性和Dat a属性启动Android系统内置组件的代码

(一)、拨打电话:

Intent intent=new Intent();

intent.setAction(Intent.ACTION_CALL);

//intent.setAction("android.intent.action.CALL");  //以下各项皆如此,都有两种写法。

intent.setData(Uri.parse("tel:1320010001"));

startActivity(intent);

//调用拨号面板:

Intent intent=new Intent();

intent.setAction(Intent.ACTION_DIAL);

intent.setData(Uri.parse("tel:1320010001"));

startActivity(intent);

//调用拨号面板:

Intent intent=new Intent();

intent.setAction(I ntent.ACTION_VIEW);

intent.setData(Uri.parse("tel:1320010001"));

startActivity(intent);

(二)、利用Uri打开浏览器、打开地图等:

Uri uri = Uri.parse("https://www.baidu.com");//浏览器

Uriuri=Uri.parse("geo:39.899533,116.036476"); //打开地图定位

Intent intent = new Intent();

intent.setAction(Intent.ACTION_VIEW);

intent.setData(uri);

startActivity(intent);

()Type属性

1Type属性用于指定Data所指定的Uri对应的MIME类型。MIME只要符合“abc /xyz”这样的字符串格式即可。

2Intent利用ActionDataType属性启动Android系统内置组件的代码:

播放视频:

Intent intent = new Intent();

Uri uri =Uri.parse("file:///sdcard/media.mp4");

intent.setAction(Intent.ACTION_VIEW);

intent.setDataAndType(uri,"video/*");

startActivity(intent);

()Extra属性

1、通过intent.putExtra(, )的形式在多个Activity之间进行数据交换。

2、系统内置的几个Extra常量

EXTRA_BCC:存放邮件密送人地址的字符串数组。

EXTRA_CC:存放邮件抄送人地址的字符串数组。

EXTRA_EMAIL :存放邮件地址的字符串数组。

EXTRA_SUBJECT:存放邮件主题字符串。

EXTRA_TEXT:存放邮件内容。

EXTRA_KEY_EVENT:以KeyEvent对象方式存放触发Intent 的按键。

EXTRA_PHONE_ NUMBER :存放调用ACTION_CALL 时的电话号码。

3 Intent 利用ActionDataTypeExtra属性启动Android系统内置组件的代码:

调用发送短信的程序

Intent intent = new Intent();

intent.setAction(Intent.ACTION_VIEW);

intent.setType("vnd.android-dir/mms-sms");

intent.putEx tra("sms_body","信息内容...");

startActivity(intent);

//发送短信息

Uri uri =Uri.parse("smsto:13200100001");

Intent intent = new Intent();

intent.setAction(Intent.ACTION_SENDTO);

intent.setData(uri);

intent.putEx tra("sms_body","信息内容...");

startActivity( intent );

//发送彩信,设备会提示选择合适的程序发送

Uri uri = Uri.parse("content://media/external/p_w_picpaths/media/23"); //设备中的资源(图像或其他资源)

Intent intent = new Intent();

intent.setAction(Intent. ACTION_SEND );

intent.setType("p_w_picpath/png");

intent.putExtra("sms_body","内容");

intent.putExtra(Intent.EX TRA_STREAM,uri);

startActivity(it);

发送Email

Intent intent=new Intent();

intent.setAction(I ntent.ACTION_SEND);

String[]tos={"android1@163.com"}

String[]ccs={"you@yahoo.com"};

intent.putExtra(Intent.EXTRA_EMAIL, tos);

intent.putExtra(Intent.EXTRA_CC, ccs);

intent.putExtra(Intent.EXTRA_TEX T,"The email body text");

intent.putExtra(Intent.EXTRA_SU BJ ECT,"The email subject text");

intent.setType("message/rfc822");

startActivity(Intent.createChooser(intent,"Choose Email Client"));

Intent intent = newIntent(Intent.ACTION_SEND);

String[] tos = {"mobileservice@ablesky.com"};

intent.putExtra(I ntent.EXTRA_EMAIL, tos);

intent.putExtra(I ntent.EXTRA_TEXT,getPhoneParameter());

intent.putExtra(Intent.EXTRA_SUBJECT,"Android日志");

intent.putEx tra(Intent.EXTRA_STREAM, Uri.fromFile(cacheDir));

intent.setType("message/rfc882");

intent.setType("plain/text");

Intent.createChooser(intent, "请选择邮件发送软件");

startActivity(intent);

intent.setAction(android.provider.Settings.ACTION_SETTINGS)


4 Intent利用Action属性中的ACTION_GET_CONTENT获取返回值

//选择图片requestCode返回的标识

Intent intent = new I ntent();

intent.setAction(I ntent.ACTION_GET_CONTENT );

intent.setType( "p_w_picpath/* " );

Intent wrapperIntent = Intent.createChooser(intent, null);

startActivityForResult(wrapperIntent,requestCode);

//添加音频

Intent intent = new Intent();

intent.setAction(Intent.ACTION_GET_CONTENT);

intent.setType( "video/* " );

Intent wrapperIntent = Intent.createChooser(intent, null);

startActivityForResult(wrapperIntent,requestCode);

//视频

Intent intent = new Intent();

intent.setAction(Intent.ACTION_GET_CONTENT);

intent.setType( "video/* ");

Intent wrapperIntent = Intent.createChooser(intent, null);

startActivityForResult(wrapperI ntent,requestCode);

//录音

Intent intent = new Intent();

intent.setAction(Intent.ACTION_GET_CONTENT );

intent.setType( "audio/amr" );

intent.setClassName("com.android.soundrecorder","com.android.soundrecorder.SoundRecorder");

startActivityForResult(intent, requestCode);

(七)、Flags 属性:Intent 可调用addFlags()方法来为Intent 添加控制标记。【重要】

1 FLAG_ ACTIVITY_CLEAR_TOP:(效果同Activity LaunchModesingleTask

如果在栈中已经有该Activity的实例,就重用该实例。重用时,会让该实例回到栈顶,因此在它上面的实例将会被移除栈。如果栈中不存在该实例,将会创建新的实例放入栈中。

2 FLAG_ACTIVITY_SINGLE_TOP:(效果同Activity L aunchModesingleTop

如果在任务的栈顶正好存在该Activity的实例, 就重用该实例,而不会创建新的Activity 对象。

3 FLAG_ ACTIVITY_NEW_TASK:

【备注:】以下几个为了解。

4 FLAG_ACTIVITY_MULTIPLE_TASK:

5 FLAG_ACTIVITY_BROUGHT_TO_FRONT:

6 FLAG_ACTIVITY_RESET_TASK_IF_NEEDED:

示例代码:

Intent intent = new Intent(this,MainActivity.class);

//Activity栈中处于MainActivity主页面之上的Activity都弹出。

intent.setFlags(Intent.FLAG_ACTI VI TY_CLEAR_TOP);

startActivity(intent);

例如:

如果依次启动了四个Activity ABC D

D Activity 里,跳到B Activity,同时希望DC finish掉,可以在startActivity (intent )里的intent 里添加flags标记,如下所示:

Intent intent = new Intent(this , B.class);   

intent .setFlags (Intent .FLAG_ACTIVITY_CLEAR_TOP); 

s t ar t Activity(intent );

这样启动B  Activity的同时,就会把DC finis hed掉。

如果B  ActivitylaunchMode是默认的“st andar d”,则B  Activity会首先finis hed掉旧的B页面,再启动一个新的Activity   B如果不想重新再创建一个新的B  Activity,而是重用之前的B  Activity,可以将B  ActivitylaunchMode设置为“singleTask”。【特别需要注意的是:在部分手机中,如三星手机。即便是singleTask也会产生新的页面,而不是重用之前的页面。】


四、利用Intent 属性调用系统app的示例代码:

1、布局核心代码:

<ScrollView
      android:id="@+id/ScrollView1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical" >

          <Button
              android:id="@+id/Button_main_call "
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:onClick="clickButton"
              android:text=" 直接拨号" />

          <Button
              android:id="@+id/Button_main_dial"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:onClick="clickButton"
              android:text=" 启动拨号面板" />

          <Button
              android:id="@+id/Button_main_dialer"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:onClick="clickButton"
              android:text="显示拨号面板" />

          <Button
              android:id="@+id/Button_main_sms"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:onClick="clickButton "
              android:text="发送短信" />

          <Button
              android:id="@+id/Button_main_setting"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:onClick="clickButton "
              android:text="系统设置" />

          <Button
              android:id="@+id/Button_main_datesetting"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:onClick="clickButton"
              android:text=" 日期设置" />

          <Button
              android:id="@+id/Button_main_soundsetting"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:onClick="clickButton"
              android:text=" 声音设置" />

          <Button
              android:id="@+id/Button_main_wifisetting"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:onClick="clickButton"
              android:text=" W I F I 设置" />

          <Button
              android:id="@+id/Button_main_web"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:onClick="clickButton"
              android:text=" 浏览网页" />

          <Button
              android:id="@+id/Button_main_contacts"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:onClick="clickButton"
              android:text=" 查看联系人" />

          <Button
              android:id="@+id/Button_main_showp_w_picpath"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:onClick="clickButton"
              android:text=" 查看图片" />

          <Button
              android:id="@+id/Button_main_showtext"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:onClick="clickButton"
              android:text=" 查看文本" />

          <Button
              android:id="@+id/Button_main_playvideo"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:onClick="clickButton"
              android:text=" 播放视频" />

          <Button
              android:id="@+id/Button_main_playaudio"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:onClick="clickButton"
              android:text=" 播放音频" />

          <Button
              android:id="@+id/Button_main_home"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:onClick="clickButton"
              android:text=" H O M E " />
      </LinearLayout>
  </ScrollView>

2 java核心代码:

public class MainActivity extends Activity
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a);
	}

	public void clickButton(View view)
	{
		Intent intent = new Intent();
		// android.content.intent.ACTION_VIEW
		intent.setAction(android.content.Intent.ACTION_VIEW);
		switch (view.getId())
		{
		case R.id.Button_main_call:
			intent.setAction(android.content.Intent.ACTION_CALL);
			intent.setData(Uri.parse("tel:10086"));
			break;
		case R.id.Button_main_dial:
			intent.setAction(android.content.Intent.ACTION_DIAL);
			intent.setData(Uri.parse("tel:10086"));
			break;
		case R.id.Button_main_dialer:
			intent.setAction("com.android.phone.action.TOUCH_DIALER");
			break;
		case R.id.Button_main_sms:
			intent.setAction(android.content.Intent.ACTION_SENDTO);
			intent.setData(Uri.parse("smsto:10086"));
			intent.putExtra("sms_body", "该吃饭了,下课吧!");
			break;
		case R.id.Button_main_setting:
			intent.setAction("android.settings.settings");
			break;
		case R.id.Button_main_datesetting:
			intent.setAction("android.settings.DATE_settingS");
			break;
		case R.id.Button_main_soundsetting:
			intent.setAction("android.settings.SOUND_settingS");
			break;
		case R.id.Button_main_wifisetting:
			intent.setAction("android.settings.WIFI_settings");
			break;
		case R.id.Button_main_contacts:
			intent.setAction("com.android.contacts.action.LIST_contacts");
			break;
		case R.id.Button_main_web:
			intent.setAction(android.content.Intent.ACTION_VIEW);
			intent.setData(Uri
					.parse("http://www.baidu.com"));
			break;
		case R.id.Button_main_showp_w_picpath:
			intent.setAction(android.content.Intent.ACTION_VIEW);
			intent.setDataAndType(
					Uri.fromFile(new File(
							"mnt/sdcard/Download/landscape.jpg")),
					"p_w_picpath/*");
			break;
		case R.id.Button_main_showtext:
			intent.setAction(android.content.Intent.ACTION_VIEW);
			intent.setDataAndType(
					Uri.fromFile(new File(
							"mnt/sdcard/Download/info.txt")),
					"text/*");
			break;
		case R.id.Button_main_playaudio:
			intent.setAction(android.content.Intent.ACTION_VIEW);
			intent.setDataAndType(
					Uri.fromFile(new File(
							"mnt/sdcard/Download/heavencity.mp3")),
					"audio/*");
			break;
		case R.id.Button_main_playvideo:
			intent.setAction(android.content.Intent.ACTION_VIEW);
			intent.setDataAndType(
					Uri.fromFile(new File(
							"mnt/sdcard/Download/girl.3gp")),
					"video/*");
			break;
		case R.id.Button_main_home:
			intent.setAction("android.intent.action.main");
			intent.addCategory("android.intent.category.HOME");
			break;
		default:
			break;
		}
		startActivity(intent);
	}
}


推荐阅读:
  1. Android之Intent(二)
  2. Android中如何使用Intent

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

属性 android intent

上一篇:测试MySQL数据库性能有什么工具

下一篇:满足MySQL复制表结构和数据需求的代码怎么写

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》