前几天分享了pc端的monkey可视化工具,今天来分享一下如何用android实现monkey的运行,原理是执行shell命令,各种传参,该工具需要root授权
先附上两个效果图:
以下为代码:
1.MainActivity代码如下
package com.example.administrator.monkeyshareblog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
Button button,button1;
EditText editText1,editText2,editText3,editText4,editText5,editText6,editText7;
TextView button_package;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
initView();
Bundle bundle=getIntent().getExtras();
try{
String str = bundle.getString("packagename");
Log.i("packagew",str);
button_package.setText(str);
}catch (NullPointerException e){
String text = "选择一个应用";
button_package.setText(text);
}
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,NewActivity.class);
startActivity(intent);
}
});
//点击跳转到包选择界面
button_package.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//ShowChoise();
Intent intent = new Intent(MainActivity.this,PackageManageActivity.class);
startActivity(intent);
}
});
String times=editText6.getText().toString();
Log.i("111",times+"==========");
//执行monkey按钮
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MonkeyThread().start();
}
});
}
//初始化控件
private void initView() {
button= (Button) findViewById(R.id.button);
button1=(Button)findViewById(R.id.button1);
editText1= (EditText) findViewById(R.id.ed_1);
editText2= (EditText) findViewById(R.id.ed_2);
editText3=(EditText)findViewById(R.id.ed_3);
editText4=(EditText)findViewById(R.id.ed_4);
editText5=(EditText)findViewById(R.id.ed_5);
editText6=(EditText)findViewById(R.id.ed_6);
editText7=(EditText)findViewById(R.id.ed_7);
button_package=(TextView) findViewById(R.id.button_package);
}
//monkey执行线程
class MonkeyThread extends Thread{
@Override
public void run() {
super.run();
String swipePercent = editText1.getText().toString();
String clickPrcent = editText2.