Android VNC Server
利用shell开启VNC服务的测试版本==
一、直接开启VNC Server
1
)下载地址
2
)参考网址
1、电脑控制手机的另一选择——android vnc server
2、Android vnc server 安装
3
)操作简述
前提:机子需要是破解的,否则su不可执行。
adb push "F:/androidvncserver" /data/(放进去)
adb shell chmod 755 /data/androidvncserver(加可执行权限)
adb shell /data/androidvncserver(执行androidvncserver)
adb forward tcp:5900 tcp:5901(端口重定向,我没弄==)
3.1)Q1:adb push
有些机子破解后adb shell仍是$符,需要su后才是#。不能直接push进入/data/目录。
因而可以先adb push "F:/androidvncserver" /sdcard/,之后adb shell->su->mv /sdcard/androidvncserver /data/
/data/目录可以再建一层,为/data/local。因为如果你要用程序把androidvncserver写入的话,需要给该目录加写权限,这样涉及到的东西少些。
3.2)Q2:chmod
755指rwxr-xr-x,是为了增加其可执行权限。和之前一样,默认是$符需要su才#的破解机子,也只能一步步来:adb shell->su->cd data->chmod a+x androidvncserver->./fbvncserver &
(su:变更用户身份,不指定时为root;a+x:所有用户执行权限;&:后台运行)
3.3)Q3:关闭服务
ps命令查看pid:ps|grep androidvnc*(这个程序里执行不过==)、ps -l androidvncserver
kill <pid>,杀死进程,即可关闭服务。
3.4)Q4:VNC Viewer连接不到==
1、确认android端开了VNC Server的进程
2、PC端看下能不能ping通android的ip
二、程序执行
1
)程序截图
![vnc.png]()
2
)活动类(VNCServerActivity.java
)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class VNCServerActivity extends Activity {
-
- private static final String TAG = "VNCServerActivity";
- private static final boolean LOGD = true;
-
-
- private static final String VNC_SERVER_FILENAME = "fbvncserver";
-
- private GlobalUtil globalUtil;
-
- private Button startBtn, stopBtn;
- private TextView statusView, connectView;
-
-
- private static final int DLG_BASE = 0;
- private static final int DLG_ROOT_FAILED = DLG_BASE + 1;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- startBtn = (Button) findViewById(R.id.startBtn);
- stopBtn = (Button) findViewById(R.id.stopBtn);
- statusView = (TextView) findViewById(R.id.statusView);
- connectView = (TextView) findViewById(R.id.connectView);
-
-
- connectView.setText(getLocalIpAddress() + ":5901");
-
- globalUtil = GlobalUtil.getInstance();
-
- if (initApp()) {
-
- changeViews(globalUtil.getPids(VNC_SERVER_FILENAME).size() >= 1);
- }
- }
-
-
- private void changeViews(boolean isServerOn) {
- startBtn.setEnabled(!isServerOn);
- stopBtn.setEnabled(isServerOn);
- statusView.setText(isServerOn ? R.string.status_run
- : R.string.status_stop);
- }
-
-
- public void startBtn(View v) {
-
- boolean result = globalUtil.rootCommand("/data/" + VNC_SERVER_FILENAME
- + " &");
- if (LOGD)
- Log.d(TAG, "/data/" + VNC_SERVER_FILENAME + " &:\n" + result);
- changeViews(result);
- connectView.setText(getLocalIpAddress() + ":5901");
- }
-
-
- public void stopBtn(View v) {
- ArrayList<String> pidArray = globalUtil.getPids(VNC_SERVER_FILENAME);
- boolean result;
- for (String pid : pidArray) {
- result = globalUtil.rootCommand("kill " + pid);
- if (LOGD)
- Log.d(TAG, "kill " + pid + ":" + result);
- }
- changeViews(false);
- }
-
-
- private boolean initApp() {
- boolean result = globalUtil.rootCommand("chmod 777 "
- + getPackageCodePath());
- if (result) {
- copyVNCServer();
- } else {
- showDialog(DLG_ROOT_FAILED);
- }
- return result;
- }
-
-
- private void copyVNCServer() {
- String filePath = "/data/" + VNC_SERVER_FILENAME;
- File file = new File(filePath);
-
-
- if (!file.exists()) {
- try {
-
- boolean result = globalUtil.rootCommand("chmod a+w /data");
- if (LOGD)
- Log.d(TAG, "==/data/目录增加写权限:" + result + "==");
-
- if (result) {
-
- InputStream is = getAssets().open(VNC_SERVER_FILENAME);
- FileOutputStream fos = new FileOutputStream(file);
- byte[] buffer = new byte[2048];
- int count = 0;
- while ((count = is.read(buffer)) > 0) {
- fos.write(buffer, 0, count);
- }
- fos.close();
- is.close();
- if (LOGD)
- Log.d(TAG, "==" + VNC_SERVER_FILENAME + "文件写入/data/!==");
-
-
- result = globalUtil.rootCommand("chmod a+x " + filePath);
- if (LOGD)
- Log.d(TAG, "==" + filePath + "增加执行权限:" + result + "==");
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- } else {
- if (LOGD)
- Log.d(TAG, "==" + VNC_SERVER_FILENAME + "文件已存在/data/目录下!==");
- }
- }
-
- @Override
- protected Dialog onCreateDialog(int id) {
- switch (id) {
- case DLG_ROOT_FAILED:
- return new AlertDialog.Builder(this)
- .setTitle(R.string.root_title)
- .setMessage(R.string.root_failed)
- .setCancelable(false)
- .setPositiveButton(R.string.dlg_ok,
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog,
- int which) {
- finish();
- }
- }).create();
- }
- return super.onCreateDialog(id);
- }
-
-
- public String getLocalIpAddress() {
- try {
- for (Enumeration<NetworkInterface> en = NetworkInterface
- .getNetworkInterfaces(); en.hasMoreElements();) {
- NetworkInterface intf = en.nextElement();
- for (Enumeration<InetAddress> enumIpAddr = intf
- .getInetAddresses(); enumIpAddr.hasMoreElements();) {
- InetAddress inetAddress = enumIpAddr.nextElement();
- if (!inetAddress.isLoopbackAddress()) {
- return inetAddress.getHostAddress().toString();
- }
- }
- }
- } catch (SocketException e) {
- e.printStackTrace();
- }
- return null;
- }
-
- }
3
)工具类(GlobalUtil.java
)
- public final class GlobalUtil {
-
-
- static class GlobalUtilHolder {
- static GlobalUtil instance = new GlobalUtil();
- }
-
-
- public static GlobalUtil getInstance() {
- return GlobalUtilHolder.instance;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public boolean rootCommand(String cmd) {
- Process process = null;
- DataOutputStream os = null;
- try {
-
- process = Runtime.getRuntime().exec("su");
-
- os = new DataOutputStream(process.getOutputStream());
- os.writeBytes(cmd + "\n");
- os.writeBytes("exit\n");
- os.flush();
- process.waitFor();
- } catch (Exception e) {
- return false;
- } finally {
- try {
- if (null != process) {
- process.destroy();
- }
- if (null != os) {
- os.close();
- }
- } catch (Exception e) {
- }
- }
- return true;
- }
-
-
-
-
-
-
-
-
- public String execCommand(String[] cmd, String workDir) {
- StringBuffer result = new StringBuffer();
- try {
-
- ProcessBuilder builder = new ProcessBuilder(cmd);
-
- if (workDir != null) {
- builder.directory(new File(workDir));
- }
-
- builder.redirectErrorStream(true);
-
- Process process = builder.start();
-
- InputStream is = process.getInputStream();
- BufferedReader br = new BufferedReader(new InputStreamReader(is));
- String line;
- while (null != (line = br.readLine())) {
- result.append(line);
- result.append("\n");
- }
- if (is != null) {
- is.close();
- br.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return result.toString();
- }
-
-
-
-
-
-
-
- public ArrayList<String> getPids(String execFilename) {
- ArrayList<String> pidArray = new ArrayList<String>();
- String result = execCommand(new String[] { "ps", "-l", execFilename },
- null);
- if (null != result && !"".endsWith(result)) {
- String[] resultArray = result.split("\n");
- int len = resultArray.length;
- try {
-
- for (int i = 1; i < len; i++) {
-
- pidArray.add(resultArray[i].trim().split("\\s+")[1]);
- }
- } catch (ArrayIndexOutOfBoundsException e) {
- }
- }
- return pidArray;
- }
- }
三、其他参考
1、linux权限详解
2、Linux命令大全(修改版).zip
3、Android执行shell命令
4、Google Code的wiki&issues
四、后记