動態(tài)權限與sd卡存儲密碼

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"/>

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末中燥,一起剝皮案震驚了整個濱河市溪王,隨后出現(xiàn)的幾起案子湃鹊,更是在濱河造成了極大的恐慌,老刑警劉巖随闪,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異叨粘,居然都是意外死亡塞蹭,警方通過查閱死者的電腦和手機孽江,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來番电,“玉大人,你說我怎么就攤上這事辆琅∈欤” “怎么了?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵婉烟,是天一觀的道長娩井。 經(jīng)常有香客問我,道長似袁,這世上最難降的妖魔是什么洞辣? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮昙衅,結果婚禮上扬霜,老公的妹妹穿的比我還像新娘。我一直安慰自己而涉,他們只是感情好著瓶,可當我...
    茶點故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著啼县,像睡著了一般材原。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上季眷,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天余蟹,我揣著相機與錄音,去河邊找鬼子刮。 笑死威酒,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播兼搏,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼卵慰,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了佛呻?” 一聲冷哼從身側響起裳朋,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎吓著,沒想到半個月后鲤嫡,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡绑莺,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年暖眼,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片纺裁。...
    茶點故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡诫肠,死狀恐怖,靈堂內的尸體忽然破棺而出欺缘,到底是詐尸還是另有隱情栋豫,我是刑警寧澤,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布谚殊,位于F島的核電站丧鸯,受9級特大地震影響,放射性物質發(fā)生泄漏嫩絮。R本人自食惡果不足惜丛肢,卻給世界環(huán)境...
    茶點故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望剿干。 院中可真熱鬧蜂怎,春花似錦、人聲如沸怨愤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽撰洗。三九已至篮愉,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間差导,已是汗流浹背试躏。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留设褐,地道東北人颠蕴。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓泣刹,卻偏偏與公主長得像,于是被迫代替她去往敵國和親犀被。 傳聞我的和親對象是個殘疾皇子椅您,可洞房花燭夜當晚...
    茶點故事閱讀 45,047評論 2 355

推薦閱讀更多精彩內容