当前位置: 代码迷 >> Android >> Android Activity/Service Intent 通讯 跳转
  详细解决方案

Android Activity/Service Intent 通讯 跳转

热度:583   发布时间:2016-05-01 16:33:48.0
Android Activity/Service Intent 通信 跳转

第一种方式,用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. ?? ???
  23. ?? ?? ?? ?? ?? ?? ?? // action跳转,需要在AndroidManifest.xml中配置action
  24. ?? ?? ?? Intent i = new Intent("android.intent.action.mydialog");
  25. ?? ?? ?? MyEditText.this.startActivity(i);
  26. ?? ?? ??
  27. ?? ?? ?? return false;
  28. ?? ??? }
  29. };
  30. }
复制代码

Xml代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.android.edit_text" android:versionCode="1"
  4. android:versionName="1.0">
  5. <application android:icon="@drawable/icon" android:label="@string/app_name">
  6. ?? ??? <activity android:name=".MyEditText" android:label="@string/app_name">
  7. ?? ?? ?? <intent-filter>
  8. ?? ?? ?? ?? <action android:name="android.intent.action.MAIN" />
  9. ?? ?? ?? ?? <category android:name="android.intent.category.LAUNCHER" />
  10. ?? ?? ?? </intent-filter>
  11. ?? ??? </activity>
  12. ?? ?? ?? ?? <!--配置跳转activity-->
  13. ?? ??? <activity android:name="com.android.dialog.MyDialog">
  14. ?? ?? ?? <intent-filter>
  15. <!--配置action路径-->
  16. ?? ?? ?? ?? <action android:name="android.intent.action.mydialog" />
  17. ?? ?? ?? ?? <category android:name="android.intent.category.DEFAULT" />
  18. ?? ?? ?? </intent-filter>
  19. ?? ??? </activity>
  20. </application>
  21. <uses-sdk android:minSdkVersion="7" />
  22. </manifest>
复制代码

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

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. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.Android" android:versionCode="1" android:versionName="1.0">
  4. <application android:icon="@drawable/icon" android:theme="@android:style/Theme.NoTitleBar">
  5. ?? ??? <activity android:name=".FormStuff" android:label="@string/app_name">
  6. ?? ?? ?? <intent-filter>
  7. ?? ?? ?? ?? <action android:name="android.intent.action.MAIN" />
  8. ?? ?? ?? ?? <category android:name="android.intent.category.LAUNCHER" />
  9. ?? ?? ?? </intent-filter>
  10. ?? ??? </activity>
  11. ?? ??? <!--申明activity-->
  12. <activity android:name="HelloTabWidget"></activity>
  13. </application>
  14. <uses-sdk android:minSdkVersion="4" />
  15. </manifest>
复制代码

一些Intent的常用发:

Java代码

  1. 显示网页
  2. 1. Uri uri = Uri.parse("http://google.com");
  3. 2. Intent it = new Intent(Intent.ACTION_VIEW, uri);
  4. 3. startActivity(it);
  5. 显示地图
  6. 1. Uri uri = Uri.parse("geo:38.899533,-77.036476");
  7. 2. Intent it = new Intent(Intent.ACTION_VIEW, uri);
  8. 3. startActivity(it);
  9. 4. //其他 geo URI 範例
  10. 5. //geo:latitude,longitude
  11. 6. //geo:latitude,longitude?z=zoom
  12. 7. //geo:0,0?q=my+street+address
  13. 8. //geo:0,0?q=business+near+city
  14. 9. //google.streetview:cbll=lat,lng&amp;cbp=1,yaw,,pitch,zoom&amp;mz=mapZoom??
  15. 路径规划
  16. 1. Uri uri = Uri.parse("http://maps.google.com/maps?f=d&amp;saddr=startLat%20startLng&amp;daddr=endLat%20endLng&amp;hl=en");
  17. 2. Intent it = new Intent(Intent.ACTION_VIEW, uri);
  18. 3. startActivity(it);
  19. 4. //where startLat, startLng, endLat, endLng are a long with 6 decimals like: 50.123456
  20. 打电话
  21. 1. //叫出拨号程序??
  22. 2. Uri uri = Uri.parse("tel:0800000123");
  23. 3. Intent it = new Intent(Intent.ACTION_DIAL, uri);
  24. 4. startActivity(it);
  25. 1. //直接打电话出去
  26. 2. Uri uri = Uri.parse("tel:0800000123");
  27. 3. Intent it = new Intent(Intent.ACTION_CALL, uri);
  28. 4. startActivity(it);
  29. 5. //用這个,要在 AndroidManifest.xml 中,加上
  30. 6. //&lt;uses-permission id="android.permission.CALL_PHONE" /&gt;
  31. 传送SMS/MMS
  32. 1. //调用短信程序??
  33. 2. Intent it = new Intent(Intent.ACTION_VIEW, uri);
  34. 3. it.putExtra("sms_body", "The SMS text");
  35. 4. it.setType("vnd.android-dir/mms-sms");
  36. 5. startActivity(it);??
  37. 1. //传送消息??
  38. 2. Uri uri = Uri.parse("smsto://0800000123");
  39. 3. Intent it = new Intent(Intent.ACTION_SENDTO, uri);
  40. 4. it.putExtra("sms_body", "The SMS text");
  41. 5. startActivity(it);??
  42. 1. //传送 MMS
  43. 2. Uri uri = Uri.parse("content://media/external/images/media/23");
  44. 3. Intent it = new Intent(Intent.ACTION_SEND);
  45. 4. it.putExtra("sms_body", "some text");
  46. 5. it.putExtra(Intent.EXTRA_STREAM, uri);
  47. 6. it.setType("image/png");
  48. 7. startActivity(it);
  49. 传送 Email
  50. 1. Uri uri = Uri.parse("mailto:[email protected]");
  51. 2. Intent it = new Intent(Intent.ACTION_SENDTO, uri);
  52. 3. startActivity(it);
  53. 1. Intent it = new Intent(Intent.ACTION_SEND);
  54. 2. it.putExtra(Intent.EXTRA_EMAIL, "[email protected]");
  55. 3. it.putExtra(Intent.EXTRA_TEXT, "The email body text");
  56. 4. it.setType("text/plain");
  57. 5. startActivity(Intent.createChooser(it, "Choose Email Client"));
  58. 1. Intent it=new Intent(Intent.ACTION_SEND); ???
  59. 2. String[] tos={"[email protected]"}; ???
  60. 3. String[] ccs={"[email protected]"}; ???
  61. 4. it.putExtra(Intent.EXTRA_EMAIL, tos); ???
  62. 5. it.putExtra(Intent.EXTRA_CC, ccs); ???
  63. 6. it.putExtra(Intent.EXTRA_TEXT, "The email body text"); ???
  64. 7. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text"); ???
  65. 8. it.setType("message/rfc822"); ???
  66. 9. startActivity(Intent.createChooser(it, "Choose Email Client"));??
  67. 1. //传送附件
  68. 2. Intent it = new Intent(Intent.ACTION_SEND);
  69. 3. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
  70. 4. it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");
  71. 5. sendIntent.setType("audio/mp3");
  72. 6. startActivity(Intent.createChooser(it, "Choose Email Client"));??
  73. 播放多媒体
  74. ?? Uri uri = Uri.parse("file:///sdcard/song.mp3");
  75. ?? Intent it = new Intent(Intent.ACTION_VIEW, uri);
  76. ?? it.setType("audio/mp3");
  77. ?? startActivity(it);??
  78. ?? Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
  79. ?? Intent it = new Intent(Intent.ACTION_VIEW, uri);
  80. ?? startActivity(it);??
  81. Market 相关
  82. 1. ?? ??? //寻找某个应用??
  83. 2. ?? ??? Uri uri = Uri.parse("market://search?q=pname:pkg_name");??
  84. 3. ?? ??? Intent it = new Intent(Intent.ACTION_VIEW, uri);
  85. 4. ?? ??? startActivity(it);
  86. 5. ?? ??? //where pkg_name is the full package path for an application??
  87. 1. ?? ??? //显示某个应用的相关信息??
  88. 2. ?? ??? Uri uri = Uri.parse("market://details?id=app_id");
  89. 3. ?? ??? Intent it = new Intent(Intent.ACTION_VIEW, uri);??
  90. 4. ?? ??? startActivity(it);
  91. 5. ?? ??? //where app_id is the application ID, find the ID
  92. 6. ?? ??? //by clicking on your application on Market home
  93. 7. ?? ??? //page, and notice the ID from the address bar??
  94. Uninstall 应用程序
  95. 1. ?? ??? Uri uri = Uri.fromParts("package", strPackageName, null);??
  96. 2. ?? ??? Intent it = new Intent(Intent.ACTION_DELETE, uri);
  97. 3. ?? ??? startActivity(it);?
  相关解决方案