文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Android基础学习之Intent

Android基础学习之Intent

时间:2011-03-10  来源:路人甲说

  Android用Intent这个特殊类实现Activity与Activity之间的切换。Intent类用于描述应用的功能。在Intent的描述结构中,有两个最重要的部分:动作和动作对应的数据。

  Intent作用的表现形式为:
      1、通过Context.startActivity() orActivity.startActivityForResult() 启动一个Activity;
      2、通过 Context.startService() 启动一个服务,或者通过Context.bindService() 和后台服务交互;
      3、通过广播方法(比如 Context.sendBroadcast(),Context.sendOrderedBroadcast(), Context.sendStickyBroadcast()) 发给broadcast receivers。

  Intent属性的设置,包括以下几点:(以下为XML中定义,当然也可以通过Intent类的方法来获取和设置)

一、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.

   当然,也可以自定义动作。

二、Data,也就是执行动作要操作的数据

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

三、type(数据类型),显式指定Intent的数据类型(MIME)。

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

四、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.

五、component(组件),指定Intent的的目标组件的类名称。

  通常 Android会根据Intent 中包含的其它属性的信息,比如action、data/type、category进行查找,最终找到一个与之匹配的目标组件。但是,如果 component这个属性有指定的话,将直接使用它指定的组件,而不再执行上述查找过程。指定了这个属性以后,Intent的其它所有属性都是可选的。

六、extras(附加信息),是其它所有附加信息的集合。

  Intent的解析机制可参考上一篇文章:http://www.cnblogs.com/lurenjiashuo/archive/2011/03/09/1978260.html

七、Android Intent常用用法汇总

  1、显示网页:

Uri uri = Uri.parse("http://www.cnblogs.com");  
Intent it
= new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);

  2、显示地图:

Uri uri = Uri.parse("geo:38.899533,-77.036476");  
Intent it
= new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);

  3、路径规划:

Uri uri = Uri.parse("http://maps.google.com/maps?                         f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");  
Intent it
= new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);

  4、打电话:

//叫出拨号程序 
Uri uri = Uri.parse("tel:0800000123");
Intent it
= new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);

//直接打电话出去
Uri uri = Uri.parse("tel:0800000123");
Intent it
= new Intent(Intent.ACTION_CALL, uri);
startActivity(it);
//用這個,要在 AndroidManifest.xml 中,加上
//<uses-permission id="android.permission.CALL_PHONE" />

  5、传送SMS/MMS:

//调用短信程序 
Intent it = new Intent(Intent.ACTION_VIEW, uri);
it.putExtra(
"sms_body", "The SMS text");
it.setType(
"vnd.android-dir/mms-sms");
startActivity(it);

//传送消息
Uri uri = Uri.parse("smsto://0800000123");
Intent it
= new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra(
"sms_body", "The SMS text");
startActivity(it);

//传送 MMS
Uri uri = Uri.parse("content://media/external/images/media/23");
Intent it
= new Intent(Intent.ACTION_SEND);
it.putExtra(
"sms_body", "some text");
it.putExtra(Intent.EXTRA_STREAM, uri);
it.setType(
"image/png");
startActivity(it);

  6、传送 Email:

//方法一
Uri uri = Uri.parse("mailto:[email protected]");
Intent it
= new Intent(Intent.ACTION_SENDTO, uri);
startActivity(it);


//方法二
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL,
"[email protected]");
it.putExtra(Intent.EXTRA_TEXT,
"The email body text");
it.setType(
"text/plain");
startActivity(Intent.createChooser(it,
"Choose Email Client"));

//方法三
Intent it=new Intent(Intent.ACTION_SEND);
String[] tos
={"[email protected]"};
String[] ccs
={"[email protected]"};
it.putExtra(Intent.EXTRA_EMAIL, tos);
it.putExtra(Intent.EXTRA_CC, ccs);
it.putExtra(Intent.EXTRA_TEXT,
"The email body text");
it.putExtra(Intent.EXTRA_SUBJECT,
"The email subject text");
it.setType(
"message/rfc822");
startActivity(Intent.createChooser(it,
"Choose Email Client"));

//传送附件
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_SUBJECT,
"The email subject text");
it.putExtra(Intent.EXTRA_STREAM,
"file:///sdcard/mysong.mp3");
sendIntent.setType(
"audio/mp3");
startActivity(Intent.createChooser(it,
"Choose Email Client"));

  7、播放多媒体:

//方法一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);

  8、Market 相关:

//寻找某个应用 
Uri uri = Uri.parse("market://search?q=pname:pkg_name");
Intent it
= new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//where pkg_name is the full package path for an application

//显示某个应用的相关信息
Uri uri = Uri.parse("market://details?id=app_id");
Intent it
= new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//where app_id is the application ID, find the ID
//by clicking on your application on Market home
//page, and notice the ID from the address bar

  9、Uninstall 应用程序:

Uri uri = Uri.fromParts("package", strPackageName, null); 
Intent it
= new Intent(Intent.ACTION_DELETE, uri);
startActivity(it);
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载