android开发杂记之Bundle&Context
来源:互联网 发布:如何安装办公软件 编辑:程序博客网 时间:2024/05/17 08:15
一、Bundle类:
作用:实现两个activity之间的通讯
用法:
(1)新建一个bundle类
Bundle undle = new Bundle();
(2)bundle类中加入数据(key -value的形式,另一个activity里面取数据的时候,就要用到key,找出对应的value)
bundle.putString("Data", "data from TestBundle");
(3)新建一个intent对象,并将该bundle加入这个intent对象
Intent intent = new Intent(); intent.setClass(TestBundle.this, Target.class); //压入数据 intent.putExtras(bundle); startActivity(intent);
(4)在另一个类中读出数据
String data = bundle.getString("Data");//读出数据
二、Context
概念:
直译上下文,加载资源、启动一个新的Activity、获取系统服务、获取内部文件(夹)路径、创建View操作时等都需要Context的参与。
Context是个抽象类,Activity、Service、Application都是Context的子类;Context是维持Android程序中各组件能够正常工作的一个核心功能类。
作用:
弹出Toast、启动Activity、启动Service、发送广播、操作数据库等等都需要用到Context。
TextView tv = new TextView(getContext());ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), ...);AudioManager am = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);getApplicationContext().getSharedPreferences(name, mode);getApplicationContext().getContentResolver().query(uri, ...);getContext().getResources().getDisplayMetrics().widthPixels * 5 / 8;getContext().startActivity(intent);getContext().startService(intent);getContext().sendBroadcast(intent);
阅读全文
1 0
- android开发杂记之Bundle&Context
- Android开发之Bundle
- Android开发之Context
- Android开发之context
- android开发之Bundle使用
- Android开发之杂记(2)
- Android开发之Android Context,上下文(Activity Context, Application Context)
- Android中Context,Intent,Bundle
- Android开发之Context理解
- Android开发之ADT Bundle配置NDK
- Android开发之Bundle与Intent详解
- Android开发工具之adt-bundle-windows
- Android开发--UI之Bundle的使用
- Android开发------IPC机制之Bundle实现
- Android开发之Bundle与Intent详解
- android开发之杂记(1)
- Android开发之杂记(3)
- Android开发之获取全局context技巧
- FPGA 主流芯片选型指导和命名规则(二)
- [HNOI 2012] 射箭
- 120. Triangle
- hdu--6129--Just do it
- 面试题58:二叉树的下一个结点
- android开发杂记之Bundle&Context
- python 爬虫 爬智联招聘的招聘要求
- ElasticSearch简介、常用命令及插件介绍
- 终极单例模式,饿汉式,懒汉式,枚举式
- Kafka测试及性能调优详细总结
- 欢迎使用CSDN-markdown编辑器
- 笔记--python列表及元组以及关于列表操作的函数使用
- codeforces 741A
- Eclipse An SWT error has occurred.You are recommended to exit 解决办法