android 转屏幕事件保存数据--就一行代码
来源:互联网 发布:暴走淘宝店 编辑:程序博客网 时间:2024/06/08 06:52
今天弄了一个下午,搞定了一件事情:让系统在转屏给我自动保存数据。
事情的缘由是:最近一个项目用到是全屏,里面的view是动态的变化的,一开始用的是台电的P73作为开发的,在锁屏解锁的时候系统是能够自动给我保存数据的,可是公司要用Samsung来做推广(是不是给我们老板是韩国的博士有关啊?)Samsung用的是GT-P1010,居然不能够自动的保存数据了。
对比两个Tab发现两个除了生产厂的不同,系统还不同国产的是2.3而韩国的是2.2 一开始就怀疑是不是2.2的不自动保存,然后就写程序来验证想法。要解决问题就我认为最好的哦方法是找到原因,为了找到原因我得有方向去寻找,我做的第一步就是让问题重现。
第一步,重现问题,写了一段代码:
public class MainActivity extends Activity { static MainActivity mainActivity;String data = "hello";byte[] buf;private SensorManager mSensorManager;private Sensor mGravity = null; /** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);mainActivity = this;setContentView(R.layout.main);mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);mGravity = mSensorManager.getDefaultSensor(Sensor.TYPE_ALL);mSensorManager.registerListener(null, mGravity, SensorManager.SENSOR_DELAY_NORMAL);data = "world";System.out.println("mGravity: " + mGravity);//buf = new byte[1024 * 1024];System.out.println("oncreate():" + System.currentTimeMillis());}public static void view2(){mainActivity.setContentView(R.layout.test_2);}@Overrideprotected void onDestroy() {System.out.println("onDestroy():" + System.currentTimeMillis());super.onDestroy();}@Overrideprotected void onStop() {System.out.println("onStop():" + System.currentTimeMillis());super.onStop();}@Overrideprotected void onPause() {System.out.println("onPause():" + System.currentTimeMillis());mSensorManager.registerListener(null, mGravity, SensorManager.SENSOR_DELAY_NORMAL);super.onPause();}@Overrideprotected void onResume() {System.out.println("onResume():" + System.currentTimeMillis());super.onResume();}@Overrideprotected void onRestart() {System.out.println("onRestart():" + System.currentTimeMillis());super.onRestart();}@Overrideprotected void onStart() {System.out.println("onStart():" + System.currentTimeMillis());super.onStart();}@Overridepublic boolean onTouchEvent(MotionEvent event) {if(event.getAction() == MotionEvent.ACTION_DOWN){setContentView(R.layout.test_2);}return true;}}然后在分别在两个他Tab上运行,居然有时候能保存,有时不能保存,经过几次的实验发现是这样的情况
在台电上只要是横屏的情况下 无论怎么锁屏解屏都是能够很好的哦保存数据的。在竖屏的情况是总是失去数据的。在Samsung上情况正好相反。
于是在就增加了代码中的7个经典的状态输出,看看到底是什么情况。输出的结果与Activity相同,在台电上横屏的是这个情况(本文只讨论解锁锁屏)
pause--resume而在竖屏的情况下确实这样的:
按下lock :pause--stop--destroy--create--start--restart--resume--pause
按下lock key:resume
解锁:pause--stop--destroy--create--start--resume
这样的话,在锁屏钱的一些操作数据都没有了,因为进行的是create还是两次!!在Samsung也有同样的情况只不过人家的是不同的方向罢了。
于是,现在就有解决方案了:
1、在onPause方法里面进行保存数据然后在onResume方法里回复数据
2、让系统给我们自动的保存恢复(根据前面说的,在台电上横屏的情况下是能够保存数据的)
我自认为我是一懒人,要是采取第一种方式来保存要写很多的代码--当然这应该是最常规的做法吧,毕竟doc要是推荐这样做的 Saving Persistent State推荐的也是这个做法,可是,我不想做这些东西,因为台电的能够给我保存,虽然只是在横屏/竖屏的情况下。
想到的第一个方案要找到屏幕旋转的原因,以及影响。
第一步禁用传感器,用
android:screenOrientation="nosensor"来设置,发现还是不行--可能是没有禁用掉传感器的缘故于是就监听事件,想到以前看android哦源码的时候有一个叫做onConfigurationChanged的方法,就试试这个方法,直接在代码中增加override 发现居然没有直接调用!!!
@Overridepublic void onConfigurationChanged(Configuration newConfig) {System.out.println("onConfigurationChanged: is called " + newConfig);newConfig.orientation = Configuration.ORIENTATION_PORTRAIT;super.onConfigurationChanged(newConfig);}
让我很郁闷!!看注释是这样的:
/** * Called by the system when the device configuration changes while your * activity is running. Note that this will <em>only</em> be called if * you have selected configurations you would like to handle with the * {@link android.R.attr#configChanges} attribute in your manifest. If * any configuration change occurs that is not selected to be reported * by that attribute, then instead of reporting it the system will stop * and restart the activity (to have it launched with the new * configuration). * * <p>At the time that this function has been called, your Resources * object will have been updated to return resource values matching the * new configuration. * * @param newConfig The new device configuration. */
然后就在 manifest文件里增加onconfiChange属性为转屏的属性值,然后就奇迹般的好了。!!
然后就能够让系统给我自动的保存数据。我的一个下午就搞了这么多东西,有效代码居然只有一行
- android 转屏幕事件保存数据--就一行代码
- android 超简单的数据保存与读取 一行代码搞定数据保存与读取 ACache
- Android 数据存储 《第一行代码》
- 【第一行代码】Android数据存储
- Android代码模拟物理、屏幕点击事件
- android button点击事件(第一行代码android学习笔记)
- android之纵横屏幕切换时保存数据
- android stdio 设置屏幕旋转后保存数据
- 每写一行代码,就有一行代码的自信。
- 第一行代码:解决活动回收时数据的保存问题
- 《第一行代码--Android》读书笔记之数据存储
- Android 事务 SQLite 数据(参考《第一行代码》)
- Android第一行代码学习笔记四----数据存储
- [Android][第一行代码][第 6 章 数据存储]
- bundle 屏幕切换保存数据
- Android第一行代码
- 一行代码搞定所有屏幕适配
- 【Android】代码实现模拟屏幕点击和键盘按键事件
- 5个哲学家抢叉子程序. 演示死锁
- asp.net 4 服务器控件ID选择
- U-boot启动第一阶段详细分析
- 关于子页面与父页面的关闭和弹出
- asp.net viewstate 解密
- android 转屏幕事件保存数据--就一行代码
- UNIX网络编程学习(14)--使用select正确处理EOF的str_cli函数修订版
- Minicom
- MySQL修改外键删除时操作出现error 121解决办法
- 我的VB第一次作业
- const在C和C++中的一个区别
- Java编程提高性能时需注意的地方
- SecureCRT 常用命令
- IIS 7.5 deploying errors and their solutions