1.5.42 Android Intent 关于 隐式意图 显示意图 intent 跳转问题

2024-08-21 20:32

本文主要是介绍1.5.42 Android Intent 关于 隐式意图 显示意图 intent 跳转问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

一、android中的显示意图和隐式意图

显示意图要求必须知道被激活组件的包和class

隐式意图只需要知道跳转activity的动作和数据,就可以激活对应的组件

若A 为主activity  B 接收跳转的activity 跳转步骤:

1.在主配置文件中声明B 至少要声明一个android:name 属性

 

<activity  android:name=".DemoActivity"  android:label="@string/demoActivity" >  <intent-filter>  <!-- 隐士意图中指定intent的名字 自己定义 可匹配多项 -->  <action android:name="com.itcast.intent.DemoActivity" />  <!-- 隐式intent需要指定的activity的类型,可自己定义该值,需要在调用的时候相对应不写该项默认为 android.intent.category.DEFAULT,可匹配多项 -->  <category android:name="android.intent.category.DEFAULT" />  <!-- 指定传想该activity数值的类型 和主机,如果指定该项,就必须在跳转activity的时候传入还数据和主机名 -->  <data  android:host="cn.itcast.demo"  android:scheme="itcast" />  </intent-filter>  </activity>

 

 

 

 2.在A的布局文件中一个textview和button,并添加点击事件

 

<TextView  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="第一个activity" />  <Button  android:id="@+id/button"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:onClick="open"  android:text="跳转" />

 

 

3.点击事件触发的集中intent意图

 

/** * 意图,描述一个动作,激活一个组件,使用其他的activity需要现在主文件中配置activity的名字等属性 * intent 要去做一件事 * @param view */  public void open(View view) {  /** * 方法一 */  Intent intent1 = new Intent();  // 1.是当前的包名,2跳转activity的类名,一定要加上包名  intent1.setClassName("com.itcast.intent", "com.itcast.intent.DemoActivity");  // startActivity(intent1);  /** * 方法二 */  Intent intent2 = new Intent(this, DemoActivity.class);  // startActivity(intent2);  /** * 方法三 */  Intent intent3 = new Intent();  ComponentName component = new ComponentName("com.itcast.intent", "com.itcast.intent.DemoActivity");  intent3.setComponent(component);  // startActivity(intent3);  /** * 上面三种方法要求必须知道被激活组件的包和class,称为显示意图 */  // ******************************************************************//  /** * 隐式意图只需要知道跳转activity的动作和数据,就可以激活对应的组件<br> * 如果要激活另外程序的组件 */  Intent intent = new Intent();  intent.setAction("com.itcast.intent.DemoActivity");  // 不管在主配置文件中有沒有声明跳转activity的category,都要写该项,不然报错找到activity,不些有系統會以默認的類型  intent.addCategory("android.intent.category.DEFAULT");  // 如果在声明activity的时候指定了data属性,在跳转的时候就一定要设置他的data属性值,和配置的属性值相等,不然也会报找不到的错误  intent.setData(Uri.parse("itcast://cn.itcast.demo"));  // startActivity(intent);  Intent imageIntent = new Intent();  imageIntent.setAction(Intent.ACTION_PICK);  imageIntent.setType("image/*");// 设置数据类型  startActivity(imageIntent);  }

 

 

以上转载:http://blog.csdn.net/hello2me/article/details/8059220一、不需要返回值的跳转

 

二、android intent 跳转的返回值问题

1.不需要返回值的跳转

Intent intent=new Intent();
intent.setClass(目前的acitivy.this, 目标activity.class);
startActivity(intent);
2.需要返回值的跳转
Intent intent=new Intent();
intent.setClass(目前的acitivy.this, 目标activity.class);
startActivity(intent);
startActivityForResult(intent, 状态值(int类型));
状态值作为接受目标返回值的验证。
requestCode 接收返回的状态值
resultCode :RESULT_OK RESULT_CANCELED 可以自己赋值,按返回键时,系统默认赋值为:RESULT_CANCELED
目标activity 关闭才能返回请求activity
例如:
(1)请求页面

 

public class MainActivy extends Activity {  @Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Intent intent=new Intent();intent.setClass(MainActivy.this, ReceiveMSG.class);startActivityForResult(intent, 0);      }protected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubsuper.onActivityResult(requestCode, resultCode, data);if (requestCode==0) {if (resultCode==RESULT_OK) {Toast.makeText(this, "Send SMS RESULT_OK", Toast.LENGTH_SHORT).show();}else if (resultCode==RESULT_CANCELED) {Toast.makeText(this, "result cancel", Toast.LENGTH_SHORT).show();}else {Toast.makeText(this, resultCode, Toast.LENGTH_SHORT).show();}}else {Toast.makeText(this, "requsetcode not 0", Toast.LENGTH_SHORT).show();}}
}

 

(2)接收返回值界面

 

public class ReceiveMSG extends Activity{protected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);        ReceiveMSG.this.setResult(RESULT_OK);this.finish();       }
}

 


以上转载:http://blog.sina.com.cn/s/blog_7309444701014u2d.html
 

 

这篇关于1.5.42 Android Intent 关于 隐式意图 显示意图 intent 跳转问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

解决IDEA报错:编码GBK的不可映射字符问题

《解决IDEA报错:编码GBK的不可映射字符问题》:本文主要介绍解决IDEA报错:编码GBK的不可映射字符问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录IDEA报错:编码GBK的不可映射字符终端软件问题描述原因分析解决方案方法1:将命令改为方法2:右下jav

MyBatis模糊查询报错:ParserException: not supported.pos 问题解决

《MyBatis模糊查询报错:ParserException:notsupported.pos问题解决》本文主要介绍了MyBatis模糊查询报错:ParserException:notsuppo... 目录问题描述问题根源错误SQL解析逻辑深层原因分析三种解决方案方案一:使用CONCAT函数(推荐)方案二:

Redis 热 key 和大 key 问题小结

《Redis热key和大key问题小结》:本文主要介绍Redis热key和大key问题小结,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、什么是 Redis 热 key?热 key(Hot Key)定义: 热 key 常见表现:热 key 的风险:二、

IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决

《IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决》:本文主要介绍IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决,本文分步骤结合实例给大... 目录步骤 1:创建 Maven Web 项目步骤 2:添加 Spring MVC 依赖1、保存后执行2、将新的依赖

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

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

Spring 中的循环引用问题解决方法

《Spring中的循环引用问题解决方法》:本文主要介绍Spring中的循环引用问题解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录什么是循环引用?循环依赖三级缓存解决循环依赖二级缓存三级缓存本章来聊聊Spring 中的循环引用问题该如何解决。这里聊

Spring Boot中JSON数值溢出问题从报错到优雅解决办法

《SpringBoot中JSON数值溢出问题从报错到优雅解决办法》:本文主要介绍SpringBoot中JSON数值溢出问题从报错到优雅的解决办法,通过修改字段类型为Long、添加全局异常处理和... 目录一、问题背景:为什么我的接口突然报错了?二、为什么会发生这个错误?1. Java 数据类型的“容量”限制

关于MongoDB图片URL存储异常问题以及解决

《关于MongoDB图片URL存储异常问题以及解决》:本文主要介绍关于MongoDB图片URL存储异常问题以及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录MongoDB图片URL存储异常问题项目场景问题描述原因分析解决方案预防措施js总结MongoDB图

SpringBoot项目中报错The field screenShot exceeds its maximum permitted size of 1048576 bytes.的问题及解决

《SpringBoot项目中报错ThefieldscreenShotexceedsitsmaximumpermittedsizeof1048576bytes.的问题及解决》这篇文章... 目录项目场景问题描述原因分析解决方案总结项目场景javascript提示:项目相关背景:项目场景:基于Spring

解决Maven项目idea找不到本地仓库jar包问题以及使用mvn install:install-file

《解决Maven项目idea找不到本地仓库jar包问题以及使用mvninstall:install-file》:本文主要介绍解决Maven项目idea找不到本地仓库jar包问题以及使用mvnin... 目录Maven项目idea找不到本地仓库jar包以及使用mvn install:install-file基