Android学习笔记----Intent

冷不防 2021-11-09 08:01 479阅读 0赞

下面讲解一下Android中的Intent的内容

Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。Intent不仅可用于应用程序之间,也可用于应用程序内部的Activity/Service之间的交互。因此,Intent在这里起着一个媒体中介的作用,专门提供组件互相调用的相关信息,实现调用者与被调用者之间的解耦。在SDK中给出了Intent作用的表现形式为:

  • 通过Context.startActivity() orActivity.startActivityForResult() 启动一个Activity;
  • 通过 Context.startService() 启动一个服务,或者通过Context.bindService() 和后台服务交互;
  • 通过广播方法(比如 Context.sendBroadcast(),Context.sendOrderedBroadcast(), Context.sendStickyBroadcast()) 发给broadcast receivers。

1、Intent类似于消息、事件通知

2、Intent构成:action、category、data

(1)Action,也就是要执行的动作

SDk中定义了一些标准的动作,包括

















































onstant Target component Action
ACTION_CALL activity Initiate a phone call.
ACTION_EDIT activity Display data for the user to edit.
ACTION_MAIN activity Start up as the initial activity of a task, with no data input and no returned output.
ACTION_SYNC activity Synchronize data on a server with data on the mobile device.
ACTION_BATTERY_LOW broadcast receiver A warning that the battery is low.
ACTION_HEADSET_PLUG broadcast receiver A headset has been plugged into the device, or unplugged from it.
ACTION_SCREEN_ON broadcast receiver The screen has been turned on.
ACTION_TIMEZONE_CHANGED broadcast receiver The setting for the time zone has changed.

当然,也可以自定义动作(自定义的动作在使用时,需要加上包名作为前缀,如”com.example.project.SHOW_COLOR”),并可定义相应的Activity来处理我们的自定义动作。

(2)Data,也就是执行动作要操作的数据

Android中采用指向数据的一个URI来表示,如在联系人应用中,一个指向某联系人的URI可能为:content://contacts/1。对于不同的动作,其URI数据的类型是不同的(可以设置type属性指定特定类型数据),如ACTION_EDIT指定Data为文件URI,打电话为tel:URI,访问网络为http:URI,而由content provider提供的数据则为content: URIs。

(3)type(数据类型),显式指定Intent的数据类型(MIME)。一般Intent的数据类型能够根据数据本身进行判定,但是通过设置这个属性,可以强制采用显式指定的类型而不再进行推导。

(4)category(类别),被执行动作的附加信息。例如 LAUNCHER_CATEGORY 表示Intent 的接受者应该在Launcher中作为顶级应用出现;而ALTERNATIVE_CATEGORY表示当前的Intent是一系列的可选动作中的一个,这些动作可以在同一块数据上执行。还有其他的为




























Constant Meaning
CATEGORY_BROWSABLE The target activity can be safely invoked by the browser to display data referenced by a link — for example, an image or an e-mail message.
CATEGORY_GADGET The activity can be embedded inside of another activity that hosts gadgets.
CATEGORY_HOME The activity displays the home screen, the first screen the user sees when the device is turned on or when the HOME key is pressed.
CATEGORY_LAUNCHER The activity can be the initial activity of a task and is listed in the top-level application launcher.
CATEGORY_PREFERENCE The target activity is a preference panel.

3、Intent是Activity、service、broast receiver之间的桥梁

看下图:

2011052901353398.jpg

Intent也有两类:

1、显式:指定具体的目标组件处理(即在构造Intent对象时就指定接收者,下面制定了AnotherActivity为接受者)

–startActivity( new Intent(ActivityLifecycle.this, AnotherActivity.class));

2、隐式:由系统接受并决定如何处理 (即Intent的发送者在构造Intent对象时,并不知道也不关心接收者是谁,有利于降低发送者和接收者之间的耦合。明白?)

–startActivity( new Intent(Intent. ACTION_DIAL));


Intent filter: action、category、data

2011052901420034.jpg

Filter分三类,action、category、data

在filter中定义相关的匹配规则,告知系统对应的component可以接受哪些intent

2011052901442138.jpg

比如:打电话的时候action是dial,data是134XXXXXXXX;百度查询的时候action是View,data是 http://www.baidu.com

Intent用法实例

1.无参数Activity跳转

  1. Intent it = new Intent(Activity.Main.this, Activity2.class);
  2. startActivity(it);

2.向下一个Activity传递数据(使用Bundle和Intent.putExtras)

  1. Intent it = new Intent(Activity.Main.this, Activity2.class);
  2. Bundle bundle=new Bundle();
  3. bundle.putString("name", "This is from MainActivity!");
  4. it.putExtras(bundle); // it.putExtra(“test”, "shuju”);
  5. startActivity(it); // startActivityForResult(it,REQUEST_CODE);

对于数据的获取可以采用:

  1. Bundle bundle=getIntent().getExtras();
  2. String name=bundle.getString("name");

3.向上一个Activity返回结果(使用setResult,针对startActivityForResult(it,REQUEST_CODE)启动的Activity)

  1. Intent intent=getIntent();
  2. Bundle bundle2=new Bundle();
  3. bundle2.putString("name", "This is from ShowMsg!");
  4. intent.putExtras(bundle2);
  5. setResult(RESULT_OK, intent);

4.回调上一个Activity的结果处理函数(onActivityResult)

  1. @Override
  2. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  3. // TODO Auto-generated method stub
  4. super.onActivityResult(requestCode, resultCode, data);
  5. if (requestCode==REQUEST_CODE){
  6. if(resultCode==RESULT_CANCELED)
  7. setTitle("cancle");
  8. else if (resultCode==RESULT_OK) {
  9. String temp=null;
  10. Bundle bundle=data.getExtras();
  11. if(bundle!=null) temp=bundle.getString("name");
  12. setTitle(temp);
  13. }
  14. }
  15. }

下面是转载来的其他的一些Intent用法实例(转自javaeye)

显示网页

  1. Uri uri = Uri.parse(“http://google.com“);
  2. Intent it = new Intent(Intent.ACTION_VIEW, uri);
  3. startActivity(it);

显示地图

  1. Uri uri = Uri.parse(“geo:38.899533,-77.036476”);
  2. Intent it = new Intent(Intent.ACTION_VIEW, uri);
  3. startActivity(it);
  4. //其他 geo URI 範例
  5. //geo:latitude,longitude
  6. //geo:latitude,longitude?z=zoom
  7. //geo:0,0?q=my+street+address
  8. //geo:0,0?q=business+near+city
  9. //google.streetview:cbll=lat,lng&cbp=1,yaw,,pitch,zoom&mz=mapZoom

路径规划

  1. Uri uri = Uri.parse(“http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en“);
  2. Intent it = new Intent(Intent.ACTION_VIEW, uri);
  3. startActivity(it);
  4. //where startLat, startLng, endLat, endLng are a long with 6 decimals like: 50.123456

打电话

  1. //叫出拨号程序
  2. Uri uri = Uri.parse(“tel:0800000123”);
  3. Intent it = new Intent(Intent.ACTION_DIAL, uri);
  4. startActivity(it);
  5. //直接打电话出去
  6. Uri uri = Uri.parse(“tel:0800000123”);
  7. Intent it = new Intent(Intent.ACTION_CALL, uri);
  8. startActivity(it);
  9. //用這個,要在 AndroidManifest.xml 中,加上
  10. //

传送SMS/MMS

  1. //调用短信程序
  2. Intent it = new Intent(Intent.ACTION_VIEW, uri);
  3. it.putExtra(“sms_body”, “The SMS text”);
  4. it.setType(“vnd.android-dir/mms-sms”);
  5. startActivity(it);
  6. //传送消息
  7. Uri uri = Uri.parse(“smsto://0800000123”);
  8. Intent it = new Intent(Intent.ACTION_SENDTO, uri);
  9. it.putExtra(“sms_body”, “The SMS text”);
  10. startActivity(it);
  11. //传送 MMS
  12. Uri uri = Uri.parse(“content://media/external/images/media/23”);
  13. Intent it = new Intent(Intent.ACTION_SEND);
  14. it.putExtra(“sms_body”, “some text”);
  15. it.putExtra(Intent.EXTRA_STREAM, uri);
  16. it.setType(“image/png”);
  17. startActivity(it);

传送 Email

  1. Uri uri = Uri.parse(“mailto:xxx@abc.com”);
  2. Intent it = new Intent(Intent.ACTION_SENDTO, uri);
  3. startActivity(it);
  1. Intent it = new Intent(Intent.ACTION_SEND);
  2. it.putExtra(Intent.EXTRA_EMAIL, “me@abc.com”);
  3. it.putExtra(Intent.EXTRA_TEXT, “The email body text”);
  4. it.setType(“text/plain”);
  5. startActivity(Intent.createChooser(it, “Choose Email Client”));
  1. Intent it=new Intent(Intent.ACTION_SEND);
  2. String[] tos={“me@abc.com”};
  3. String[] ccs={“you@abc.com”};
  4. it.putExtra(Intent.EXTRA_EMAIL, tos);
  5. it.putExtra(Intent.EXTRA_CC, ccs);
  6. it.putExtra(Intent.EXTRA_TEXT, “The email body text”);
  7. it.putExtra(Intent.EXTRA_SUBJECT, “The email subject text”);
  8. it.setType(“message/rfc822”);
  9. startActivity(Intent.createChooser(it, “Choose Email Client”));
  1. //传送附件
  2. Intent it = new Intent(Intent.ACTION_SEND);
  3. it.putExtra(Intent.EXTRA_SUBJECT, “The email subject text”);
  4. it.putExtra(Intent.EXTRA_STREAM, “file:///sdcard/mysong.mp3”);
  5. sendIntent.setType(“audio/mp3”);
  6. startActivity(Intent.createChooser(it, “Choose Email Client”));

播放多媒体
Uri uri = Uri.parse(“file:///sdcard/song.mp3”);
Intent it = new Intent(Intent.ACTION_VIEW, uri);
it.setType(“audio/mp3”);
startActivity(it);
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, “1”);
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);

转载于:https://www.cnblogs.com/azai/archive/2011/05/29/2061601.html

发表评论

表情:
评论列表 (有 0 条评论,479人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Android—————Intent

    一、什么是Intent > Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将

    相关 Android Intent

    因为刚做完天气预报下一个项目还没有开始 组长还没有分配我应该写的部分 所以我就开始不知道干啥呢 刚好有个大三的学姐过来问我这几天有没有在学习 顺便给我指点一下 结果学姐问的问题

    相关 Android intent

    1、新建一个Intent 对象,用于跳转。 2、调用Intent对象的setClass()方法,实现界面跳转。 3、创建个Timer对象,注意:java.util中的。

    相关 Android学习笔记----Intent

    下面讲解一下Android中的Intent的内容 Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作、动作涉及数据、附加数