Android mobile applications based learning - Chapter IV data storage

Chapter IV data storage

A data storage

1, file storage

2, shared Preference is essentially a xml file

3, sqlite when large amounts of data have a similar structure needs to be stored

4, the content provider content Provider

5, the data stored in the network

Second, file storage

  • Grant read and write permissions: Manifest-> Permissions-> add-> User Permission-> Name is android.permission.WRITE_EXTERNAL_STORAGE
    Here Insert Picture Description
  public class MainActivity extends Activity {
  	private static final String tag = "MainActivity";
  	private EditText et_qq;
  	private EditText et_pwd;
  	private CheckBox cb_remember;
  	@Override
  	protected void onCreate(Bundle savedInstanceState) {
  		super.onCreate(savedInstanceState);
  		setContentView(R.layout.activity_main);
  		et_qq = (EditText) findViewById(R.id.et_qq);
  		et_pwd = (EditText) findViewById(R.id.et_pwd);
  		cb_remember = (CheckBox) findViewById(R.id.cb_remember);
  		// 读取用户保存密码的文件
  		try {
  			File file = new File(Environment.getExternalStorageDirectory(),
  					"info.txt");
  			FileInputStream fis = new FileInputStream(file);
  			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
  			String info = br.readLine();
  			String qq = info.split("##")[0];
  			String pwd = info.split("##")[1];
  			et_qq.setText(qq);
  			et_pwd.setText(pwd);
  		} catch (Exception e) {
  			// TODO Auto-generated catch block
  			e.printStackTrace();
  		}
  	}
  	public void login(View view) {
  		String qq = et_qq.getText().toString();
  		String pwd = et_pwd.getText().toString();
  		if (TextUtils.isEmpty(qq) || TextUtils.isEmpty(qq)) {
  			Toast.makeText(this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
  		} else {
  			// 登录操作
  			// 判断用户是否勾选了记住密码
  			if (cb_remember.isChecked()) {
  				// 记住密码
  				Log.i(tag, "记住密码");
  				// 保存用户qq和密码
  				try {
  					//判读sd卡状态是否有,是否可以被读写
  					String status = Environment.getExternalStorageState();
  					Environment.getExternalStorageDirectory().getFreeSpace();
  					if (Environment.MEDIA_MOUNTED.equals(status)) {
  						File file = new File(
  								Environment.getExternalStorageDirectory(),
  								"info.txt");
  						FileOutputStream fos = new FileOutputStream(file);
  						// 用##作分隔符
  						String info = qq + "##" + pwd;
  						fos.write(info.getBytes());
  						fos.close();
  						Toast.makeText(this, "保存密码成功", 0).show();
  					} else {
  						Toast.makeText(this, "sd卡不可写,请检查sd卡状态", 0).show();
  					}
  				} catch (Exception e) {
  					e.printStackTrace();
  					Toast.makeText(this, "保存密码失败", 0).show();
  				}
  			} else {
  				// 不需要记住密码
  				Log.i(tag, "不需要记住密码");
  			}
  		}
  	}
  }

Third, store user information Case

Here Insert Picture Description

  • OnCreate method

Here Insert Picture Description

  • Save button

Here Insert Picture Description

  • Read button

Here Insert Picture Description

Four, XML serialization

  • Xml parsing

Here Insert Picture Description

  • Code (xml serialization: encryption and qq pwd)

Here Insert Picture Description

习题:

Android中使用serializer对象生成xml 文档开头的方法是( start Document )

Five, XML parsing

  • File Access

Here Insert Picture Description

  • Forecast xml parsing of -MainActivity
  public class MainActivity extends Activity {
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		TextView tv_info=(TextView)findViewById(R.id.tv_info);
    		ImageView iv_info=(ImageView)findViewById(R.id.iv_info);
    		// 上下文,实际上就是一个环境
    		AssetManager am = this.getAssets();
    		try {
    			WeatherInfo weatherInfo=new WeatherInfo();
    			// 得到文件的输入流
    			InputStream is = am.open("weather.xml");
    			// 解析xml文件(sax dom dom4j pull解析)
    			// 声明一个pull解析器
    			XmlPullParser parser = Xml.newPullParser();
    			// 初始化解析器,设置解析哪个流,用什么样的编码
    			parser.setInput(is, "utf-8");
    			// 获取事件的类型
    			int type = parser.getEventType();
    			while(type!=XmlPullParser.END_DOCUMENT){
    				if(type==XmlPullParser.START_TAG){
    					if("name".equals(parser.getName())){
    						String name=parser.nextText();
    						weatherInfo.setName(name);
    					}else if("weather".equals(parser.getName())){
    						String weather=parser.nextText();
    						weatherInfo.setWeather(weather);
    					}else if("temp".equals(parser.getName())){
    						String temp=parser.nextText();
    						weatherInfo.setTemp(temp);
    					}else if("info".equals(parser.getName())){
    						String info=parser.nextText();
    						weatherInfo.setInfo(info);
    					}
    				}
    				type=parser.next();
    			}
    //			Toast.makeText(this, weatherInfo.toString(), 1).show();
    	        tv_info.setText(weatherInfo.toString());
    	        //根据天气类型,设置图片weatherInfo.getweather()
    	        iv_info.setImageResource(R.drawable.ic_launcher);       
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
  • assets directory WeatherInfo.xml (usually available at the server)
<?xml version="1.0" encoding="utf-8"?>
<city id='2'>
    <name>郑州</name>
    <weather>多云</weather>
    <temp>20</temp>
    <info>请穿个大棉袄</info> 
</city>

六、Shared Preferences

Here Insert Picture Description

  • Examples

Here Insert Picture Description

  • Stored data

Here Insert Picture Description

  • Take data

Here Insert Picture Description

习题:

Android获取到SharedPreferences对象sp后,保存数据正确的逻辑是(  sp.edit().putString("name","zhangsan").commit(); )

Seven, QQ login Case

  • onCreate method

Here Insert Picture Description

  • Click event

Here Insert Picture Description

Published 11 original articles · won praise 3 · Views 291

Guess you like

Origin blog.csdn.net/weixin_44367041/article/details/104521497