zoukankan      html  css  js  c++  java
  • 执行monkey APK 制作

    一.主布局文件 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 = "
    ";   
                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 + "
    ";                  
                    }   
                } 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>
  • 相关阅读:
    指针和引用的区别
    vs不自动退出控制台程序的办法
    ads出现村田电容电感无法仿真的问题解决(`BJT1' is an instance of an undefined model `BJTM1')
    解决浏览器跨域的几中方式
    js的事件委托机制
    let和const命令
    JavaScript中闭包的写法和作用详解
    提升HTML5的性能体验系列之五 webview启动速度优化及事件顺序解析
    提升HTML5的性能体验系列之四 使用原生UI
    提升HTML5的性能体验系列之三 流畅下拉刷新和上拉
  • 原文地址:https://www.cnblogs.com/jingzaixin/p/8515766.html
Copyright © 2011-2022 走看看