如何记录android点击事件背景

如何记录android点击事件背景,android,Android,我想在使用其他应用程序时记录android点击事件。对于每次单击,我都会记录其时间戳。所以,这个应用程序应该在后台运行。我使用“getevent”捕捉单击事件。我对安卓操作不是很熟悉。我的代码 他有一只虫子。它的输出不是很好,输出中有许多“null”,并且记录不完全正确。(我的应用程序需要root权限) 1.MainActivity.java package kingofne.seu.edu.ndktest; import android.content.Intent; import andr

我想在使用其他应用程序时记录android点击事件。对于每次单击,我都会记录其时间戳。所以,这个应用程序应该在后台运行。我使用“getevent”捕捉单击事件。我对安卓操作不是很熟悉。我的代码 他有一只虫子。它的输出不是很好,输出中有许多“null”,并且记录不完全正确。(我的应用程序需要root权限)

1.MainActivity.java

package kingofne.seu.edu.ndktest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private EditText editText = null;
    private TextView textView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    editText = (EditText) findViewById(R.id.editText);
    textView = (TextView) findViewById(R.id.textView);

    Button btn_start = (Button) findViewById(R.id.btn_start);
    if (btn_start != null) {
        btn_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), MyService.class);
                int param = 1;
                if (editText != null) {
                    param = Integer.valueOf(String.valueOf(editText.getText()));
                }
                intent.putExtra("param", param);
                startService(intent);
                Toast.makeText(getApplicationContext(), "start", Toast.LENGTH_SHORT).show();
                textView.setText(String.valueOf(System.currentTimeMillis()));
            }
        });
    }

    Button btn_stop = (Button) findViewById(R.id.btn_stop);
    if (btn_stop != null) {
        btn_stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), MyService.class);
                stopService(intent);
                Toast.makeText(getApplicationContext(), "stop", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
package kingofne.seu.edu.ndktest;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MyService extends Service {
    private boolean isRunning = false;
    private int param = 1;   
    private Thread captureThread = null;
    private volatile boolean doCapture = false;
    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (isRunning) {
            return START_NOT_STICKY;
        }

        param = intent.getIntExtra("param", 1);
        this.captureThread = new Thread(new Producer());
        captureThread.setDaemon(true);

        this.doCapture = true;
        captureThread.start();

        isRunning = true;

        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        // stop the thread
        this.doCapture = false;

        // destroy the thread
        this.captureThread = null;
    }

    private class Producer implements Runnable {

        public void run() {
            Log.d("ps", "going to run");
            Date now = new Date();
            SimpleDateFormat sDateFormat = new SimpleDateFormat(
                    "yyyy-MM-dd-HH-mm-ss");
            String str2 = sDateFormat.format(now);
            // String location = str2;
            //String cmdLine = "getevent -t -l /dev/input/event" + String.valueOf(param) + " > /sdcard/guo"  + ".txt";
            String cmdLine = "cat /dev/input/event" + String.valueOf(param) + " > /sdcard/guo.txt";
            CMDExecute cmd = new CMDExecute();
            try {
                // cmd.run_su("getevent -t > /sdcard/Yang_"+location+".txt");
                cmd.run_su(cmdLine);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.d("ps", "error");
                e.printStackTrace();
            }
        }
    };


    class CMDExecute {

        public synchronized String run(String[] cmd, String workdirectory)
                throws IOException {
            String result = "";
            try {
                ProcessBuilder builder = new ProcessBuilder(cmd);
                BufferedReader in = null;
                // 设置一个路径
                if (workdirectory != null) {
                    builder.directory(new File(workdirectory));
                    builder.redirectErrorStream(true);
                    Process process = builder.start();
                    process.waitFor();
                    in = new BufferedReader(new InputStreamReader(process
                            .getInputStream()));
                    char[] re = new char[1024];
                    int readNum;
                    while ((readNum = in.read(re, 0, 1024)) != -1) {
                        // re[readNum] = '\0';
                        result = result + new String(re, 0, readNum);
                    }
                }
                if (in != null) {
                    in.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }

            return result;
        }

        public synchronized String run_su(String cmd)
                throws IOException {
            String result = "";
            Process p=null;
            try {
                p = Runtime.getRuntime().exec("su");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    DataOutputStream os = new DataOutputStream(p.getOutputStream());
    BufferedReader is = new BufferedReader(new InputStreamReader(p.getInputStream()));
      try {
                String istmp;
                os.writeBytes(cmd+"\n");
                //os.writeBytes("exit\n");
                os.flush();

                os.writeBytes("exit\n");
                os.flush();
                p.waitFor();
                is.close();
                os.close();
                p.destroy();
            } catch (Exception e) {
                // TODO Auto-generated catch block
          e.printStackTrace();
            } return result;
}
}
}
}

2.MyService.java

package kingofne.seu.edu.ndktest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private EditText editText = null;
    private TextView textView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    editText = (EditText) findViewById(R.id.editText);
    textView = (TextView) findViewById(R.id.textView);

    Button btn_start = (Button) findViewById(R.id.btn_start);
    if (btn_start != null) {
        btn_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), MyService.class);
                int param = 1;
                if (editText != null) {
                    param = Integer.valueOf(String.valueOf(editText.getText()));
                }
                intent.putExtra("param", param);
                startService(intent);
                Toast.makeText(getApplicationContext(), "start", Toast.LENGTH_SHORT).show();
                textView.setText(String.valueOf(System.currentTimeMillis()));
            }
        });
    }

    Button btn_stop = (Button) findViewById(R.id.btn_stop);
    if (btn_stop != null) {
        btn_stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), MyService.class);
                stopService(intent);
                Toast.makeText(getApplicationContext(), "stop", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
package kingofne.seu.edu.ndktest;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MyService extends Service {
    private boolean isRunning = false;
    private int param = 1;   
    private Thread captureThread = null;
    private volatile boolean doCapture = false;
    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (isRunning) {
            return START_NOT_STICKY;
        }

        param = intent.getIntExtra("param", 1);
        this.captureThread = new Thread(new Producer());
        captureThread.setDaemon(true);

        this.doCapture = true;
        captureThread.start();

        isRunning = true;

        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        // stop the thread
        this.doCapture = false;

        // destroy the thread
        this.captureThread = null;
    }

    private class Producer implements Runnable {

        public void run() {
            Log.d("ps", "going to run");
            Date now = new Date();
            SimpleDateFormat sDateFormat = new SimpleDateFormat(
                    "yyyy-MM-dd-HH-mm-ss");
            String str2 = sDateFormat.format(now);
            // String location = str2;
            //String cmdLine = "getevent -t -l /dev/input/event" + String.valueOf(param) + " > /sdcard/guo"  + ".txt";
            String cmdLine = "cat /dev/input/event" + String.valueOf(param) + " > /sdcard/guo.txt";
            CMDExecute cmd = new CMDExecute();
            try {
                // cmd.run_su("getevent -t > /sdcard/Yang_"+location+".txt");
                cmd.run_su(cmdLine);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.d("ps", "error");
                e.printStackTrace();
            }
        }
    };


    class CMDExecute {

        public synchronized String run(String[] cmd, String workdirectory)
                throws IOException {
            String result = "";
            try {
                ProcessBuilder builder = new ProcessBuilder(cmd);
                BufferedReader in = null;
                // 设置一个路径
                if (workdirectory != null) {
                    builder.directory(new File(workdirectory));
                    builder.redirectErrorStream(true);
                    Process process = builder.start();
                    process.waitFor();
                    in = new BufferedReader(new InputStreamReader(process
                            .getInputStream()));
                    char[] re = new char[1024];
                    int readNum;
                    while ((readNum = in.read(re, 0, 1024)) != -1) {
                        // re[readNum] = '\0';
                        result = result + new String(re, 0, readNum);
                    }
                }
                if (in != null) {
                    in.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }

            return result;
        }

        public synchronized String run_su(String cmd)
                throws IOException {
            String result = "";
            Process p=null;
            try {
                p = Runtime.getRuntime().exec("su");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    DataOutputStream os = new DataOutputStream(p.getOutputStream());
    BufferedReader is = new BufferedReader(new InputStreamReader(p.getInputStream()));
      try {
                String istmp;
                os.writeBytes(cmd+"\n");
                //os.writeBytes("exit\n");
                os.flush();

                os.writeBytes("exit\n");
                os.flush();
                p.waitFor();
                is.close();
                os.close();
                p.destroy();
            } catch (Exception e) {
                // TODO Auto-generated catch block
          e.printStackTrace();
            } return result;
}
}
}

时间戳不是系统时间。我们需要进一步转换。时间戳不是系统时间。我们需要进一步转换它。