2017ESLdota2比赛解说:android Intent 多种用法全面介绍

来源:百度文库 编辑:偶看新闻 时间:2024/04/23 22:45:39
android Intent 多种用法全面介绍2010-06-01 9:14

第一种方式,用action来跳转。

  1、使用Action跳转,如果有一个程序的AndroidManifest.xml中的某一个 Activity的IntentFilter段中 定义了包含了相同的Action那么这个Intent就与这个目标Action匹配。如果这个IntentFilter段中没有定义 Type,Category,那么这个 Activity就匹配了。但是如果手机中有两个以上的程序匹配,那么就会弹出一个对话可框来提示说明。
Action 的值在Android中有很多预定义,如果你想直接转到你自己定义的Intent接收者,你可以在接收者的IntentFilter 中加入一个自定义的Action值(同时要设定 Category值为"android.intent.category.DEFAULT"),在你的Intent中设定该值为Intent的 Action,就直接能跳转到你自己的Intent接收者中。因为这个Action在系统中是唯一的。

  2,data/type,你可以用Uri 来做为data,比如Uri uri = Uri.parse(http://www.google.com);
Intent i = new Intent(Intent.ACTION_VIEW,uri);手机的Intent分发过程中,会根据http://www.google.com 的scheme判断出数据类型type
手机的Brower则能匹配它,在Brower的Manifest.xml中的IntenFilter中 首先有ACTION_VIEW Action,也能处理http:的type,

  3, 至于分类Category,一般不要去在Intent中设置它,如果你写Intent的接收者,就在Manifest.xml的Activity的 IntentFilter中包含android.category.DEFAULT,这样所有不设置 Category(Intent.addCategory(String c);)的Intent都会与这个Category匹配。

  4,extras(附 加信息),是其它所有附加信息的集合。使用extras可以为组件提供扩展信息,比如,如果要执行“发送电子邮件”这个动 作,可以将电子邮件的标题、正文等保存在extras里,传给电子邮件发送组件。

Java 代码

  1. package com.android.edit_text;  
  2. import android.app.Activity;  
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.view.KeyEvent;  
  6. import android.view.View;  
  7. import android.widget.EditText;  
  8. public class MyEditText extends Activity {  
  9.     private TextView m_TextView;  
  10.     private EditText m_EditText;  
  11.     /** Called when the activity is first created. */  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         m_EditText = (EditText) this.findViewById(R.id.EditText01);  
  17.         m_EditText.setOnKeyListener(editTextKeyListener);  
  18.     }  
  19.     private EditText.OnKeyListener editTextKeyListener = new EditText.OnKeyListener() {  
  20.         @Override  
  21.         public boolean onKey(View arg0, int arg1, KeyEvent arg2) {  
  22.             // action 跳转,需要在AndroidManifest.xml中配置action  
  23.             Intent i = new Intent("android.intent.action.mydialog");  
  24.             MyEditText.this.startActivity(i);  
  25.             return false;  
  26.         }  
  27.     };  
  28. }  

Xml代 码

  1.   
  2.     package="com.android.edit_text" android:versionCode="1"  
  3.     android:versionName="1.0">  
  4.       
  5.           
  6.               
  7.                   
  8.                   
  9.               
  10.           
  11.                   
  12.           
  13.               
  14.       
  15.                   
  16.                   
  17.               
  18.           
  19.       
  20.       
  21.    

第二种方式,用类名跳转。

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

  Intent传递过程中,要找 到目标消费者(另一个Activity,IntentReceiver或Service),也就是Intent的响 应者。

Java 代码

  1. package com.Android;  
  2. import android.app.Activity;  
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. public class FormStuff extends Activity {  
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.formstuff);  
  12.         final ImageButton button = (ImageButton) findViewById(R.id.android_button);  
  13.         button.setOnClickListener(new OnClickListener() {  
  14.             public void onClick(View v) {  
  15. // 用 类名跳转,需要在AndroidManifest.xml中申明activity  
  16.                 Intent intent = new Intent(FormStuff.this, HelloTabWidget.class);  
  17.                 startActivity(intent);  
  18.             }  
  19.         });  
  20. }  

Xml代 码

  1.   
  2.     package="com.Android" android:versionCode="1" android:versionName="1.0">  
  3.       
  4.           
  5.               
  6.                   
  7.                   
  8.               
  9.           
  10.           
  11.       
  12.   
  13.       
  14.    

下面列出几种Intent的用法
显示网页:

  1. Uri uri = Uri.parse("http://www.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);

路径规划:

  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);

拨打电话:
调用拨号程序

  1. Uri uri = Uri.parse("tel:xxxxxx");
  2. Intent it = new Intent(Intent.ACTION_DIAL, uri);  
  3. startActivity(it);  
  1. Uri uri = Uri.parse("tel.xxxxxx");
  2. Intent it =new Intent(Intent.ACTION_CALL,uri);
  3. 要使用这个必须在配置文件中加入

发送SMS/MMS
调用发送短信的程序

  1. Intent it = new Intent(Intent.ACTION_VIEW);
  2. it.putExtra("sms_body", "The SMS text");
  3. it.setType("vnd.android-dir/mms-sms");
  4. startActivity(it);  

发送短信

  1. Uri uri = Uri.parse("smsto:0800000123");
  2. Intent it = new Intent(Intent.ACTION_SENDTO, uri);
  3. it.putExtra("sms_body", "The SMS text");
  4. startActivity(it);  

发送彩信

  1. Uri uri = Uri.parse("content://media/external/images/media/23");
  2. Intent it = new Intent(Intent.ACTION_SEND);
  3. it.putExtra("sms_body", "some text");
  4. it.putExtra(Intent.EXTRA_STREAM, uri);
  5. it.setType("image/png");
  6. 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. Intent it = new Intent(Intent.ACTION_SEND);
  2. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
  3. it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");
  4. sendIntent.setType("audio/mp3");
  5. startActivity(Intent.createChooser(it, "Choose Email Client"));

播放多媒体

  1. Intent it = new Intent(Intent.ACTION_VIEW);
  2. Uri uri = Uri.parse("file:///sdcard/song.mp3");
  3. it.setDataAndType(uri, "audio/mp3");
  4. startActivity(it);
  1. Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
  2. Intent it = new Intent(Intent.ACTION_VIEW, uri);
  3. startActivity(it);  

Uninstall 程序

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

Install 程序

  1. Uri installUri = Uri.fromParts("package", "xxx", null);
  2. returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);

搜索应用

  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

参考资料:

http://wallage.blog.163.com/blog/static/1738962420104264340803/

http://www.eoeandroid.com/viewthread.php?tid=266

http://hi.baidu.com/weiyousheng/blog/item/41a5533b587451e614cecb68.html