当前位置: 移动技术网 > 移动技术>移动开发>Android > Android提高之手游转电视游戏的模拟操控

Android提高之手游转电视游戏的模拟操控

2019年07月24日  | 移动技术网移动技术  | 我要评论

目前智能电视终端(智能电视和智能电视盒子)已经越来越火,过去主打视频功能,如今的智能电视终端不仅会继续完善视频功能,还会加入电视游戏功能,同时这也赶上了“电视游戏机解禁”的时机。

当今的大部分android手游都能够在android系统的电视终端上运行,其中有少数手游是原生支持手柄(例如moga手柄),这部分游戏可以作为电视游戏。但其他手游(射击,赛车,动作等游戏)若要在电视上玩,就需要修改操控模式,把触摸屏操控改为手柄实体键操控。

本文主要讲解的是如何使用/system/bin/之下的input命令模拟按键和触摸屏操作,调用input命令需要具备root权限。本文完整代码点击此处。

程序运行结果如下图所示:

 

本文核心rootcommand.java的代码如下,不建议把代码浓缩成全局静态方法,这里保持process和os这2个变量的生命周期直到app结束,可以减去多次初始化/释放的耗时。具体代码如下:

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;
 }
}

调用rootcommand的代码如下,input命令的使用格式详见代码:

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();
}
}

感兴趣的朋友可以下载本实例的完整代码加以调试运行,相信会对大家的android程序设计有很大的帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网