java 安卓界面 可视化_Monkey可视化工具开发(android篇)
前几天分享了pc端的monkey可视化工具,今天来分享一下如何用android实现monkey的运行,原理是执行shell命令,各种传参,该工具需要root授权
先附上两个效果图:
image.png
image.png
以下为代码:
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.getText().toString();
String systemClickPercent = editText3.getText().toString();
String startActivityPercent = editText4.getText().toString();
String seedNum = editText5.getText().toString();
String delayTime = editText6.getText().toString();
String times = editText7.getText().toString();
if (swipePercent.equals("") == false) {
swipePercent = " --pct-motion " + swipePercent;
} else {
swipePercent = "";
}
if (clickPrcent.equals("") == false) {
clickPrcent = " --pct-touch " + clickPrcent;
} else {
clickPrcent = "";
}
if (systemClickPercent.equals("") == false) {
systemClickPercent = " --pct-syskeys " + systemClickPercent;
} else {
systemClickPercent = "";
}
if (startActivityPercent.equals("") == false) {
startActivityPercent = " --pct-appswitch " + startActivityPercent;
} else {
startActivityPercent = "";
}
if (seedNum.equals("") == false) {
seedNum = " -s " + seedNum;
} else {
seedNum = "";
}
if (delayTime.equals("") == false) {
delayTime = " --throttle " + delayTime;
} else {
delayTime = "";
}
if (times.equals("") == false) {
times = " " + times;
String command = "monkey -p " + button_package.getText() + delayTime + swipePercent + systemClickPercent + startActivityPercent + clickPrcent + seedNum + times;
Log.i("monkeyarg", "======" + command);
ShellUtils.execCommand(command, true);
} else {
Looper.prepare();
Toast toast = Toast.makeText(getApplicationContext(), "请输入monkey事件次数", Toast.LENGTH_SHORT);
toast.show();
Looper.loop();
}
}
}
}
2.MainActivity布局如下:
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/button_package"
android:gravity="center"
android:text="选择应用"
android:background="@drawable/textview_border"
/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ed_1"
android:inputType="number"
android:hint="输入monkey滑动事件比例"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ed_2"
android:inputType="number"
android:hint="输入monkey点击事件比例"/>
android:layout_width="match_parent"
android:id="@+id/ed_3"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="输入monkey系统按钮事件比例"/>
android:layout_width="match_parent"
android:id="@+id/ed_4"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="输入monkey的activity启动比例"/>
android:layout_width="match_parent"
android:id="@+id/ed_5"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="输入monkey随机种子数"/>
android:layout_width="match_parent"
android:id="@+id/ed_6"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="输入monkey延时时间"/>
android:layout_width="match_parent"
android:id="@+id/ed_7"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="输入monkey的事件数(必填)"/>
android:text="启动monkey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button" />
android:text="说明"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1" />
3.在drawable下添加一个样式文件textview_border.xml
4.shell命令执行工具类如下
package com.example.administrator.monkeyshareblog;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
/**
* Created by Administrator on 2017/7/13.
* shell执行工具类
*/
public class ShellUtils {
public static final String COMMAND_SU = "su";
public static final String COMMAND_SH = "sh";
public static final String COMMAND_EXIT = "exit\n";
public static final String COMMAND_LINE_END = "\n";
private ShellUtils() {
throw new AssertionError();
}
/**
* check whether has root permission
*
* @return
*/
public static boolean checkRootPermission() {
return execCommand("echo root", true, false).result == 0;
}
/**
* execute shell command, default return result msg
*
* @param command command
* @param isRoot whether need to run with root
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(String command, boolean isRoot) {
return execCommand(new String[] {command}, isRoot, true);
}
/**
* execute shell commands, default return result msg
*
* @param commands command list
* @param isRoot whether need to run with root
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(List commands, boolean isRoot) {
return execCommand(commands == null ? null : commands.toArray(new String[] {}), isRoot, true);
}
/**
* execute shell commands, default return result msg
*
* @param commands command array
* @param isRoot whether need to run with root
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(String[] commands, boolean isRoot) {
return execCommand(commands, isRoot, true);
}
/**
* execute shell command
*
* @param command command
* @param isRoot whether need to run with root
* @param isNeedResultMsg whether need result msg
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(String command, boolean isRoot, boolean isNeedResultMsg) {
return execCommand(new String[] {command}, isRoot, isNeedResultMsg);
}
/**
* execute shell commands
*
* @param commands command list
* @param isRoot whether need to run with root
* @param isNeedResultMsg whether need result msg
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(List commands, boolean isRoot, boolean isNeedResultMsg) {
return execCommand(commands == null ? null : commands.toArray(new String[] {}), isRoot, isNeedResultMsg);
}
/**
* execute shell commands
*
* @param commands command array
* @param isRoot whether need to run with root
* @param isNeedResultMsg whether need result msg
* @return
*
if isNeedResultMsg is false, {@link CommandResult#successMsg} is null and
* {@link CommandResult#errorMsg} is null.
*
if {@link CommandResult#result} is -1, there maybe some excepiton.
*
*/
public static CommandResult execCommand(String[] commands, boolean isRoot, boolean isNeedResultMsg) {
int result = -1;
if (commands == null || commands.length == 0) {
return new CommandResult(result, null, null);
}
Process process = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = null;
StringBuilder errorMsg = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
os = new DataOutputStream(process.getOutputStream());
for (String command : commands) {
if (command == null) {
continue;
}
// donnot use os.writeBytes(commmand), avoid chinese charset error
os.write(command.getBytes());
os.writeBytes(COMMAND_LINE_END);
os.flush();
}
os.writeBytes(COMMAND_EXIT);
os.flush();
result = process.waitFor();
// get command result
if (isNeedResultMsg) {
successMsg = new StringBuilder();
errorMsg = new StringBuilder();
successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String s;
while ((s = successResult.readLine()) != null) {
successMsg.append(s);
}
while ((s = errorResult.readLine()) != null) {
errorMsg.append(s);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
if (successResult != null) {
successResult.close();
}
if (errorResult != null) {
errorResult.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (process != null) {
process.destroy();
}
}
return new CommandResult(result, successMsg == null ? null : successMsg.toString(), errorMsg == null ? null
: errorMsg.toString());
}
/**
* result of command
*
*
{@link CommandResult#result} means result of command, 0 means normal, else means error, same to excute in
* linux shell
*
{@link CommandResult#successMsg} means success message of command result
*
{@link CommandResult#errorMsg} means error message of command result
*
*
* @author Trinea 2013-5-16
*/
public static class CommandResult {
/** result of command **/
public int result;
/** success message of command result **/
public String successMsg;
/** error message of command result **/
public String errorMsg;
public CommandResult(int result) {
this.result = result;
}
public CommandResult(int result, String successMsg, String errorMsg) {
this.result = result;
this.successMsg = successMsg;
this.errorMsg = errorMsg;
}
}
}
5.新建一个activity,命名为PackageManageActivity ,代码如下
package com.example.administrator.monkeyshareblog;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
//包名选择activity
public class PackageManageActivity extends AppCompatActivity {
private ListView lv_app_list;
private AppAdapter mAppAdapter;
public Handler mHandler = new Handler();
List appInfos=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_package_manage);
lv_app_list = (ListView) findViewById(R.id.lv_app_list);
mAppAdapter = new AppAdapter();
lv_app_list.setAdapter(mAppAdapter);
initAppList();
//点击listView条目跳转回主界面,并传包名到主应用
lv_app_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
MyAppInfo myAppInfo=appInfos.get(position);
Log.i("pacagename",myAppInfo.getAppName());
Intent intent=new Intent(PackageManageActivity.this,MainActivity.class);
intent.putExtra("packagename",myAppInfo.getAppName());
Log.i("packagew1",myAppInfo.getAppName());
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}
@Override
protected void onResume() {
super.onResume();
}
//获取app包名list并初始化界面
private void initAppList(){
new Thread(){
@Override
public void run() {
super.run();
//扫描得到APP列表
appInfos = ApkTool.scanLocalInstallAppList(PackageManageActivity.this.getPackageManager());
Log.i("pacagename11",appInfos.get(1).getAppName().toString());
mHandler.post(new Runnable() {
@Override
public void run() {
mAppAdapter.setData(appInfos);
}
});
}
}.start();
}
class AppAdapter extends BaseAdapter {
List myAppInfos = new ArrayList<>();
public void setData(List myAppInfos) {
this.myAppInfos = myAppInfos;
notifyDataSetChanged();
}
public List getData() {
return myAppInfos;
}
@Override
public int getCount() {
if (myAppInfos != null && myAppInfos.size() > 0) {
return myAppInfos.size();
}
return 0;
}
@Override
public Object getItem(int position) {
if (myAppInfos != null && myAppInfos.size() > 0) {
return myAppInfos.get(position);
}
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
//listview绑定数据
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mViewHolder;
MyAppInfo myAppInfo = myAppInfos.get(position);
if (convertView == null) {
mViewHolder = new ViewHolder();
convertView = LayoutInflater.from(getBaseContext()).inflate(R.layout.package_item, null);
mViewHolder.iv_app_icon = (ImageView) convertView.findViewById(R.id.iv_app_icon);
mViewHolder.tx_app_name = (TextView) convertView.findViewById(R.id.tv_app_name);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (ViewHolder) convertView.getTag();
}
mViewHolder.iv_app_icon.setImageDrawable(myAppInfo.getImage());
mViewHolder.tx_app_name.setText(myAppInfo.getAppName());
return convertView;
}
class ViewHolder {
ImageView iv_app_icon;
TextView tx_app_name;
}
}
}
6.PackageManageActivity的布局代码如下
android:orientation="vertical"
android:id="@+id/activity_package_manage"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/lv_app_list"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="match_parent">
7.新添一个listView条目布局文件package_item
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="60dp">
android:id="@+id/iv_app_icon"
android:layout_weight="4"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_weight="1"
android:gravity="center_vertical"
android:id="@+id/tv_app_name"
android:layout_width="match_parent"
android:layout_height="match_parent" />
java 安卓界面 可视化_Monkey可视化工具开发(android篇)相关推荐
- Android连载之:第二章第三节:利用其他的开发环境和工具开发Android应用程序
2.3 利用其他的开发环境和工具开发Android应用程序 推荐使用带有Android插件的Eclipse来开发Android应用程序,ADT插件提供了编辑.编译.调试功能并集成进了IDE中.然而,S ...
- react native开发Android 篇——APP名称、图标、启动页
react native开发Android 篇--APP名称.图标.启动页 设置APP名称 设置APP图标 设置启动页 隐藏启动页 设置APP名称 编辑 android/app/src/main/re ...
- react native开发Android 篇——集成自定义的字体
react native开发Android 篇--集成自定义的字体 第一种:link添加自定义字体 第二种:直接复制字体到`android/app/src/main/assets/fonts`目录下 ...
- Go、Java、C++,下一代测序工具开发谁更强?
作者 | Pascal Costanza, Charlotte Herzeel & Wilfried Verachtert 译者 | 弯月,责编 | 夕颜 出品 | CSDN(ID:CSDNn ...
- python怎么开发安卓程序_怎样用python开发安卓app-到底如何使用Python开发Android程序.txt...
Python是一种动态语言,是比较简单的. Android不直接支持使用Python开发的应用程序,它需要使用它的中间件或数据库.它提供了在Android平台上的Python语言的支持; Python ...
- AutomatorX自动化测试工具介绍(Android篇)
准备环境 先准备一台安卓手机,电脑上配置好Python环境. 根据 ATX官方主页上的说明,把环境配置好. https://github.com/NetEaseGame/AutomatorX 需要用到 ...
- Java安卓适配全面屏_GitHub - chnagzhaolin/NotchScreenTool: Android刘海屏、水滴屏等全面屏适配工具...
NotchScreenTool 适配刘海屏水滴屏等全面屏工具 具体的适配过程可以查看这篇博客: 安装 项目根目录的build.gradle中添加: allprojects { repositories ...
- java安卓6.0闪退_Android开发activity跳转闪退
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 现在调试也是闪退 Java.lang.RuntimeException: Fail to connect to camera service at and ...
- 在ubuntu用arm ds-5社区版配合linaro交叉编译工具开发android linux应用
下载开发工具arm ds-5社区版,并安装 下载交叉编译工具linaro, 也可以使用ds-5自带的交叉编译工具链(需要旗舰版本), sourcery , Android NDK bundle或者自行 ...
最新文章
- LIS ZOJ - 4028
- 计算机网络系统中hn是,中南大学计算机网络作业1.pdf
- javascript常用的事件
- javaScript初学者易错点
- destoon php,DESTOON_7.0_UTF8
- 使用coding.net上传项目
- ITK:排序ITK索引
- 未来ui设计的发展趋势_2025年的未来UI趋势?
- 第 8 章 查找算法
- Idea添加Jetty时提示JMX module is not included
- 《从零开始学Swift》学习笔记(Day 13)——数据类型之整型和浮点型
- 祝贺在龙芯平台上编译jogamp(gluegen/jogl)2.3.2通过,并运行成功
- HTML5期末大作业:动漫网站设计——千与千寻(10页) 含设计报告 HTML+CSS+JavaScript 学生动漫网页设计模板下载 海贼王大学生HTML网页制作作品
- 浅谈Johnson算法
- WIFI共享大师无法开启发射功能
- 壳 查壳 去壳 加壳的基本原理
- git报错 failed: The TLS connection was non-properly terminated
- ITEXT 把表格定位在固定位置
- 电脑和手机如何将PPT转成PDF?
- LeetCode:Database 21.统计各专业学生人数
热门文章
- 20应用统计考研复试要点(part33)--简答题
- Python 面向对象编程:类的创建与初始化、实例属性与方法、类属性与方法
- 数据分析工具评测丨Yonghong Desktop对战Tableau Desktop
- Sklearn参数详解—GBDT
- 什么是 SAP UI5 的 Element binding
- 在 SAP BTP Kyma Runtime 上使用 Redis 读取和存储数据
- Angular router-outlet占位符层级结构的子节点,运行时是如何插入的
- 一个SAP UI5 TreeTable控件的错误分析
- SAP Spartacus Storefront 页面 cx-page-layout 的赋值逻辑
- Angular [(ngModel)]的ng-dirty设置时机