Posted by:
努力记

努力记,通过记录,计划,让自己的努力可以触碰彩虹!

4,549

Android:Intent机制

  • 2014-01-30
  • 暂无评论

Android中提供了Intent机制来协助应用间通讯
Intent对动作,数据,进行描述,Android根据此Intent的描述,负责找到对应的组件,将数据传递给调用的组件,并完成组件的调用。
Intent不仅可以用于应用程序之间,也可以用户内部的Activity/service之间通讯

对应Activity:Context.startActivity() or Activity.startActivityForResult()启动一个Activity
对应Service:Context.startService() 启动一个服务 or Context.bindService() 与后台服务通讯
通用:Context.sendBroadcast() Context.sendOrderedBroadcast() Context.sendStickyBroadcast() 发送给广播接收者

Intent的属性:

Action(动作):

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”

Data(数据):
对于系统内置的应用通常用一种uri格式进行访问

  • 页面:http://
  • 联系人:content://contacts/people/xxx
  • 电话:tel:
  • 短信:smsto://
  • 彩信:content://
  • 邮件:mailto:
  • 播放:file:///
  • Market:market://

Type(类型):

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

Category(类别):

设定动作的附加信息

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.

Component(组成):

指定用于意图的组件类的显示名称,指定该属性,其他属性为可选

Extras(附加):

捆绑的额外信息,通过Bundle进行结构化

实例:
1.无参数Activity跳转

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

2.向下一个Activity传递数据

Intent it = new Intent(Activity.Main.this,Activity2.class);
Bundle bundle = new Bundle();
bundle.putString("name","This is from MainActivity!");
it.putExtras(bundle);
startActivity(it);

获取数据

Bundle bundle=getIntet().getExtras();
String name = bundle.getString("name");

3.向上一个Activity返回结果

(使用setResult,针对startActivityForResult(it,REQUEST_CODE)启动的Activity)

@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(requestCode==REQUEST_CODE){
if(resultCode==RESULT_CANCELED)
setTitle("cancel");
else if(resultCode==RESULT_OK){
String temp=null;
Bundle bundle=data.getExtras();
if(bundle!=null) temp=bundle.getString("name");
setTitle(temp);
}
}
}

 



back up ↑

无觅相关文章插件,快速提升流量