package com.example.pengtuanyuan.logindemo01;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.text.format.Formatter;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText ed_inputName;
private EditText ed_inputPassword;
private CheckBox cb_choice;
private Button bt_login;
private static Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext=this;
ed_inputName = (EditText) findViewById(R.id.ed_inputName);
ed_inputPassword = (EditText) findViewById(R.id.ed_inputPassword);
cb_choice = (CheckBox) findViewById(R.id.cb_choice);
bt_login = (Button) findViewById(R.id.bt_login);
bt_login.setOnClickListener(this);
Map<String,String> map=UserInfoUtil.getUserInfo_android(mContext);
if (map!=null){
String userName=map.get("useName");
String password=map.get("usePassword");
ed_inputName.setText(userName);
ed_inputPassword.setText(password);
cb_choice.setChecked(true);
}
File sdcard_filedir = Environment.getExternalStorageDirectory();
long usableSpace = sdcard_filedir.getUsableSpace();
long totalSpace = sdcard_filedir.getTotalSpace();
String usableSpace_str = Formatter.formatFileSize(mContext, usableSpace);
String totalSpace_str = Formatter.formatFileSize(mContext, totalSpace);
if(usableSpace <1024*1024*200){
Toast.makeText(mContext,"SD card have not enough space,the rest is :"+usableSpace_str,Toast.LENGTH_SHORT).show();
return;
}
}
private void login(){
String useName = ed_inputName.getText().toString().trim();
String usePassword = ed_inputPassword.getText().toString().trim();
boolean isChecked=cb_choice.isChecked();
if (TextUtils.isEmpty(useName)||TextUtils.isEmpty(usePassword)){
Toast.makeText(mContext,"Name and password cannot be empty",Toast.LENGTH_SHORT).show();
return;
}
if (isChecked){
if(! Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Toast.makeText(mContext,"SDcard no be mounted",Toast.LENGTH_SHORT).show();
return;
}
Boolean result=UserInfoUtil.saveUserInfo_android(mContext,useName,usePassword);
if (result){
Toast.makeText(mContext,"Name and password be saved",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(mContext,"Name and password cannot be saved",Toast.LENGTH_SHORT).show();
}
}else {
Toast.makeText(mContext,"Name and password don't need to be saved",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_login:
if((ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE))!= PackageManager.PERMISSION_GRANTED) {
//说明没有动态权限需要申请
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},0);
}else {
//说明有权限直接调用方法
login();
}
break;
default:
break;
}
}
}
----------------------------------------------------------
package com.example.pengtuanyuan.logindemo01;
import android.content.Context;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class UserInfoUtil {
public static Boolean saveUserInfo_android (Context context,String useName, String usePassword) {
try {
String userInfo=useName+"##"+usePassword;
FileOutputStream fileOutputStream = context.openFileOutput("userinfo.txt", Context.MODE_PRIVATE);
fileOutputStream.write(userInfo.getBytes());
fileOutputStream.close();
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public static Map<String,String> getUserInfo_android(Context context){
try {
FileInputStream fileInputStream = context.openFileInput("userinfo.txt");
BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(fileInputStream));
String readLine =bufferedReader.readLine();
String[] split=readLine.split("##");
HashMap<String,String> hashMap=new HashMap<String,String>();
hashMap.put("useName",split[0]);
hashMap.put("usePassword",split[1]);
bufferedReader.close();
fileInputStream.close();
return hashMap;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
------------------------------------------------
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
动态权限与sd卡存储密码
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Android钟对数据的存储与访问是很有必要的,在Android中对于数据存储提供了如下几种方法: 文件形式Sha...
- 直接上重点: 1:获取内置SD卡的路径, 但是判断是否有效(是否挂载), 需要用到下面检测挂载点的方法 2:枚举系...
- 我们的app一般都会需要缓存和一些图片的存储,当然我们的目录可以是自己的私有目录,getExternalCache...
- 一、保存文件到手机内存 二、保存文件到SD卡 获取手机sd空间的大小: 加入写外部存储的权限: 三、Sharedp...
- 之前出了个乌龙 接上一篇文章:Android 6.0 读写SD卡权限问题STEP5、最简单的方法----push之...