Intent用法简单总结

来源:互联网 发布:易企秀免费下载 mac 编辑:程序博客网 时间:2024/05/01 05:44

在一个Android应用中,主要是由四种组件组成的,这四种组件可参考“Android应用的构成”。而这四种组件是独立的,它们之间可以互相调用,协调工作,最终组成一个真正的Android应用。

Intent的中文意思是“意图,目的”的意思,可以理解为不同组件之间通信的“媒介”或者“信使”。

Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。因此,Intent在这里起着一个媒体中介的作用,专门提供组件互相调用的相关信息,实现调用者与被调用者之间的解耦。

例如,在一个联系人维护的应用中,当我们在一个联系人列表屏幕(假设对应的Activity为listActivity)上,点击某个联系人后,希望能够跳出此联系人的详细信息屏幕(假设对应的Activity为detailActivity)为了实现这个目的,listActivity需要构造一个 Intent,这个Intent用于告诉系统,我们要做“查看”动作,此动作对应的查看对象是“某联系人”,然后调用startActivity (Intent intent),将构造的Intent传入,系统会根据此Intent中的描述,到ManiFest中找到满足此Intent要求的Activity,系统会调用找到的 Activity,即为detailActivity,最终传入Intent,detailActivity则会根据此Intent中的描述,执行相应的操作。


抽象描述要描述什么


在Android参考文档中,对Intent的定义是执行某操作的一个抽象描述(确实很抽象)。我们先来看看这里的抽象描述,到底描述了什么。
首先,是要执行的动作(action)的一个简要描述,如VIEW_ACTION(查看)、EDIT_ACTION(修改)等,Android为我们定义了一套标准动作:
onstantTarget componentActionACTION_CALLactivityInitiate a phone call.ACTION_EDITactivityDisplay data for the user to edit.ACTION_MAINactivityStart up as the initial activity of a task, with no data input and no returned output.ACTION_SYNCactivitySynchronize data on a server with data on the mobile device.ACTION_BATTERY_LOWbroadcast receiverA warning that the battery is low.ACTION_HEADSET_PLUGbroadcast receiverA headset has been plugged into the device, or unplugged from it.ACTION_SCREEN_ONbroadcast receiverThe screen has been turned on.ACTION_TIMEZONE_CHANGEDbroadcast receiverThe setting for the time zone has changed.

此外,我们还可以根据应用的需要,定义我们自己的动作,并可定义相应的Activity来处理我们的自定义动作。

应用内的页面切换:

Intent intent=new Intent();

intent.setClass(目前的acitivy.this, 目标activity.class);

startActivity(intent);

显示网页
   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);  
   1. //直接打电话出去  
   2. Uri uri = Uri.parse("tel:0800000123");  
   3. Intent it = new Intent(Intent.ACTION_CALL, uri);  
   4. startActivity(it);  
   5. //用這個,要在 AndroidManifest.xml 中,加上  
   6. //<uses-permission id="android.permission.CALL_PHONE" /> 

传送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); 
   1. //传送消息 
   2. Uri uri = Uri.parse("smsto://0800000123");  
   3. Intent it = new Intent(Intent.ACTION_SENDTO, uri);  
   4. it.putExtra("sms_body", "The SMS text");  
   5. startActivity(it); 
   1. //传送 MMS  
   2. Uri uri = Uri.parse("content://media/external/images/media/23");  
   3. Intent it = new Intent(Intent.ACTION_SEND);   
   4. it.putExtra("sms_body", "some text");   
   5. it.putExtra(Intent.EXTRA_STREAM, uri);  
   6. it.setType("image/png");   
   7. 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);

Market 相关
1.        //寻找某个应用 
2.        Uri uri = Uri.parse("market://search?q=pname:pkg_name"); 
3.        Intent it = new Intent(Intent.ACTION_VIEW, uri);  
4.        startActivity(it);  
5.        //where pkg_name is the full package path for an application 
1.        //显示某个应用的相关信息 
2.        Uri uri = Uri.parse("market://details?id=app_id");  
3.        Intent it = new Intent(Intent.ACTION_VIEW, uri); 
4.        startActivity(it);  
5.        //where app_id is the application ID, find the ID   
6.        //by clicking on your application on Market home   
7.        //page, and notice the ID from the address bar

Uninstall 应用程序
1.        Uri uri = Uri.fromParts("package", strPackageName, null); 
2.        Intent it = new Intent(Intent.ACTION_DELETE, uri);   
3.        startActivity(it); 

以上部分或全部内容转载及参考至以下博客:

http://bbs.hiapk.com/thread-7959-1-1.html

http://www.cnblogs.com/feisky/archive/2010/01/16/1649081.html

http://www.uml.org.cn/mobiledev/201208211.asp

http://hualang.iteye.com/blog/983471

感谢他们的无私奉献。

本博客全部内容均可转载,转载请注明来源。版权归原作者所有,未经同意严禁用于任何商业用途。
微博:http://weibo.com/theworldsong
邮箱:theworldsong@foxmail.com

原创粉丝点击