android开发笔记之Instrumentation一个小应用
来源:互联网 发布:网络课和直播课的区别 编辑:程序博客网 时间:2024/06/05 07:21
Instrumentation:
在做东西的时候,有时候要模拟一个按键的操作,如按下menu,home,back键,这个时,Instrumentation能完成这个操作
import android.os.Bundle;import android.app.Activity;import android.app.Instrumentation;import android.view.KeyEvent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;
public class InstrumentationActivity extends Activity implements OnClickListener {private Button testKeyMenu;private Button testKeyHome;private Button testKeyBack;private Instrumentation instrumentation;
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_instrumentation);testKeyMenu = (Button)findViewById(R.id.testKeyMenu);testKeyHome = (Button)findViewById(R.id.testKeyHome);testKeyBack = (Button)findViewById(R.id.testKeyBack);testKeyMenu.setOnClickListener(this);testKeyHome.setOnClickListener(this);testKeyBack.setOnClickListener(this);instrumentation = new Instrumentation();}
public boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.instrumentation, menu);return true;}
public void onClick(View view) {// TODO Auto-generated method stubint id = view.getId();switch(id){case R.id.testKeyMenu:sendKey(KeyEvent.KEYCODE_MENU);break;case R.id.testKeyHome:sendKey(KeyEvent.KEYCODE_HOME);break;case R.id.testKeyBack:sendKey(KeyEvent.KEYCODE_BACK);break;}}
private void sendKey(final int keycode) {// TODO Auto-generated method stubnew Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubinstrumentation.sendKeyDownUpSync(keycode);}}).start();}
}
0 0
- android开发笔记之Instrumentation一个小应用
- Android应用开发学习笔记之启动另外一个Activity
- android开发笔记之handler+Runnable的一个巧妙应用
- 【Android应用开发】Instrumentation简单的模拟按键发送消息
- Android测试之Instrumentation
- android之Instrumentation
- Android开发-API指南-<instrumentation>
- 一个小程序:Instrumentation的使用
- android开发笔记之来电小骗子
- Android开发之小问题笔记
- Android应用开发学习笔记之Selector
- Android应用开发学习笔记之进度条
- Android应用开发学习笔记之startActivityForResult
- Android应用开发学习笔记之Fragment
- Android应用开发学习笔记之Intent
- Android应用开发学习笔记之菜单
- Android应用开发学习笔记之绘图
- Android应用开发学习笔记之ContentProvider
- JKS和PKCS12两种格式证书互转代码如下:
- poj 3252 Round Numbers(数位dp+组合计数)
- ACM-DFS之SumItUp——hdu1258
- beaglebone black EZSDK linux从EMMC中启动一
- jquery里面的trigger和triggerHandler的区别
- android开发笔记之Instrumentation一个小应用
- vs2012 俄罗斯方块游戏
- 最速下降法
- 3.为自定义控件添加属性
- Java 8 简明教程
- Cool JavaScript Tricks
- java.sql.SQLException: Io 异常: Got minus one from a read call
- Android 自动化测试—robotium(三)EditText控件
- Linux学习——shell编程