app

来源:互联网 发布:同花顺mac版功能全吗 编辑:程序博客网 时间:2024/04/19 01:24
package com.example.tp_break_tset;


import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class TP_break_test extends Activity {
private Button mStart;
private TextView mSum;
private TextView mSuccess;
private TextView mFail;
private int mSum_int = 0;
private int mSuccess_int = 0;
private int mFail_int = 0;
private Thread mThread;
private PowerManager.WakeLock mWakeLock;

private boolean mStart_flag = true;
private Handler mHandler;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mStart = (Button)findViewById(R.id.start);
        mSum = (TextView)findViewById(R.id.sum);
        mSuccess = (TextView)findViewById(R.id.success);
        mFail = (TextView)findViewById(R.id.fail);
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "TAG");
        mWakeLock.acquire();
        mHandler=new Handler() {  
        public void handleMessage(Message msg) {  
        switch(msg.what) {  
        case 1:    
                    break;  
                default:  
                    break;        
                }
        mSum.setText(""+mSum_int);
        mSuccess.setText("" +mSuccess_int);
        mFail.setText("" +mFail_int);
        if(mFail_int > 100){
        mStart.setText("start");
        mStart_flag = true;
        }
                super.handleMessage(msg);  
            }  
        };
        mThread = new Thread(new Runnable() {  
public void run() {
while(true){
if(!mStart_flag){
        String mRetVal = readFile("/proc/touchscreen/rawdata");
        if((mRetVal != null) && mRetVal.contains("1P")){
        mSuccess_int++;
        }else {
        mFail_int++;
        do_exec("mkdir /sdcard/tp");
        do_exec("cp /sdcard/testdata.txt /sdcard/tp/testdata"+mFail_int+".txt");
        }
        mSum_int++;
Message message=new Message();
message.what=1;
mHandler.sendMessage(message);
}
/*
try{
Thread.sleep(3000);
} catch(Exception e){
e.printStackTrace();
}
*/
}
}  
        });
        mThread.start();
        mStart.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View v) {
            if(mStart_flag) {
            mStart.setText("stop");
            }else{
            mStart.setText("start");
            }
            mStart_flag = !mStart_flag;
            }
        });
    }


    public String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }


            if (sb.indexOf("\n") != -1 && sb.lastIndexOf("\n") == sb.length() - 1) {
                sb.delete(sb.lastIndexOf("\n"), sb.lastIndexOf("\n") + 1);
            }


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }


    String readFile(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            System.out.println("the path [" + filePath + "] is not exist");
            return null;
        } else if (!file.canRead()) {
            System.out.println("the path [" + filePath + "] is can't read,permission denied");
            return null;
        } else if (file.isDirectory()) {
            System.out.println("the path [" + filePath + "] is a directory,not file");
            return null;
        }


        try {
            return convertStreamToString(new FileInputStream(filePath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


@Override
protected void onDestroy() {
// TODO Auto-generated method stub
mWakeLock.release();
super.onDestroy();
}


String do_exec(String cmd) {  
        String s = "/n";  
        try {  
            Process p = Runtime.getRuntime().exec(cmd);  
            try{
            p.waitFor();
            }catch(Exception e){
           
            }
            BufferedReader in = new BufferedReader(  
                                new InputStreamReader(p.getInputStream()));  
            String line = null;  
            while ((line = in.readLine()) != null) {  
                s += line + "/n";                 
            }  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        //mSuccess.setText(s);  
        return cmd;       
    }  
}













0 0
原创粉丝点击