andriod 多个Activity之间共享数据
来源:互联网 发布:淘宝访客内网ip 编辑:程序博客网 时间:2024/05/19 13:09
在项目中要在多个Activity之间共享数据,刚开始想了多种方法,但是都失败了,通过查找资料,Android提供了一个叫Application的共享数据很合适,下面就贴出代码。
写一个继承Application的数据模型
package cn.edu.cqu.bluetooth.dao;import android.app.Application;public class Bluetooth extends Application{String mDeviceAddress;String mDeviceName;String phoneMac;public String getPhoneMac() {return phoneMac;}public void setPhoneMac(String phoneMac) {this.phoneMac = phoneMac;}public String getmDeviceAddress() {return mDeviceAddress;}public void setmDeviceAddress(String mDeviceAddress) {this.mDeviceAddress = mDeviceAddress;}public String getmDeviceName() {return mDeviceName;}public void setmDeviceName(String mDeviceName) {this.mDeviceName = mDeviceName;}}跟一般的数据模型很像,只不过这个是继承了Application,然后要在AndroidManifest.xml中注册。
<application android:name="cn.edu.cqu.bluetooth.dao.Bluetooth" android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:theme="@android:style/Theme.Holo.Light">这个一定不能少。
下面就是写入数据。
Bluetooth bluetooth = (Bluetooth) getApplicationContext(); bluetooth.setPhoneMac(phoneMac); bluetooth.setmDeviceAddress(device.getAddress()); bluetooth.setmDeviceName(device.getName());
然后获取数据
Bluetooth bluetooth = (Bluetooth) getApplicationContext();System.out.println("bluetooth---" + bluetooth);mDeviceName = bluetooth.getmDeviceName();mDeviceAddress = bluetooth.getmDeviceAddress();phoneMac = bluetooth.getPhoneMac();这样共享数据就OK了!
0 0
- andriod 多个Activity之间共享数据
- 多个activity之间的数据共享
- 多个Activity之间共享数据
- 多个Activity之间共享数据的5种方式
- 在多个Activity之间共享变量
- 多Activity之间的数据共享
- Android 多个Activity之间共享类(数据)——Application Context
- Android多个Activity之间共享类(数据)——Application Context
- 多个Activity之间共享数据的5种方式以及 Application Context
- Android多个Activity之间共享类(数据)——Application Context
- 多个Activity之间共享数据的5种方式以及 Application Context
- 多个Activity之间共享数据的5种方式以及 Application Context
- Android在多个Activity共享数据
- 多个线程之间共享数据
- SharedPreferences多个程序之间共享数据
- 7-多个线程之间共享数据
- SharedPreferences多个程序之间共享数据
- 多个线程之间共享数据
- 一键生成各种尺寸Icon的php脚本
- Android单元测试2-工具
- 黑马程序员--ios protocol语法简介
- Android之JUnit
- Java面试问题和答案(一)
- andriod 多个Activity之间共享数据
- 一步一步教你用PHP+MySql搭建网站 No.2 图片显示和Page页面
- linux c 实现冒泡排序
- JAVA中的单例模式(采用单例模式定义的类)
- 2.1.1链栈的设计与实现(不推荐)
- 在VMWare上安装ubuntu及VMWare Tools
- 读书笔记------程序员的修炼之道(33技巧)
- 三菱plc编程软件gx+developer安装教程
- Java面试问题和答案(二)