当前位置:   article > 正文

【Android】-- Intent(显式和隐式Intent)

隐式intent

什么是Intent?

Intent是各个组件之间信息沟通的桥梁,它用于Android各组件之间的通信,主要完成下列工作:

  • 标明本次通信请求从哪里来、到哪里去、要怎么走。
  • 发起方携带本次通信需要的数据内容,接收方从收到的意图中解析数据。
  • 发起方若想判断接收方的处理结果,意图就要负责让接收方传回应答的数据内容。

 Intent的组成部分


一、显式Intent和隐式Intent

1、显式Intent

显式Intent,直接指定来源活动与目标活动,属于精确匹配,有三种构建方式:

  • 在Intent的构造函数中指定。
  • 调用意图对象的setClass方法指定。
  • 调用意图对象的setComponent方法指定。

 (1)在Intent构造函数中指定

例:

Intent intent = new Intent(this,ActNextActivity.class)//创建一个目标确定的意图

(2)调用意图对象的setClass方法指定

例:

  1. Intent intent = new Intent();//创建新意图
  2. intent.setClass(this,ActNextActivity.class)//设置意图要跳转的目标活动

(3)调用意图对象的setComponent方法指定

例:

  1. Intent intent = new Intent();//创建新意图
  2. //创建包含目标活动在内的组件名称对象
  3. ComponentName component = new ComponentName(this,ActNextActivity.class);
  4. intent.setComponent(component);//设置意图携带的组件信息

2、隐式Intent

没有明确指定要跳转的目标活动,只给出一个动作字符串让系统自动匹配,属于模糊匹配

通常APP不希望向外部暴露活动名称,只给出一个事先定义好的标记串,这个动作名称标记串,可以是自己定义的动作,可以是已有的系统动作,常见系统动作取值如下:

 例:

java

  1. public class ActionUrlActivity extends AppCompatActivity implements View.OnClickListener {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_action_url);
  6. findViewById(R.id.btn_dial).setOnClickListener(this);
  7. findViewById(R.id.btn_sms).setOnClickListener(this);
  8. findViewById(R.id.btn_my).setOnClickListener(this);
  9. }
  10. @Override
  11. public void onClick(View view) {
  12. String phoneNo = "12345";
  13. Intent intent = new Intent();
  14. switch (view.getId()){
  15. case R.id.btn_dial:
  16. //设置意图动作为准备拨号
  17. intent.setAction(Intent.ACTION_DIAL);
  18. Uri uri = Uri.parse("tel:"+phoneNo);
  19. intent.setData(uri);
  20. startActivity(intent);
  21. break;
  22. case R.id.btn_sms:
  23. //设置意图动作为发短信
  24. intent.setAction(Intent.ACTION_SENDTO);
  25. Uri uri2 = Uri.parse("smsto:"+phoneNo);
  26. intent.setData(uri2);
  27. startActivity(intent);
  28. break;
  29. case R.id.btn_my:
  30. intent.setAction("android.intent.action.NING");
  31. intent.addCategory(Intent.CATEGORY_DEFAULT);
  32. startActivity(intent);
  33. break;
  34. }
  35. }
  36. }

xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <TextView
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:padding="5dp"
  10. android:text="点击以下按钮向号码发起请求"/>
  11. <Button
  12. android:id="@+id/btn_dial"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:text="跳到拨号页面"/>
  16. <Button
  17. android:id="@+id/btn_sms"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:text="跳到短信页面"/>
  21. <Button
  22. android:id="@+id/btn_my"
  23. android:layout_width="match_parent"
  24. android:layout_height="wrap_content"
  25. android:text="跳到我的页面"/>
  26. </LinearLayout>

需要跳转到的自定义的页面的AndroidManifest.xml文件

  1. <activity
  2. android:name=".ButtonClickActivity"
  3. android:exported="true">//需要设置为true,意为允许其他应用跳转
  4. <intent-filter>
  5. <action android:name="android.intent.action.MAIN" />
  6. <category android:name="android.intent.category.LAUNCHER" />
  7. </intent-filter>
  8. //添加的代码:
  9. <intent-filter>
  10. <action android:name="android.intent.action.NING" />
  11. <category android:name="android.intent.category.DEFAULT" />
  12. </intent-filter>
  13. </activity>

 


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/236040
推荐阅读
相关标签
  

闽ICP备14008679号