执行monkey APK 制作

2018/03/06 17:47
阅读数 14

一.主布局文件 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>

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部