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使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me