package com.hellogv.slinput;
import java.io.DataOutputStream;
import java.io.IOException;
import android.util.Log;
/**
* 调用su执行input命令
* 全局只调用一次init()和exit(),多次调用run()。
* @author hellogv
*
*/
public class RootCommand {
private String TAG="RootCommand";
private Process process = null;
private DataOutputStream os = null;
public void init() {
try {
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
} catch (IOException e) {
Log.e(TAG, getExceptionMessage(e));
}
}
/**
* 模仿shell来执行命令,必须先root再使用
*
* @param command
* @return
*/
public boolean run(String command) {
try {
os.writeBytes(command + "\n");
os.flush();
} catch (Exception e) {
Log.e(TAG, getExceptionMessage(e));
return false;
}
return true;
}
/**
* 模仿shell来执行命令,必须先root再使用
*
* @param command
* @return
*/
public void release() {
try {
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
Log.e(TAG, getExceptionMessage(e));
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
}
}
}
private static String getExceptionMessage(Exception ex){
String result="";
StackTraceElement[] stes = ex.getStackTrace();
for(int i=0;i<stes.length;i++){
result=result+stes[i].getClassName()
+ "." + stes[i].getMethodName()
+ " " + stes[i].getLineNumber() +"line"
+"\r\n";
}
return result;
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rootCommand.init();
//模拟按下Home键
btnTestKey = (Button) this.findViewById(R.id.btnTestKey);
btnTestKey.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//命令格式:input keyevent keycode
rootCommand.run("/system/bin/input keyevent "+KeyEvent.KEYCODE_HOME);
}
});
//模拟滑动触摸屏
btnTestSwipe= (Button) this.findViewById(R.id.btnTestSwipe);
btnTestSwipe.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
int x2 = MainActivity.this.getWindow().getDecorView().getWidth() - 10;
//先去到桌面
rootCommand.run("/system/bin/input keyevent "+KeyEvent.KEYCODE_HOME);
//滑动桌面,命令格式:input swipe x1 y1 x2 y2
for(int i=0;i<4;i++){
rootCommand.run("/system/bin/input swipe 10 300 "+x2+" 400");
rootCommand.run("/system/bin/input swipe "+x2+" 300 10 400");
}
}
});
//模拟点击触摸屏
btnTestTap= (Button) this.findViewById(R.id.btnTestTap);
btnTestTap.setOnClickListener( new OnClickListener(){
@Override
public void onClick(View v) {
int[] location = new int[2];
btnTestSwipe.getLocationOnScreen(location);
int x = location[0]+btnTestSwipe.getWidth()/2;
int y = location[1]+btnTestSwipe.getHeight()/2;
//模拟点击btnTestTap
rootCommand.run("/system/bin/input tap "+x+" "+y);
}
});
//退出程序
btnExit = (Button) this.findViewById(R.id.btnExit);
btnExit.setOnClickListener( new OnClickListener(){
@Override
public void onClick(View v) {
rootCommand.release();
MainActivity.this.finish();
}
});
//判断是否root过,没root过不可用
if(RootTools.isRootAvailable()==false){
Toast.makeText(this, "本程序需要使用ROOT权限。", Toast.LENGTH_SHORT).show();
this.finish();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有