Android VNC Server New
关于执行Android VNC Server,请参见前一篇文章:点击链接
一、
VNC
下载
1
)fastdroid-vnc
Android VNC Server开源项目
2
)TightVNC
免费的VNC软件(这个都不需要填邮箱^^)
二、程序执行
这种方式看手机了==。Google HTC上倒是可以,详细的说明看注释了。
1
)简易UI
布局一点没变动…
VNCServerNewActivity.java
- public class VNCServerNewActivity extends Activity {
-
- private static final String TAG = "VNCServer";
- private static final boolean LOGD = true;
-
-
- private static final String VNC_SERV_NAME = "fastdroid-vnc";
-
-
- private ShellUtil mShellUtil;
-
-
- private static final int DLG_BASE = 0;
-
- private static final int DLG_ROOT_FAILED = DLG_BASE + 1;
-
- private static final int DLG_EXEC_FAILED = DLG_BASE + 2;
-
-
- private static final String VNC_SERV_PORT = "5901";
-
-
- private Button startBtn, stopBtn;
-
- private TextView statusView, connectView;
-
-
- private static final String[] PS_VNC_SERVER = new String[] { "ps", "-l",
- VNC_SERV_NAME };
-
- private ArrayList<String> pidList;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- mShellUtil = ShellUtil.getInstance();
-
- initApp();
- initViews();
- }
-
-
- private void initViews() {
- startBtn = (Button) findViewById(R.id.startBtn);
- stopBtn = (Button) findViewById(R.id.stopBtn);
- statusView = (TextView) findViewById(R.id.statusView);
- connectView = (TextView) findViewById(R.id.connectView);
- updateViews(isServerOn());
- }
-
-
- private void initApp() {
- boolean result = mShellUtil.root();
- if (LOGD)
- Log.d(TAG, "获取Root权限:" + result);
- if (result) {
- copyVNCServer();
- } else {
- showDialog(DLG_ROOT_FAILED);
- }
- }
-
-
- private void copyVNCServer() {
- String filePath = "/data/local/" + VNC_SERV_NAME;
- File file = new File(filePath);
-
-
- if (!file.exists()) {
-
- boolean result = mShellUtil.rootCommand("chmod a+x /data/local/");
- if (LOGD)
- Log.d(TAG, "/data/local/增加写权限:" + result);
-
-
- result = mShellUtil.rootCommand("touch " + filePath);
- if (LOGD)
- Log.d(TAG, "创建一个空文件:" + result);
-
-
- result = mShellUtil.rootCommand("chmod 777 " + filePath);
- if (LOGD)
- Log.d(TAG, "/data/local/设为777权限:" + result);
-
- if (result) {
- try {
-
- InputStream is = getAssets().open(VNC_SERV_NAME);
- FileOutputStream fos = new FileOutputStream(file);
- byte[] buffer = new byte[4096];
- int count = 0;
- while ((count = is.read(buffer)) > 0) {
- fos.write(buffer, 0, count);
- }
- fos.close();
- is.close();
- if (LOGD)
- Log.d(TAG, VNC_SERV_NAME + "文件写入/data/local/!");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- } else {
- if (LOGD)
- Log.d(TAG, VNC_SERV_NAME + "文件已存在/data/目录下!");
- }
- }
-
-
- public void startBtn(View v) {
-
- String cmd = "/data/local/" + VNC_SERV_NAME + " &";
- boolean result = mShellUtil.rootCommand(cmd);
- if (LOGD)
- Log.d(TAG, cmd + ":" + result);
- if (isServerOn()) {
- updateViews(true);
- } else {
-
-
-
-
- showDialog(DLG_EXEC_FAILED);
- }
- }
-
-
- public void stopBtn(View v) {
- boolean result;
-
- while (isServerOn()) {
- for (String pid : pidList) {
- result = mShellUtil.rootCommand("kill " + pid);
- if (LOGD)
- Log.d(TAG, "kill " + pid + ":" + result);
- }
- }
- updateViews(false);
- }
-
- @Override
- protected void onDestroy() {
- super.onDestroy();
- boolean result = mShellUtil.rootRelease();
- if (LOGD)
- Log.d(TAG, "释放占用资源:" + result);
- }
-
-
- private void updateViews(boolean isServerOn) {
-
- startBtn.setEnabled(!isServerOn);
- stopBtn.setEnabled(isServerOn);
-
- if (isServerOn) {
- statusView.setText(R.string.status_run);
- connectView.setText(getLocalIpAddress() + ":" + VNC_SERV_PORT);
- } else {
- statusView.setText(R.string.status_stop);
- connectView.setText(R.string.address);
- }
- }
-
-
- private boolean isServerOn() {
- mShellUtil.setFilter(new PsLineFilter());
-
- pidList = mShellUtil.execCommand(PS_VNC_SERVER, null, true);
- mShellUtil.resetFilter();
- boolean result = (null != pidList) && (pidList.size() >= 1);
- if (LOGD)
- Log.d(TAG, "VNC服务开启状态:" + result);
- return result;
- }
-
-
- private 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;
- }
-
- @Override
- protected Dialog onCreateDialog(int id) {
- 省略……
- }
-
- }
2
)Shell
工具类
ShellUtil.java
-
- public final class ShellUtil {
-
-
- static class ShellUtilHolder {
- static ShellUtil instance = new ShellUtil();
- }
-
-
- public static ShellUtil getInstance() {
- return ShellUtilHolder.instance;
- }
-
-
- private Process process;
-
-
- private DataOutputStream dos;
-
-
- private IStdoutFilter<String> mIStdoutFilter;
-
-
- public void setFilter(IStdoutFilter<String> filter) {
- this.mIStdoutFilter = filter;
- }
-
-
- public void resetFilter() {
- this.mIStdoutFilter = null;
- }
-
-
-
-
-
-
-
-
- public boolean root() {
- try {
-
- process = Runtime.getRuntime().exec("su");
-
- dos = new DataOutputStream(process.getOutputStream());
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- return true;
- }
-
-
-
-
-
-
-
- public boolean rootCommand(String cmd) {
- if (null != dos) {
- try {
- dos.writeBytes(cmd + "\n");
- dos.flush();
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- return true;
- }
- return false;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public boolean rootRelease() {
- try {
- dos.writeBytes("exit\n");
- dos.flush();
- process.waitFor();
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- } finally {
- try {
- if (null != process) {
- process.destroy();
- }
- if (null != dos) {
- dos.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return true;
- }
-
-
-
-
-
-
-
-
-
- public ArrayList<String> execCommand(String[] cmd, String workDir,
- boolean isStdout) {
- ArrayList<String> lineArray = null;
- try {
-
- ProcessBuilder builder = new ProcessBuilder(cmd);
-
- if (workDir != null) {
- builder.directory(new File(workDir));
- }
-
- builder.redirectErrorStream(true);
-
- Process process = builder.start();
-
-
- if (isStdout) {
- lineArray = new ArrayList<String>();
- handleStdout(lineArray, process);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return lineArray;
- }
-
-
-
-
-
-
- private void handleStdout(ArrayList<String> lineArray, Process process)
- throws IOException {
- InputStream is = process.getInputStream();
- if (null != mIStdoutFilter) {
-
- if (mIStdoutFilter instanceof AbstractLineFilter) {
-
- BufferedReader br = new BufferedReader(
- new InputStreamReader(is));
- String line;
- while (null != (line = br.readLine())) {
-
- if (!mIStdoutFilter.filter(line)) {
- lineArray.add(mIStdoutFilter.handle());
- }
- }
- if (br != null) {
- br.close();
- }
- } else {
-
- lineArray.add(inputStream2Str(is));
- }
- } else {
-
- lineArray.add(inputStream2Str(is));
- }
- if (is != null) {
- is.close();
- }
- }
-
-
-
-
-
-
- public String inputStream2Str(InputStream is) throws IOException {
- StringBuffer out = new StringBuffer();
- byte[] b = new byte[4096];
- for (int n; (n = is.read(b)) != -1;) {
- out.append(new String(b, 0, n));
- }
- return out.toString();
- }
-
- }
3
)过滤器
IStdoutFilter.java
-
- public interface IStdoutFilter<T> {
-
-
-
-
-
-
-
- boolean filter(T stdout);
-
-
-
-
-
- T handle();
-
- }
AbstractLineFilter.java
-
-
-
-
- public abstract class AbstractLineFilter implements IStdoutFilter<String> {
-
-
- protected String line;
-
-
-
-
-
-
-
- protected abstract boolean lineFilter(String line);
-
- @Override
- public boolean filter(String stdout) {
- this.line = stdout;
- return lineFilter(stdout);
- }
-
- @Override
- public String handle() {
- return line;
- }
-
- }
PsLineFilter.java(应该加个单例==)
-
-
-
- public final class PsLineFilter extends AbstractLineFilter {
-
- @Override
- protected boolean lineFilter(String line) {
-
- if (null == line || "".endsWith(line) || line.startsWith("USER")) {
- return true;
- }
- return false;
- }
-
- @Override
- public String handle() {
- try {
- return line.trim().split("\\s+")[1];
- } catch (Exception e) {
- return line;
- }
- }
-
- }
三、后记
这个东西貌似还得动手改源码么T^T。小弟告罄了,不想碰那个东西==。
附件工程,随便看看了……
ps:Doxygen简单生成了个chm帮助文件,在docs目录下。
![]()
我是不是很无聊了-_-!