Android移动应用基础学习——第四章数据存储

Android移动应用基础学习——第四章数据存储,第1张

概述第四章数据存储一、数据存储方式1、文件存储2、sharedPreference本质就是一个xml文件3、sqlite有大量相似的结构的数据需要存储的时候4、内容提供者contentProvider5、网络存储数据二、文件存储授予读写权限:Manifest–>Permissions–>add–>UserPermissi 第四章 数据存储一、数据存储方式

1、文件存储

2、shared Preference 本质就是一个xml文件

3、sqlite 有大量相似的结构的数据需要存储的时候

4、内容提供者 content ProvIDer

5、网络存储数据

二、文件存储授予读写权限:Manifest–>Permissions–>add–>User Permission–>name为androID.permission.WRITE_EXTERNAL_STORAGE

  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, "不需要记住密码");  			}  		}  	}  }
三、存储用户信息案例

OnCreate方法

存储按钮

读取按钮

四、XML序列化解析xml

代码(xml序列化:加密qq与pwd)

习题:AndroID中使用serializer对象生成xml 文档开头的方法是( start document )
五、XML解析文件访问权限

解析xml之预报–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目录下WeatherInfo.xml(一般是在服务器获取)
<?xml version="1.0" enCoding="utf-8"?><city ID='2'>    <name>郑州</name>    <weather>多云</weather>    <temp>20℃</temp>    <info>请穿个大棉袄</info> </city>
六、Shared Preferences

实例

存数据

取数据

习题:AndroID获取到SharedPreferences对象sp后,保存数据正确的逻辑是(  sp.edit().putString("name","zhangsan").commit(); )
七、QQ登录案例onCreate方法

点击事件

点赞收藏分享@L_301_11@文章举报

blowhen发布了11 篇原创文章 · 获赞 3 · 访问量 297私信 关注 总结

以上是内存溢出为你收集整理的Android移动应用基础学习——第四章数据存储全部内容,希望文章能够帮你解决Android移动应用基础学习——第四章数据存储所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1066607.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存