生活随笔
收集整理的這篇文章主要介紹了
Android VNC Server New
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?Android VNC Server New
關(guān)于VNC請參見維基百科:http://zh.wikipedia.org/wiki/VNC
? ? ? ???關(guān)于執(zhí)行Android VNC Server,請參見前一篇文章:點(diǎn)擊鏈接
一、VNC下載 1)fastdroid-vnc
? ? ? ???Android VNC Server開源項(xiàng)目
http://code.google.com/p/fastdroid-vnc/ 2)TightVNC 免費(fèi)的VNC軟件(這個都不需要填郵箱^^) http://www.tightvnc.com/download.php 二、程序執(zhí)行 這種方式看手機(jī)了==。Google HTC上倒是可以,詳細(xì)的說明看注釋了。 1)簡易UI 布局一點(diǎn)沒變動… 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權(quán)限:"?+?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/增加寫權(quán)限:"?+?result);???????????????????????????result?=?mShellUtil.rootCommand("touch?"?+?filePath);?????????????if?(LOGD)?????????????????Log.d(TAG,?"創(chuàng)建一個空文件:"?+?result);???????????????????????????result?=?mShellUtil.rootCommand("chmod?777?"?+?filePath);?????????????if?(LOGD)?????????????????Log.d(TAG,?"/data/local/設(shè)為777權(quán)限:"?+?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服務(wù)開啟狀態(tài):"?+?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(應(yīng)該加個單例==)
???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目錄下。
我是不是很無聊了-_-!
?
轉(zhuǎn)載于:https://blog.51cto.com/vaero/815264
總結(jié)
以上是生活随笔為你收集整理的Android VNC Server New的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。