美文网首页
动态权限与sd卡存储密码

动态权限与sd卡存储密码

作者: pengtuanyuan | 来源:发表于2016-10-22 19:04 被阅读0次
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卡存储密码

      本文链接:https://www.haomeiwen.com/subject/bjehuttx.html