随笔 - 633,  文章 - 0,  评论 - 13,  阅读 - 48万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

一.主布局文件 activity_main.xml

复制代码
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <EditText
        android:id="@+id/edit_monkeyPackage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/monkeypackage"
        android:maxLines="1" >
    </EditText>
    
    <EditText
        android:id="@+id/edit_monkeytime"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/monkeytime"
        android:maxLines="1" >
    </EditText>
    
    <EditText
        android:id="@+id/edit_monkeycount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/monkeycount"
        android:maxLines="1" >
    </EditText>
    <Button
             android:id="@+id/button_submit"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:text="@string/button_submit" />

    
    <TextView
            android:id="@+id/TextView_01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/TextView_01" />
    


</LinearLayout>
复制代码

二. res/values/strings.xml 

复制代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">MonkeyTest</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="monkeypackage">Input monkeyPackage</string>
    <string name="monkeytime">Input monkeyTime</string>
    <string name="monkeycount">Input monkeyCount</string>
    <string name="button_submit">Submit</string>
    <string name="TextView_01">Log Out</string>

</resources>
复制代码

三.主Activity文件 MainActivity.java

复制代码
package com.example.runmonkeytest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

import android.widget.TextView;
import android.widget.Toast;





@SuppressLint("NewApi")
public class MainActivity extends Activity {

    private EditText monkeyPackage;
    private EditText monkeyTime;
    private EditText monkeyCount;
    private TextView LogOut;
    
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        
        monkeyPackage = (EditText) findViewById(R.id.edit_monkeyPackage);
        monkeyTime = (EditText) findViewById(R.id.edit_monkeytime);
        monkeyCount = (EditText) findViewById(R.id.edit_monkeycount);
        LogOut = (TextView) findViewById(R.id.TextView_01);   
        Button button1 =(Button) findViewById(R.id.button_submit);
        
        
        
        button1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
//                Toast.makeText(MonkeyTestActivity.this, "You clicked Button Submit", Toast.LENGTH_SHORT).show();
                String monkeyPackageContext = monkeyPackage.getText().toString();
                String monkeyTimeContext = monkeyTime.getText().toString();
                String monkeyCountContext = monkeyCount.getText().toString();
                

                
                if(monkeyPackageContext.isEmpty()){
                    Toast.makeText(MainActivity.this, "input packageName", Toast.LENGTH_SHORT).show();
                }else if(monkeyTimeContext.isEmpty()){
                    Toast.makeText(MainActivity.this, "input pauseTime", Toast.LENGTH_SHORT).show();
                }else if(monkeyCountContext.isEmpty()){
                    Toast.makeText(MainActivity.this, "input Count", Toast.LENGTH_SHORT).show();
                }else{
                    int T = Integer.parseInt(monkeyTimeContext) ;
                    int C = Integer.parseInt(monkeyCountContext);
                    String acctionText = "monkey -p "+monkeyPackageContext+" --throttle "+ 
                            T + " -s 1000 " + " -v -v -v " + C;// + ">/sdcard/aaaaaa_monkey.log";
                    
                
                    Log.d("MonkeyTestActivity", "Toast");    
                    Log.d("MonkeyTestActivity", acctionText);    
                    Toast.makeText(MainActivity.this, acctionText, Toast.LENGTH_SHORT).show();
                    
                    do_exec(acctionText);
                    Log.d("MonkeyTestActivity", "Toast2");
                }
                

                
                }
            }
        );
    }

    protected String do_exec(String cmd) {
           String s = "\n";   
            try {   
                Process p = Runtime.getRuntime().exec(cmd);   
                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();   
            }   
            LogOut.setText(s);   
            return cmd;    
        
    }

}
复制代码

四.AndroidManifest.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.runmonkeytest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.ACCESS_SUPERUSER"/>
    <uses-permission android:name="andorid.permission.WRITE_EXTERNAL_STORAGE"/>

    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.runmonkeytest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk
        android:minSdkVersion="8" 
        />


</manifest>
复制代码

 

posted on   大话人生  阅读(709)  评论(3编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示