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) {
//說明沒有動態(tài)權限需要申請
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"/>
動態(tài)權限與sd卡存儲密碼
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來番电,“玉大人,你說我怎么就攤上這事辆琅∈欤” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵婉烟,是天一觀的道長娩井。 經(jīng)常有香客問我,道長似袁,這世上最難降的妖魔是什么洞辣? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮昙衅,結果婚禮上扬霜,老公的妹妹穿的比我還像新娘。我一直安慰自己而涉,他們只是感情好著瓶,可當我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著啼县,像睡著了一般材原。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上季眷,一...
- 文/蒼蘭香墨 我猛地睜開眼卵慰,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了佛呻?” 一聲冷哼從身側響起裳朋,我...
- 正文 年R本政府宣布谚殊,位于F島的核電站丧鸯,受9級特大地震影響,放射性物質發(fā)生泄漏嫩絮。R本人自食惡果不足惜丛肢,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望剿干。 院中可真熱鬧蜂怎,春花似錦、人聲如沸怨愤。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽撰洗。三九已至篮愉,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間差导,已是汗流浹背试躏。 一陣腳步聲響...
推薦閱讀更多精彩內容
- Android鐘對數(shù)據(jù)的存儲與訪問是很有必要的,在Android中對于數(shù)據(jù)存儲提供了如下幾種方法: 文件形式Sha...
- 直接上重點: 1:獲取內置SD卡的路徑, 但是判斷是否有效(是否掛載), 需要用到下面檢測掛載點的方法 2:枚舉系...
- 我們的app一般都會需要緩存和一些圖片的存儲,當然我們的目錄可以是自己的私有目錄,getExternalCache...
- 一员舵、保存文件到手機內存 二、保存文件到SD卡 獲取手機sd空間的大信号稀: 加入寫外部存儲的權限: 三马僻、Sharedp...
- 之前出了個烏龍 接上一篇文章:Android 6.0 讀寫SD卡權限問題STEP5、最簡單的方法----push之...