频道栏目
IT货架 > > 正文
android之纵横屏幕切换时保存数据
网友分享于:Jan 1, 1970 8:00:00 AM    来源: IT货架   

标签:android   package   public   java   import   

java代码:

package com.sxt.day04_05;


import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


import com.sxt.day04_05.R;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;


public class MainActivity extends Activity {
String mUsers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("main","onCreate()");
if(savedInstanceState==null){// 若是第一次创建MainActivity
//从服务端下载users.json文件
downloadUsers();
}else{
mUsers=savedInstanceState.getString("users");
Log.i("main",mUsers);
}
}


//从服务端下载users.json文件
private void downloadUsers() {
new Thread(){//sdk从4.0开始要求下载数据必须在工作线程中执行
public void run() {
InputStream in =null;
try {
URL url=new URL("http://10.0.2.2/users.json");
URLConnection conn = url.openConnection();
in = conn.getInputStream();
byte[] buffer=new byte[1024];
int len = in.read(buffer);
mUsers=new String(buffer, 0, len);
Log.i("main",mUsers);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(in!=null){
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
}.start();
}


@Override
protected void onStart() {
super.onStart();
Log.i("main","onStart()");
}

@Override
protected void onResume() {
super.onResume();
Log.i("main","onResume()");
}

@Override
protected void onPause() {
super.onPause();
Log.i("main", "onPause()");
}

@Override
protected void onStop() {
super.onStop();
Log.i("main","onStop()");
}

@Override
protected void onDestroy() {
super.onDestroy();
Log.i("main","onDestroy()");
}

@Override
protected void onRestart() {
super.onRestart();
Log.i("main","onRestatr()");
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i("main","onSaveInstanceState()");
outState.putString("users", mUsers);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.i("main","onRestoreInstanceState()");
}
}

本文出自 “追随心的彼岸” 博客,谢绝转载!

android之纵横屏幕切换时保存数据

标签:android   package   public   java   import   

广告服务联系QQ:1134687142 | 网站地图

版权所有: IT货架- 内容来自互联网,仅供用于技术学习,请遵循相关法律法规. 京ICP备11030978号-1