Android之Intent全面解析及用法

来源:互联网 发布:java代码如何添加log4j 编辑:程序博客网 时间:2024/05/17 06:41

Intent对Android的核心和灵魂,是各组件之间的桥梁。四大组件分别为Activity 、Service、BroadcastReceiver、ContentProvider。
而这四种组件是独立的,它们之间可以互相调用,协调工作,最终组成一个真正的Android应用。

Intent的中文意思为“意图”,在Android中可以理解为想要做什么,What do want to do? 所以什么时候要用到Intent就很好理解了。

[javascript] view plaincopy
  1. 显示网页:Uri uri = Uri.parse("http://www.google.com");   
  2. Intent it   = new Intent(Intent.ACTION_VIEW,uri);   
  3. startActivity(it);   
  4.   
  5. 显示地图:  
  6. Uri uri = Uri.parse("geo:38.899533,-77.036476");   
  7. Intent it = new Intent(Intent.Action_VIEW,uri);   
  8. startActivity(it);   
  9.   
  10. 路径规划:  
  11. Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");   
  12. Intent it = new Intent(Intent.ACTION_VIEW,URI);   
  13. startActivity(it);   
  14.   
  15. 拨打电话:  
  16. 调用拨号程序  
  17. Uri uri = Uri.parse("tel:xxxxxx");   
  18. Intent it = new Intent(Intent.ACTION_DIAL, uri);     
  19. startActivity(it);     
  20. Uri uri = Uri.parse("tel.xxxxxx");   
  21. Intent it =new Intent(Intent.ACTION_CALL,uri);   
  22. 要使用这个必须在配置文件中加入<uses-permission id="android.permission.CALL_PHONE" />   
  23.   
  24.   
  25.   
  26.   
  27. 发送SMS/MMS  
  28. 调用发送短信的程序  
  29.   
  30. Intent it = new Intent(Intent.ACTION_VIEW);   
  31. it.putExtra("sms_body""The SMS text");   
  32. it.setType("vnd.android-dir/mms-sms");   
  33. startActivity(it);    
  34.    
  35. 发送短信  
  36. Uri uri = Uri.parse("smsto:0800000123");   
  37. Intent it = new Intent(Intent.ACTION_SENDTO, uri);   
  38. it.putExtra("sms_body""The SMS text");   
  39. startActivity(it);     
  40.   
  41. 发送彩信  
  42. Uri uri = Uri.parse("content://media/external/images/media/23");   
  43. Intent it = new Intent(Intent.ACTION_SEND);   
  44. it.putExtra("sms_body""some text");   
  45. it.putExtra(Intent.EXTRA_STREAM, uri);   
  46. it.setType("image/png");   
  47. startActivity(it);   
  48.   
  49. 发送Email  
  50. Uri uri = Uri.parse("mailto:xxx@abc.com");   
  51. Intent it = new Intent(Intent.ACTION_SENDTO, uri);   
  52. startActivity(it);   
  53. Intent it = new Intent(Intent.ACTION_SEND);   
  54. it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");   
  55. it.putExtra(Intent.EXTRA_TEXT, "The email body text");   
  56. it.setType("text/plain");   
  57. startActivity(Intent.createChooser(it, "Choose Email Client"));     
  58. Intent it=new Intent(Intent.ACTION_SEND);       
  59. String[] tos={"me@abc.com"};       
  60. String[] ccs={"you@abc.com"};       
  61. it.putExtra(Intent.EXTRA_EMAIL, tos);       
  62. it.putExtra(Intent.EXTRA_CC, ccs);       
  63. it.putExtra(Intent.EXTRA_TEXT, "The email body text");       
  64. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");       
  65. it.setType("message/rfc822");       
  66. startActivity(Intent.createChooser(it, "Choose Email Client"));   
  67.   
  68. 添加附件  
  69. Intent it = new Intent(Intent.ACTION_SEND);   
  70. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");   
  71. it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");   
  72. sendIntent.setType("audio/mp3");   
  73. startActivity(Intent.createChooser(it, "Choose Email Client"));   
  74.   
  75. 播放多媒体  
  76. Intent it = new Intent(Intent.ACTION_VIEW);   
  77. Uri uri = Uri.parse("file:///sdcard/song.mp3");   
  78. it.setDataAndType(uri, "audio/mp3");   
  79. startActivity(it);   
  80. Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");   
  81. Intent it = new Intent(Intent.ACTION_VIEW, uri);   
  82. startActivity(it);  
  83.      
  84. Uninstall 程序  
  85. Uri uri = Uri.fromParts("package", strPackageName, null);   
  86. Intent it = new Intent(Intent.ACTION_DELETE, uri);   
  87. startActivity(it);   
  88.   
  89. Install 程序  
  90. Uri installUri = Uri.fromParts("package""xxx"null);   
  91. returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);   
[javascript] view plaincopy
  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     
  6. //显示指定应用的详细页面(这个好像不支持了,找不到app_id)   
  7. Uri uri = Uri.parse("market://details?id=app_id");     
  8. Intent it = new Intent(Intent.ACTION_VIEW, uri);     
  9. startActivity(it);     
  10. //where app_id is the application ID, find the ID     
  11. //by clicking on your application on Market home     
  12. //page, and notice the ID from the address bar   


 

[java] view plaincopy
  1. 打开另一程序  
  2. Intent i = new Intent();      
  3.    ComponentName cn = new ComponentName("com.drip.android2",      
  4.                    "com.drip.android2.AndroidSearch");      
  5.          i.setComponent(cn);      
  6.          i.setAction("android.intent.action.MAIN");      
  7.          startActivityForResult(i, RESULT_OK);   
  8.    
  9. 打开联系人列表  
  10. <1>                
  11.     Intent i = new Intent();      
  12.     i.setAction(Intent.ACTION_GET_CONTENT);      
  13.     i.setType("vnd.android.cursor.item/phone");      
  14.     startActivityForResult(i, REQUEST_TEXT);      
  15.     
  16.    <2>      
  17.    Uri uri = Uri.parse("content://contacts/people");      
  18.    Intent it = new Intent(Intent.ACTION_PICK, uri);      
  19.    startActivityForResult(it, REQUEST_TEXT);      
  20. 打开录音机  
  21. Intent mi = new Intent(Media.RECORD_SOUND_ACTION);      
  22.          startActivity(mi);   
  23.    
  24.   从gallery选取图片     
  25. Inten t i = new Intent();    
  26.   i.setType("image/*");      
  27.        i.setAction(Intent.ACTION_GET_CONTENT);      
  28.        startActivityForResult(i, 11);    
  29.    
  30. 打开照相机  
  31. <1>Intent i = new Intent(Intent.ACTION_CAMERA_BUTTON, null);      
  32.           this.sendBroadcast(i);      
  33.     <2>long dateTaken = System.currentTimeMillis();      
  34.          String name = createName(dateTaken) + ".jpg";      
  35.          fileName = folder + name;      
  36.          ContentValues values = new ContentValues();      
  37.          values.put(Images.Media.TITLE, fileName);      
  38.          values.put("_data", fileName);      
  39.          values.put(Images.Media.PICASA_ID, fileName);      
  40.          values.put(Images.Media.DISPLAY_NAME, fileName);      
  41.          values.put(Images.Media.DESCRIPTION, fileName);      
  42.          values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, fileName);      
  43.          Uri photoUri = getContentResolver().insert(      
  44.                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);      
  45.                 
  46.          Intent inttPhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);      
  47.          inttPhoto.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);      
  48.          startActivityForResult(inttPhoto, 10);    
  49.     
  50. 播放多媒体  
  51. Intent it = new Intent(Intent.ACTION_VIEW);      
  52. Uri uri = Uri.parse("file:///sdcard/song.mp3");      
  53. it.setDataAndType(uri, "audio/mp3");      
  54. startActivity(it);      
  55.   
  56. 或者     
  57. Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");        
  58. Intent it = new Intent(Intent.ACTION_VIEW, uri);        
  59. startActivity(it);      
  60.       
  61. 或者     
  62. Uri playUri = Uri.parse("file:///sdcard/download/everything.mp3");     
  63. returnIt = new Intent(Intent.ACTION_VIEW, playUri);     
  64. 发送Email  
  65. Uri uri = Uri.parse("mailto:xxx@abc.com");  
  66. Intent it = new Intent(Intent.ACTION_SENDTO, uri);  
  67. startActivity(it);  
  68.    
  69. 或者:  
  70. Intent intent = new Intent(Intent.ACTION_SEND);  
  71. intent.putExtra(Intent.EXTRA_EMAIL, address);  
  72. intent.putExtra(Intent.EXTRA_SUBJECT, filename);  
  73. intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + filename)); ;  
  74. intent.setType("text/csv");  
  75. startActivity(Intent.createChooser(intent, "EMail File"));  
  76.    
  77. 或者:  
  78. Intent it = new Intent(Intent.ACTION_SEND);  
  79. it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");  
  80. it.putExtra(Intent.EXTRA_TEXT, "The email body text");  
  81. it.setType("text/plain");  
  82. startActivity(Intent.createChooser(it, "Choose Email Client"));  
  83.    
  84. 或者:  
  85. Intent it=new Intent(Intent.ACTION_SEND);  
  86. String[] tos={"me@abc.com"};  
  87. String[] ccs={"you@abc.com"};  
  88. it.putExtra(Intent.EXTRA_EMAIL, tos);  
  89. it.putExtra(Intent.EXTRA_CC, ccs);  
  90. it.putExtra(Intent.EXTRA_TEXT, "The email body text");  
  91. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");  
  92. it.setType("message/rfc822");  
  93. startActivity(Intent.createChooser(it, "Choose Email Client"));  
  94.    
  95. 或者:  
  96. Intent it = new Intent(Intent.ACTION_SEND);  
  97. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");  
  98. it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");  
  99. sendIntent.setType("audio/mp3");  
  100. startActivity(Intent.createChooser(it, "Choose Email Client"));  
  101.   
  102.  后台发送短信  
  103. 注册权限  
  104. <uses-permission android:name="android.permission.SEND_SMS" />  
  105. 代码实现 :  
  106. // 获取信息内容  
  107. String message ;  
  108. // 移动运营商允许每次发送的字节数据有限,我们可以使用Android给我们提供 的短信工具。  
  109. if (message != null) {  
  110. SmsManager sms = SmsManager.getDefault();  
  111. // 如果短信没有超过限制长度,则返回一个长度的List。  
  112. List<String> texts = sms.divideMessage(message);  
  113. for (String text : texts) {  
  114. sms.sendTextMessage(“这里是接收者电话号码”,  “这里是发送者电话号码”,  “这里是短信内容”,  nullnull);  
  115. }  
  116. }  
  117. //说明  
  118. sms.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent):  
  119. destinationAddress:接收方的手机号码  
  120. scAddress:发送方的手机号码  
  121. text:信息内容  
  122. sentIntent:发送是否成功的回执,  
  123. DeliveryIntent:接收是否成功的回执,  
  124.    
  125. 从google搜索内容  
  126. Intent intent = new Intent();      
  127. intent.setAction(Intent.ACTION_WEB_SEARCH);      
  128. intent.putExtra(SearchManager.QUERY,"searchString")      
  129. startActivity(intent);    
[java] view plaincopy
  1. APK 安装  
  2. Intent intent =new Intenet();  
  3.   
  4. String filepath="/SDCard/XXX.apk";  
[java] view plaincopy
  1. intent.setDataAndType(Uri.parse("file://" + filepath),"application/vnd.android.package-archive");  
  2. starActivity(intent);  

Activity之间的传递

[java] view plaincopy
  1. Intent intent=new Intent();  
  2. intent.setClass(MainActivity.this, SecondActivity.class);  
  3. startActivity(intent);
0 0