AndroidWkk保存数据到手机内存及Sd卡资料.docx
- 文档编号:17690695
- 上传时间:2023-08-03
- 格式:DOCX
- 页数:12
- 大小:16.98KB
AndroidWkk保存数据到手机内存及Sd卡资料.docx
《AndroidWkk保存数据到手机内存及Sd卡资料.docx》由会员分享,可在线阅读,更多相关《AndroidWkk保存数据到手机内存及Sd卡资料.docx(12页珍藏版)》请在冰点文库上搜索。
AndroidWkk保存数据到手机内存及Sd卡资料
Android_Wkk_保存数据到手机内存及Sd卡
//手机内存
public classAa extendsActivity implementsOnClickListener{
CheckBoxcheckbox1;
CheckBoxcheckbox2;
Buttonbutton2;
EditTextedittext1;
EditTextedittext2;
protected void onCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.aa);
checkbox1 =(CheckBox)findViewById(R.id.checkBox1);
checkbox2 =(CheckBox)findViewById(R.id.checkBox2);
button2 =(Button)findViewById(R.id.button2);
button2.setOnClickListener(this);
edittext2=(EditText)findViewById(R.id.editText2);
edittext1=(EditText)findViewById(R.id.editText1);
Map
if(m!
=null){
edittext1.setText(m.get("a"));
edittext2.setText(m.get("b"));
}
}
public void onClick(Viewv){
Stringstr1=edittext1.getText().toString();
Stringstr2=edittext2.getText().toString();
switch (v.getId()){
case R.id.checkBox1:
break;
case R.id.checkBox2:
break;
case R.id.button2:
Log.d("TGA", "当此段代码运行时,会将此内容显示在TGA上");
if(!
TextUtils.isEmpty(str1)&&!
TextUtils.isEmpty(str2)){
if(checkbox1.isChecked()){//判断记住密码选项是否被勾中 如果被选中则记住其帐号和密码
boolean bo=Utils.saveUserInfo(str1,str2);
if(bo){//保存帐号密码并且成功时
Toast.makeText(Aa.this,"保存成功",Toast.LENGTH_LONG).show();
Intentin=new Intent(Aa.this,MainActivity.class);
startActivity(in);
break;
}else{//帐号密码保存失败时
Toast.makeText(Aa.this,"保存失败",Toast.LENGTH_LONG).show();
break;
}
}
}else{//帐号密码为空时
Toast.makeText(Aa.this,"别闹",Toast.LENGTH_LONG).show();
break;
}
Toast.makeText(Aa.this,"登录成功",Toast.LENGTH_LONG).show();
Intentin=newIntent(Aa.this,MainActivity.class);
startActivity(in);
break;
default:
break;
}
}
}
public classUtils{
public static boolean saveUserInfo(Stringa,Stringb) {
Stringc= "/data/data/com.example.text/utils1.txt";
Filef= newFile(c);
try {
FileOutputStreamfi= newFileOutputStream(f);
Stringab=a+ "##"+b;
fi.write(ab.getBytes());
fi.flush();
fi.close();
return true;
}catch(IOExceptione){
e.printStackTrace();
}
return false;
}
public staticMap
try{
Stringc= "/data/data/com.example.text/utils1.txt";
FileInputStreamifs=newFileInputStream(c);
BufferedReaderbr=newBufferedReader(newInputStreamReader(ifs));
Stringaa=br.readLine();
if(!
TextUtils.isEmpty(aa)){
Stringb[]=aa.split("##");
Map
ma.put("a",b[0]);
ma.put("b",b[1]);
br.close();
return ma;
}
br.close();
}catch(Exceptione){
}
return null;
}
}
//保存数据到sd卡
//写入SD卡的权限
name="android.permission.WRITE_EXTERNAL_STORAGE"/> //读取SD卡的权限 name="android.permission.READ_EXTERNAL_STORAGE"/> package com.example.text.utils; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import android.content.Context; import android.os.Environment; import android.text.TextUtils; public classUtilsOfSDCard{ //保存用户信息到SD卡 public static boolean saveUserInfo(Contextcontex, Stringa,Stringb) { Stringstr= Environment.getExternalStorageState(); if(! Environment.MEDIA_MOUNTED.equals(str)){//判断sd卡是否挂载 挂载返回! true return false; } FilesdCardFile=Environment.getExternalStorageDirectory(); Filef=newFile(sdCardFile,"utils1.txt"); try { FileOutputStreamfi=newFileOutputStream(f); Stringdata=a+"##"+b; fi.write(data.getBytes()); fi.flush(); fi.close(); return true; }catch(Exceptione){ e.printStackTrace(); } return false; } //从Sd卡获取用户信息 public staticMap Stringstr=Environment.getExternalStorageState(); if(! Environment.MEDIA_MOUNTED.equals(str)){//判断sd卡是否挂载 挂载返回! true return null; } try { FilesdCardFile=Environment.getExternalStorageDirectory(); Filef=newFile(sdCardFile,"utils1.txt"); BufferedReaderbu=newBufferedReader(newInputStreamReader(new FileInputStream(f))); Stringst=bu.readLine(); bu.close(); if(! TextUtils.isEmpty(st)){ String[]a=st.split("##"); Map m.put("a",a[0]); m.put("b",a[1]); return m; } }catch(Exceptione){ e.printStackTrace(); } return null; } } package com.example.text; import java.util.Map; import com.example.text.utils.UtilsOfSDCard; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public classAa extendsActivity implementsOnClickListener{ CheckBoxcheckbox1; CheckBoxcheckbox2; Buttonbutton2; EditTextedittext1; EditTextedittext2; protected void onCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.aa); ///data/data/包名/files this.getFilesDir(); checkbox1 =(CheckBox)findViewById(R.id.checkBox1); checkbox2 =(CheckBox)findViewById(R.id.checkBox2); button2 =(Button)findViewById(R.id.button2); button2.setOnClickListener(this); edittext2=(EditText)findViewById(R.id.editText2); edittext1=(EditText)findViewById(R.id.editText1); Map if(m! =null){ edittext1.setText(m.get("a")); edittext2.setText(m.get("b")); } } public void onClick(Viewv){ Stringstr1=edittext1.getText().toString(); Stringstr2=edittext2.getText().toString(); switch (v.getId()){ case R.id.checkBox1: break; case R.id.checkBox2: break; case R.id.button2: Log.d("TGA", "当此段代码运行时,会将此内容显示在TGA上"); if(! TextUtils.isEmpty(str1)&&! TextUtils.isEmpty(str2)){ if(checkbox1.isChecked()){//判断记住密码选项是否被勾中 如果被选中则记住其帐号和密码 boolean bo=UtilsOfSDCard.saveUserInfo(this,str1,str2); if(bo){//保存帐号密码并且成功时 Toast.makeText(Aa.this,"保存成功",Toast.LENGTH_LONG).show(); Intentin=newIntent(Aa.this,MainActivity.class); startActivity(in); break; }else{//帐号密码保存失败时 Toast.makeText(Aa.this,"保存失败",Toast.LENGTH_LONG).show(); break; } } }else{//帐号密码为空时 Toast.makeText(Aa.this,"别闹",Toast.LENGTH_LONG).show(); break; } Toast.makeText(Aa.this,"登录成功",Toast.LENGTH_LONG).show(); Intentin=newIntent(Aa.this,MainActivity.class); startActivity(in); break; default: break; } } }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- AndroidWkk 保存 数据 手机 内存 Sd 资料