Android显式意图、隐式意图、意图过滤器(intent-filter)、意图间传值

2024-06-11 18:58

本文主要是介绍Android显式意图、隐式意图、意图过滤器(intent-filter)、意图间传值,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

intent主要包括隐式意图和显式意图。显式意图通常主要是启动本应用中的Activity之间的数据,而隐式意图则常见于启动系统中的某些特定的动作,比如打电话,发短信,或者是跨应用的Activity启动(如在QQ点击链接地址启动一个浏览器Activity)。

显式意图:调用Intent.setComponent()、Intent.setClass()、Intent.setClassName()方法明确指定了组件名的Intent为显式意图,显式意图明确指定了Intent应该传递给哪个组件。
隐式意图:没有明确指定组件名的Intent为隐式意图。 Android系统会根据隐式意图中设置的动作(action)、类别(category)、数据(URI和数据类型)找到最合适的组件来处理这个意图。

MainActivity.java
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package cn.android.intent;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.os.Bundle;  
  8. import android.view.KeyEvent;  
  9. import android.view.View;  
  10. import android.widget.TextView;  
  11.   
  12. public class NewActivity extends Activity {  
  13.   
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         // TODO Auto-generated method stub  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.other);  
  19.         Intent intent = getIntent();  
  20.         //Log.i("NewActivity", intent.getData().getPath());  
  21.         String title = intent.getStringExtra("title");  
  22.         double since = intent.getDoubleExtra("since"1995.5);  
  23.         TextView textView = (TextView) this.findViewById(R.id.textView);  
  24.         textView.setText("语言:" + title + ",发布时间:" + since + "。");  
  25.     }  
  26.       
  27.     public void closeActivity(View v) {  
  28.         Intent intent = new Intent();  
  29.         intent.putExtra("result"new Date().toString());  
  30.         setResult(501, intent);  
  31.         this.finish();  
  32.     }  
  33.     @Override  
  34.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  35.         if (keyCode == KeyEvent.KEYCODE_BACK) {  
  36.             closeActivity(null);  
  37.         }  
  38.         return true;  
  39.     }  
  40. }  

NewActivity.java
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package cn.android.intent;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.os.Bundle;  
  8. import android.view.KeyEvent;  
  9. import android.view.View;  
  10. import android.widget.TextView;  
  11.   
  12. public class NewActivity extends Activity {  
  13.   
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         // TODO Auto-generated method stub  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.other);  
  19.         Intent intent = getIntent();  
  20.         //Log.i("NewActivity", intent.getData().getPath());  
  21.         String title = intent.getStringExtra("title");  
  22.         double since = intent.getDoubleExtra("since"1995.5);  
  23.         TextView textView = (TextView) this.findViewById(R.id.textView);  
  24.         textView.setText("语言:" + title + ",发布时间:" + since + "。");  
  25.     }  
  26.       
  27.     public void closeActivity(View v) {  
  28.         Intent intent = new Intent();  
  29.         intent.putExtra("result"new Date().toString());  
  30.         setResult(501, intent);  
  31.         this.finish();  
  32.     }  
  33.     @Override  
  34.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  35.         if (keyCode == KeyEvent.KEYCODE_BACK) {  
  36.             closeActivity(null);  
  37.         }  
  38.         return true;  
  39.     }  
  40. }  

main.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <TextView  
  6.         android:layout_width="fill_parent"  
  7.         android:layout_height="wrap_content"  
  8.         android:text="@string/hello"  
  9.         android:id="@+id/textView" />  
  10.     <LinearLayout   
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="fill_parent"  
  13.         android:orientation="horizontal"  
  14.         android:gravity="center_horizontal"  
  15.         android:layout_below="@id/textView">  
  16.         <Button   
  17.             android:layout_width="wrap_content"  
  18.             android:layout_height="wrap_content"  
  19.             android:text="@string/button_display"  
  20.             android:id="@+id/buttonDisplay"  
  21.             android:onClick="openActivityDisplay" />  
  22.         <Button   
  23.             android:layout_width="wrap_content"  
  24.             android:layout_height="wrap_content"  
  25.             android:text="@string/button_hidden"  
  26.             android:onClick="openActivityHidden" />      
  27.     </LinearLayout>  
  28. </RelativeLayout>  

other.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <TableRow>  
  6.         <TextView  
  7.             android:layout_width="fill_parent"  
  8.             android:layout_height="wrap_content"  
  9.             android:text="@string/newActivity"  
  10.             android:id="@+id/textView" />  
  11.         <Button   
  12.             android:layout_width="wrap_content"  
  13.             android:layout_height="wrap_content"  
  14.             android:text="@string/button_close"  
  15.             android:onClick="closeActivity" />  
  16.     </TableRow>  
  17. </TableLayout>  

AndroidManifest.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="cn.android.intent"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="8" />  
  8.   
  9.     <application  
  10.         android:icon="@drawable/ic_launcher"  
  11.         android:label="@string/app_name" >  
  12.         <activity  
  13.             android:label="@string/app_name"  
  14.             android:name=".MainActivity" >  
  15.             <intent-filter >  
  16.                 <action android:name="android.intent.action.MAIN" />  
  17.   
  18.                 <category android:name="android.intent.category.LAUNCHER" />  
  19.             </intent-filter>  
  20.         </activity>  
  21.         <activity android:name=".NewActivity" android:label="@string/newActivity">  
  22.             <intent-filter>  
  23.                 <action android:name="cn.java.action.android" />  
  24.                 <action android:name="cn.java.action.windowsphone" />  
  25.                 <category android:name="android.intent.category.DEFAULT" />  
  26.                 <category android:name="cn.java.category.intent" />  
  27.                 <category android:name="cn.java.category.action" />  
  28.                 <data android:scheme="intent" android:host="www.android.com" />  
  29.                 <data android:mimeType="image/*" />  
  30.             </intent-filter>  
  31.         </activity>  
  32.     </application>  
  33.   
  34. </manifest>  

strings.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">Hello World, MainActivity!</string>  
  4.     <string name="newActivity">New Activity!</string>  
  5.     <string name="app_name">Android意图</string>  
  6.     <string name="button_display">打开显示意图</string>  
  7.     <string name="button_hidden">打开隐式意图</string>  
  8.     <string name="button_close">关闭窗口</string>  
  9. </resources>  

这篇关于Android显式意图、隐式意图、意图过滤器(intent-filter)、意图间传值的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1052017

相关文章

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

python中的显式声明类型参数使用方式

《python中的显式声明类型参数使用方式》文章探讨了Python3.10+版本中类型注解的使用,指出FastAPI官方示例强调显式声明参数类型,通过|操作符替代Union/Optional,可提升代... 目录背景python函数显式声明的类型汇总基本类型集合类型Optional and Union(py

Python中的filter() 函数的工作原理及应用技巧

《Python中的filter()函数的工作原理及应用技巧》Python的filter()函数用于筛选序列元素,返回迭代器,适合函数式编程,相比列表推导式,内存更优,尤其适用于大数据集,结合lamb... 目录前言一、基本概念基本语法二、使用方式1. 使用 lambda 函数2. 使用普通函数3. 使用 N

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

Oracle迁移PostgreSQL隐式类型转换配置指南

《Oracle迁移PostgreSQL隐式类型转换配置指南》Oracle迁移PostgreSQL时因类型差异易引发错误,需通过显式/隐式类型转换、转换关系管理及冲突处理解决,并配合验证测试确保数据一致... 目录一、问题背景二、解决方案1. 显式类型转换2. 隐式转换配置三、维护操作1. 转换关系管理2.

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

深度解析Spring Boot拦截器Interceptor与过滤器Filter的区别与实战指南

《深度解析SpringBoot拦截器Interceptor与过滤器Filter的区别与实战指南》本文深度解析SpringBoot中拦截器与过滤器的区别,涵盖执行顺序、依赖关系、异常处理等核心差异,并... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局