android系统下sd卡里文件识别

来源:互联网 发布:vb.netide 编辑:程序博客网 时间:2024/06/03 02:16

废话不多说,先上图:

关键就在SD_IdentifyActivity类中

SD_IdentifyActivity.java

package com.haungcheng.sd;import java.io.File;import com.huangcheng.file.FileInput;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.widget.Toast;public class SD_IdentifyActivity extends Activity {/** Called when the activity is first created. */private Handler handler;private Message msg;private String path = "/sdcard/identify.txt"; // 是别的文件路劲private boolean sign = false;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);this.setTitle("认证");Thread thread = new Thread() {@Overridepublic void run() {int waitingTime = 5000; // mstry {while (waitingTime > 0) {msg = handler.obtainMessage();sleep(5000);waitingTime -= 500; // 500ms per timeFile file = new File(path);if (file.exists() && file.canRead()) {byte[] fileinput = FileInput.input(path);String str_id = new String(fileinput);System.out.println("读取的身份信息:" + str_id);if (str_id.equals("huangcheng")) {msg.what = 2;waitingTime = 0;sign = true;}} else {msg.what = 1;waitingTime = 0;sign = true;}handler.sendMessage(msg);}if (sign == false) {msg = handler.obtainMessage();msg.what = 3;handler.sendMessage(msg);}} catch (InterruptedException e) {e.printStackTrace();}}};handler = new Handler() {@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubsuper.handleMessage(msg);if (msg.what == 1) {// 无sd卡直接退出Toast.makeText(SD_IdentifyActivity.this,"找不到SDCARD,请插入新的SDCARD!", Toast.LENGTH_LONG).show();} else if (msg.what == 2) {// 认证sdcardToast.makeText(SD_IdentifyActivity.this, "认证成功",Toast.LENGTH_LONG).show();SD_IdentifyActivity.this.finish();} else if (msg.what == 3) {// 认证超时Toast.makeText(SD_IdentifyActivity.this, "认证超时,退出请重试!",Toast.LENGTH_LONG).show();SD_IdentifyActivity.this.finish();}}};thread.start();}}


很简单的方法,源代码我已上传,地址是http://download.csdn.net/detail/hc260164797/4598427

原创粉丝点击