`
yidongkaifa
  • 浏览: 4059118 次
文章分类
社区版块
存档分类
最新评论

Activity详解 Intent显式跳转和隐式跳转, 及多个Activity之间传值 总结

 
阅读更多

Activity 生命周期

显式 Intent 调用

复制代码
 1     //创建一个显式的 Intent 对象(方法一:在构造函数中指定)
 2      Intent intent = new Intent(Intent_Demo1.this, Intent_Demo1_Result1.class);
 3      
 4      Bundle bundle = new Bundle();
 5      bundle.putString("id", strID);
 6      intent.putExtras(bundle);
 7      
 8      intent.putExtra("name", "bbb");
 9      intent.putExtra("userInfo", new UserInfo(1, "name"));
10      startActivity(intent);
11      
12      //创建一个显式的 Intent 对象(方法二:用 setClass 方法)
13      Intent intent = new Intent();
14      Bundle bundle = new Bundle();
15      bundle.putString("id", strID);
16      intent.setClass(Intent_Demo1.this, Intent_Demo1_Result1.class);
17      intent.putExtras(bundle);
18      startActivity(intent);
19      
20      //创建一个显式的 Intent 对象(方法三:用 setClass 方法)
21      Intent intent = new Intent();
22      Bundle bundle = new Bundle();
23      bundle.putString("id", strID);
24      intent.setClassName(Intent_Demo1.this, "com.great.activity_intent.Intent_Demo1_Result1");
25      intent.putExtras(bundle);
26      startActivity(intent);
27      
28      //创建一个显式的 Intent 对象(方法四:用 setComponent 方法)
29      Intent intent = new Intent();
30      Bundle bundle = new Bundle();
31      bundle.putString("id", strID);
32      //setComponent方法的参数:ComponentName
33      intent.setComponent(new ComponentName(Intent_Demo1.this, Intent_Demo1_Result1.class));
34      intent.putExtras(bundle);
35      startActivity(intent);
复制代码

Intent隐式跳转 Action

复制代码
 1     //创建一个隐式的 Intent 对象:Action 动作
 2     /**
 3      * 这里指定的是 AndroidManifest.xml 文件中配置的
 4      * <intent-filter>标签中的<action android:name="com.great.activity_intent.Intent_Demo1_Result3" />
 5      * 所在的 Activity,注意这里都要设置 <category android:name="android.intent.category.DEFAULT" />
 6      */
 7     Intent intent = new Intent();
 8     //设置 Intent 的动作
 9     intent.setAction("com.great.activity_intent.Intent_Demo1_Result3");
10     Bundle bundle = new Bundle();
11     bundle.putString("id", strID);
12     intent.putExtras(bundle);
13     startActivity(intent);
复制代码

AndroidManifest.xml

复制代码
1    <activity android:name="Intent_Demo1_Result3"
2                   android:label="Intent_Demo1_Result3">
3             <intent-filter>
4                 <action android:name="com.great.activity_intent.Intent_Demo1_Result3" />
5                 <category android:name="android.intent.category.DEFAULT" />
6             </intent-filter>
7         </activity>
复制代码

Category 类别

复制代码
 1 //创建一个隐式的 Intent 对象:Category 类别
 2 Intent intent = new Intent();
 3 intent.setAction("com.great.activity_intent.Intent_Demo1_Result33");
 4 /**
 5  * 不指定 Category 或 只指定 AndroidManifest.xml 文件中 <intent-filter> 标签中配置的任意一个 Category
 6  * <category android:name="android.intent.category.DEFAULT" /> 除外,就可以访问该 Activity,
 7  */
 8 intent.addCategory(Intent.CATEGORY_INFO);
 9 intent.addCategory(Intent.CATEGORY_DEFAULT);
10 Bundle bundle = new Bundle();
11 bundle.putString("id", strID);
12 intent.putExtras(bundle);
13 startActivity(intent);
复制代码

AndroidManifest.xml

复制代码
  <activity android:name="Intent_Demo1_Result2"
                  android:label="Intent_Demo1_Result2">
            <intent-filter>
                
                <category android:name="android.intent.category.INFO" />
                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />
                
            </intent-filter>
        </activity>
复制代码

Date 数据 跳转

复制代码
 1 //创建一个隐式的 Intent 对象,方法四:Date 数据
 2 Intent intent = new Intent();
 3 Uri uri = Uri.parse("http://www.great.org:8080/folder/subfolder/etc/abc.pdf");
 4 
 5 //注:setData、setDataAndType、setType 这三种方法只能单独使用,不可共用                
 6 //要么单独以 setData 方法设置 URI
 7 //intent.setData(uri);
 8 //要么单独以 setDataAndType 方法设置 URI 及 mime type
 9 intent.setDataAndType(uri, "text/plain");
10 //要么单独以 setDataAndType 方法设置 Type
11 //intent.setType("text/plain");
12 
13 /**
14  * 不指定 Category 或 只指定 AndroidManifest.xml 文件中 <intent-filter> 标签中配置的任意一个 Category
15  * <category android:name="android.intent.category.DEFAULT" /> 除外,就可以访问该 Activity
16  */
17 Bundle bundle = new Bundle();
18 bundle.putString("id", strID);
19 intent.putExtras(bundle);
20 startActivity(intent);
复制代码

AndroidManifest.xml

复制代码
 1         <activity android:name="Intent_Demo1_Result2"
 2                   android:label="Intent_Demo1_Result2">
 3             <intent-filter>
 4                 <category android:name="android.intent.category.DEFAULT" />
 5                 <data 
 6                     android:scheme="http"
 7                     android:host="www.great.org"
 8                     android:port="8080"
 9                     android:pathPattern=".*pdf"
10                     android:mimeType="text/plain"/>
11             </intent-filter>
12         </activity>
13         
复制代码

调用系统的的组件

复制代码
    //web浏览器
    Uri uri= Uri.parse("http://www.baidu.com:8080/image/a.jpg");
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
    
    //地图(要在 Android 手机上才能测试)
    Uri uri = Uri.parse("geo:38.899533,-77.036476");
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
    
    //路径规划
    Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");
    Intent it = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(it);
    
    //拨打电话-调用拨号程序
    Uri uri = Uri.parse("tel:15980665805");
    Intent intent = new Intent(Intent.ACTION_DIAL, uri);
    startActivity(intent);
    
    //拨打电话-直接拨打电话
    //要使用这个必须在配置文件中加入<uses-permission android:name="android.permission.CALL_PHONE"/>
    Uri uri = Uri.parse("tel:15980665805");
    Intent intent = new Intent(Intent.ACTION_CALL, uri);
    startActivity(intent);

    //调用发送短信程序(方法一)
    Uri uri = Uri.parse("smsto:15980665805");
    Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
    intent.putExtra("sms_body", "The SMS text");
    startActivity(intent);
    
    //调用发送短信程序(方法二)
    Intent intent = new Intent(Intent.ACTION_VIEW);     
    intent.putExtra("sms_body", "The SMS text");     
    intent.setType("vnd.android-dir/mms-sms");     
    startActivity(intent);
    
    //发送彩信
    Uri uri = Uri.parse("content://media/external/images/media/23");
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra("sms_body", "some text");
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    intent.setType("image/png");
    startActivity(intent);
    
    //发送Email(方法一)(要在 Android 手机上才能测试)
    Uri uri = Uri.parse("mailto:zhangsan@gmail.com");
    Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
    startActivity(intent);
    
    //发送Email(方法二)(要在 Android 手机上才能测试)
    Intent intent = new Intent(Intent.ACTION_SENDTO);  
    intent.setData(Uri.parse("mailto:zhangsan@gmail.com"));  
    intent.putExtra(Intent.EXTRA_SUBJECT, "这是标题");  
    intent.putExtra(Intent.EXTRA_TEXT, "这是内容");  
    startActivity(intent); 
    
    //发送Email(方法三)(要在 Android 手机上才能测试)
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");
    intent.putExtra(Intent.EXTRA_SUBJECT, "这是标题");  
    intent.putExtra(Intent.EXTRA_TEXT, "这是内容");
    intent.setType("text/plain");
    //选择一个邮件客户端
    startActivity(Intent.createChooser(intent, "Choose Email Client"));  
    
    //发送Email(方法四)(要在 Android 手机上才能测试)
    Intent intent = new Intent(Intent.ACTION_SEND);
    //收件人
    String[] tos = {"to1@abc.com", "to2@abc.com"};
    //抄送人
    String[] ccs = {"cc1@abc.com", "cc2@abc.com"};
    //密送人
    String[] bcc = {"bcc1@abc.com", "bcc2@abc.com"};
    intent.putExtra(Intent.EXTRA_EMAIL, tos);    
    intent.putExtra(Intent.EXTRA_CC, ccs);
    intent.putExtra(Intent.EXTRA_BCC, bcc);
    intent.putExtra(Intent.EXTRA_SUBJECT, "这是标题"); 
    intent.putExtra(Intent.EXTRA_TEXT, "这是内容");    
    intent.setType("message/rfc822");    
    startActivity(Intent.createChooser(intent, "Choose Email Client"));
    
    //发送Email且发送附件(要在 Android 手机上才能测试)
    Intent intent = new Intent(Intent.ACTION_SEND);    
    intent.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");    
    intent.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mp3/醉红颜.mp3");    
    intent.setType("audio/mp3");    
    startActivity(Intent.createChooser(intent, "Choose Email Client"));
    
    //播放媒体文件(android 对中文名的文件支持不好)
    Intent intent = new Intent(Intent.ACTION_VIEW);
    //Uri uri = Uri.parse("file:///sdcard/zhy.mp3");
    Uri uri = Uri.parse("file:///sdcard/a.mp3"); 
    intent.setDataAndType(uri, "audio/mp3"); 
    startActivity(intent);
    
    Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");     
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);     
    startActivity(intent); 
    
    //音乐选择器
    //它使用了Intent.ACTION_GET_CONTENT 和 MIME 类型来查找支持 audio/* 的所有 Data Picker,允许用户选择其中之一
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    intent.setType("audio/*");
    //Intent.createChooser:应用选择器,这个方法创建一个 ACTION_CHOOSER Intent
    startActivity(Intent.createChooser(intent, "选择音乐"));
    
    Intent intent1 = new Intent(Intent.ACTION_GET_CONTENT); 
    intent1.setType("audio/*");
    
    Intent intent2 = new Intent(Intent.ACTION_CHOOSER);
    intent2.putExtra(Intent.EXTRA_INTENT, intent1);
    intent2.putExtra(Intent.EXTRA_TITLE, "aaaa");
    startActivity(intent2);
    
    
//                //设置壁纸
//                Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER);  
//                startActivity(Intent.createChooser(intent, "设置壁纸"));  
    
    //卸载APK
    //fromParts方法
    //参数1:URI 的 scheme
    //参数2:包路径
    //参数3:
    Uri uri = Uri.fromParts("package", "com.great.activity_intent", null);    
    Intent intent = new Intent(Intent.ACTION_DELETE, uri);     
    startActivity(intent);
    
    //安装APK(???)
    Uri uri = Uri.fromParts("package", "com.great.activity_intent", null);  
    Intent intent = new Intent(Intent.ACTION_PACKAGE_ADDED, uri);
    startActivity(intent);
    
    //调用搜索
    Intent intent = new Intent();  
    intent.setAction(Intent.ACTION_WEB_SEARCH);  
    intent.putExtra(SearchManager.QUERY, "android");  
    startActivity(intent);


多个Activity之间传值

可以通过Bundle对象存储需要传递的数据,例如:
  在IntentDemoActivity里面传值,
  Intent explicitIntent=new Intent(IntentDemoActivity.this, ExplicitActivity.class);//这是在Intent的构造函数中指定
EditText nameText=(EditText)findViewById(R.id.username);
// 通过Bundle对象存储需要传递的数据 
Bundle bundle=new Bundle();
 bundle.putString("userName", nameText.getText().toString());
//把Bundle对象bundle给explicitIntent
explicitIntent.putExtras(bundle);
startActivity(explicitIntent);

  在ExplicitActivity获取值:
  //获取Intent中的Bundle对象
 Bundle bundle = this.getIntent().getExtras();
//获取Bundle中的数据,注意类型和key
String userName=bundle.getString("userName");

两个个Activity之间切换

在ExplicitActivity页面上加一个返回按钮,并在事件写如下代码:

/*给上一个Activity返回结果*/

Intent intent=new Intent(ExplicitActivity.this, IntentDemoActivity.class);//这是在Intent的构造函数中指定 ExplicitActivity.this.setResult(RESULT_OK,intent); /*结束本Activity*/ ExplicitActivity.this.finish();

这样就返回到IntentDemoActivity这个Activity去了。

分享到:
评论

相关推荐

    node-v16.12.0-darwin-x64.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    试用Dev Containers的示例项目-Go

    计算机技术是指评价计算机系统的各种知识和技能的总称。它涵盖了计算机硬件、软件、网络和信息安全等方面。计算机技术的发展使我们能够进行高效的数据处理、信息存储和传输。现代计算机技术包括操作系统、数据库管理、编程语言、算法设计等。同时,人工智能、云计算和大数据等新兴技术也在不断推动计算机技术的进步。计算机技术的应用广泛,涵盖了各个领域,如商业、医疗、教育和娱乐等。随着计算机技术的不断革新,我们可以更加高效地实现预期自动化、标准化

    NTsky新闻发布v1.0测试版(提供JavaBean).zip

    ### 内容概要: 《NTsky新闻发布v1.0测试版》是一款基于 Java 开发的新闻发布系统的测试版本,旨在为新闻机构和媒体提供一个简单易用的新闻发布平台。该系统具有基本的新闻发布和管理功能,包括新闻分类、新闻编辑、新闻发布等核心功能。此外,该版本还提供了 JavaBean,使开发人员能够方便地将系统集成到自己的项目中,快速实现新闻发布的功能。 ### 适用人群: 本测试版本适用于新闻机构、媒体从业者以及Java开发人员。如果你是一家新闻机构或媒体,希望拥有一个简单易用的新闻发布平台,方便快捷地发布和管理新闻,那么这个测试版本将为你提供一个初步的体验。同时,如果你是一名Java开发人员,希望学习和掌握新闻发布系统的开发技术,并且对新闻行业有一定的了解,那么通过这个测试版本,你可以获取到一些JavaBean,并且可以参考系统的设计和实现,为你的项目开发提供参考和借鉴。无论是从业务需求还是技术学习的角度,该测试版本都将为你提供一定的帮助和支持。

    JavaScript介绍.zip

    javascript,JavaScript 最初由 Netscape 公司的 Brendan Eich 在 1995 年开发,用于 Netscape Navigator 浏览器。随着时间的推移,JavaScript 成为了网页开发中不可或缺的一部分,并且其应用范围已经远远超出了浏览器,成为了全栈开发的重要工具。

    15-21.php

    15-21.php

    汽车租赁系统(毕业设计)

    汽车租赁系统后端采用了spring,spring mvc,mybatis框架,前端使用了layui,界面美观。 包含功能:客户管理,车辆管理,出租,出租单管理,汽车入库,检查单管理,菜单管理,用户管理,角色管理,日志管理,统计分析等。 该毕业设计功能涵盖了大部分汽车租赁中的业务需求,特点是业务功能较多,有助于学生加深业务到技术的理解。

    设计模式_行为型_访问者模式.md

    设计模式_行为型_访问者模式

    HTML25-创意网站产品主页模板官网落地页APP主页产品宣传页源码 landing静态页面.zip

    HTML25-创意网站产品主页模板官网落地页APP主页产品宣传页源码 landing静态页面

    快手弹幕采集学习源码!!

    快手弹幕采集学习源码

    general-exporter windows

    自定义监控项 Windows 二进制文件

    数据可视化大屏展示系统HTML模板源码 大数据大屏展示源码 VUE.zip

    数据可视化大屏展示系统HTML模板源码 大数据大屏展示源码 VUE

    node-v18.2.0-linux-armv7l.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    这个项目是用于个人参加浙江大学移动创新竞赛而使用。.zip

    这个项目是用于个人参加浙江大学移动创新竞赛而使用。

    2023年全国职业院校技能大赛“区块链技术应用赛项”国赛正式赛题

    2023年全国职业院校技能大赛“区块链技术应用赛项”国赛正式赛题 高职 全国职业院校技能大赛 正式赛题

    基于stm32的智能家居系统

    基于stm32的智能家居系统 基于stm32的智能家居系统

    21九章空间解析几何.pdf

    21九章空间解析几何.pdf

    吴恩达机器学习python版本代码(完结).zip

    吴恩达机器学习python版本代码(完结)

    HTML21-印刷模板官网落地页APP主页产品宣传页源码 landing静态页面.zip

    HTML21-印刷模板官网落地页APP主页产品宣传页源码 landing静态页面

    信息办公简易java开源订销管理系统-javainfo.zip

    ### 内容概要: 《[信息办公]简易Java开源订销管理系统》是一款基于 Java 开发的开源项目,旨在为企业和机构提供一套简单易用的订购和销售管理解决方案。该系统具有简洁的界面设计和丰富的功能模块,包括客户管理、产品管理、订单管理、库存管理等核心功能,同时支持生成报表和统计分析,帮助用户快速了解企业的订购和销售情况,提高管理效率和业务决策能力。 ### 适用人群: 本项目适用于企业信息办公人员和Java开发人员。如果你是一位信息办公人员,希望通过一个简单易用的系统来管理企业的订购和销售业务,该系统将为你提供一个方便快捷的解决方案。同时,如果你是一名Java开发人员,希望学习和掌握Java开发技术,并且对企业信息管理系统感兴趣,那么这个开源项目将为你提供一个学习和实践的机会。无论是从业务管理还是技术学习的角度,该项目都将为你提供实用的功能和丰富的学习资源。

    [精品] todo_appAdobeXD源码下载设计素材UI设计.xd

    [精品] todo_appAdobeXD源码下载设计素材UI设计

Global site tag (gtag.js) - Google Analytics