2016-12-05 14:24:43 aoaoxiexie 阅读数 19005

可以通过如下ADB命令实现PC对Android手机的模拟输入,点击,滑动等事件,进而对Android手机的操作与控制。

1:adb  input keyevent <event_code> 

   这条命令模拟Android手机按下了event_code对应的按键。

   event_code表如下:

0 -->  "KEYCODE_UNKNOWN"
1 -->  "KEYCODE_MENU"
2 -->  "KEYCODE_SOFT_RIGHT"
3 -->  "KEYCODE_HOME"
4 -->  "KEYCODE_BACK"
5 -->  "KEYCODE_CALL"
6 -->  "KEYCODE_ENDCALL"
7 -->  "KEYCODE_0"
8 -->  "KEYCODE_1"
9 -->  "KEYCODE_2"
10 -->  "KEYCODE_3"
11 -->  "KEYCODE_4"
12 -->  "KEYCODE_5"
13 -->  "KEYCODE_6"
14 -->  "KEYCODE_7"
15 -->  "KEYCODE_8"
16 -->  "KEYCODE_9"
17 -->  "KEYCODE_STAR"
18 -->  "KEYCODE_POUND"
19 -->  "KEYCODE_DPAD_UP"
20 -->  "KEYCODE_DPAD_DOWN"
21 -->  "KEYCODE_DPAD_LEFT"
22 -->  "KEYCODE_DPAD_RIGHT"
23 -->  "KEYCODE_DPAD_CENTER"
24 -->  "KEYCODE_VOLUME_UP"
25 -->  "KEYCODE_VOLUME_DOWN"
26 -->  "KEYCODE_POWER"
27 -->  "KEYCODE_CAMERA"
28 -->  "KEYCODE_CLEAR"
29 -->  "KEYCODE_A"
30 -->  "KEYCODE_B"
31 -->  "KEYCODE_C"
32 -->  "KEYCODE_D"
33 -->  "KEYCODE_E"
34 -->  "KEYCODE_F"
35 -->  "KEYCODE_G"
36 -->  "KEYCODE_H"
37 -->  "KEYCODE_I"
38 -->  "KEYCODE_J"
39 -->  "KEYCODE_K"
40 -->  "KEYCODE_L"
41 -->  "KEYCODE_M"
42 -->  "KEYCODE_N"
43 -->  "KEYCODE_O"
44 -->  "KEYCODE_P"
45 -->  "KEYCODE_Q"
46 -->  "KEYCODE_R"
47 -->  "KEYCODE_S"
48 -->  "KEYCODE_T"
49 -->  "KEYCODE_U"
50 -->  "KEYCODE_V"
51 -->  "KEYCODE_W"
52 -->  "KEYCODE_X"
53 -->  "KEYCODE_Y"
54 -->  "KEYCODE_Z"
55 -->  "KEYCODE_COMMA"
56 -->  "KEYCODE_PERIOD"
57 -->  "KEYCODE_ALT_LEFT"
58 -->  "KEYCODE_ALT_RIGHT"
59 -->  "KEYCODE_SHIFT_LEFT"
60 -->  "KEYCODE_SHIFT_RIGHT"
61 -->  "KEYCODE_TAB"
62 -->  "KEYCODE_SPACE"
63 -->  "KEYCODE_SYM"
64 -->  "KEYCODE_EXPLORER"
65 -->  "KEYCODE_ENVELOPE"
66 -->  "KEYCODE_ENTER"
67 -->  "KEYCODE_DEL"
68 -->  "KEYCODE_GRAVE"
69 -->  "KEYCODE_MINUS"
70 -->  "KEYCODE_EQUALS"
71 -->  "KEYCODE_LEFT_BRACKET"
72 -->  "KEYCODE_RIGHT_BRACKET"
73 -->  "KEYCODE_BACKSLASH"
74 -->  "KEYCODE_SEMICOLON"
75 -->  "KEYCODE_APOSTROPHE"
76 -->  "KEYCODE_SLASH"
77 -->  "KEYCODE_AT"
78 -->  "KEYCODE_NUM"
79 -->  "KEYCODE_HEADSETHOOK"
80 -->  "KEYCODE_FOCUS"
81 -->  "KEYCODE_PLUS"
82 -->  "KEYCODE_MENU"
83 -->  "KEYCODE_NOTIFICATION"
84 -->  "KEYCODE_SEARCH"
85 -->  "TAG_LAST_KEYCODE"


2:adb shell input tap <X> <Y>

    这条命令模拟Android手机在屏幕坐标(X,Y)处进行了点击操作。



3:adb shell input swipe <X1> <Y1> <X2> <Y2>

    这条命令模拟Android手机从屏幕坐标(X1,Y1)滑动到坐标(X2,Y2)的操作。


注:如果PC要想同时控制多台Android手机,必须在adb 后面添加-s <android 设备序列号>

例如:adb -s 13b6e4c4 shell input tap 400 400

表示对13b6e4c4这台Android手机进行在屏幕上(400,400)坐标位置进行模拟的点击事件。

获取Android手机的设备序列号如下图所示



示例代码:

package com.hdzb.adb;

import java.io.IOException;

public class TestControlAndroid {

	public static void main(String[] args) throws InterruptedException {
		try {
			Thread.sleep(1000);
			//testInputStr();
			//testSwipe();
			//testTap();
			testTapForTwoAndroid();
			
		} catch (Exception e3) {
			e3.printStackTrace();
		}

	}
	/**
	 * 两台手机点击一下
	 */
	private static void testTapForTwoAndroid() {
		try {
			Runtime.getRuntime()
			.exec("D:\\android\\adt-bundle-windows-x86\\sdk\\platform-tools\\adb -s 13b6e4c4 shell input tap 400 400 ");
			Runtime.getRuntime()
			.exec("D:\\android\\adt-bundle-windows-x86\\sdk\\platform-tools\\adb -s 296ec3e2 shell input tap 400 400 ");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
        
	}
	/**
	 * 控制手机点击一下
	 */
	private static void testTap() {
		try {
			Runtime.getRuntime()
			.exec("D:\\android\\adt-bundle-windows-x86\\sdk\\platform-tools\\adb shell input tap 400 400 ");
			Thread.sleep(1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
        
	}
	/**
	 * 控制手机滑动
	 */
	private static void testSwipe() {
		try {
			Runtime.getRuntime()
			.exec("D:\\android\\adt-bundle-windows-x86\\sdk\\platform-tools\\adb shell input swipe 800 800 100 100");
			Thread.sleep(1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
        
	}
	/**
	 * 控制手机输入0-9
	 */
	private static void testInputStr() {
		try {
			Runtime.getRuntime()
					.exec("D:\\android\\adt-bundle-windows-x86\\sdk\\platform-tools\\adb shell input keyevent 7");
			Thread.sleep(1000);
			Runtime.getRuntime()
					.exec("D:\\android\\adt-bundle-windows-x86\\sdk\\platform-tools\\adb shell input keyevent 8");
			Thread.sleep(1000);
			Runtime.getRuntime()
					.exec("D:\\android\\adt-bundle-windows-x86\\sdk\\platform-tools\\adb shell input keyevent 9");
			Thread.sleep(1000);
			Runtime.getRuntime()
					.exec("D:\\android\\adt-bundle-windows-x86\\sdk\\platform-tools\\adb shell input keyevent 10");
			Thread.sleep(1000);
			Runtime.getRuntime()
					.exec("D:\\android\\adt-bundle-windows-x86\\sdk\\platform-tools\\adb shell input keyevent 11");
			Thread.sleep(1000);
			Runtime.getRuntime()
					.exec("D:\\android\\adt-bundle-windows-x86\\sdk\\platform-tools\\adb shell input keyevent 12");
			Thread.sleep(1000);
			Runtime.getRuntime()
					.exec("D:\\android\\adt-bundle-windows-x86\\sdk\\platform-tools\\adb shell input keyevent 13");
			Thread.sleep(1000);
			Runtime.getRuntime()
					.exec("D:\\android\\adt-bundle-windows-x86\\sdk\\platform-tools\\adb shell input keyevent 14");
			Thread.sleep(1000);
			Runtime.getRuntime()
					.exec("D:\\android\\adt-bundle-windows-x86\\sdk\\platform-tools\\adb shell input keyevent 15");
			Thread.sleep(1000);
			Runtime.getRuntime()
					.exec("D:\\android\\adt-bundle-windows-x86\\sdk\\platform-tools\\adb shell input keyevent 16");

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
}

 

2012-08-07 22:41:45 shuzui1985 阅读数 8641

1、VNC访问

a 、在android手机中安装vnc 的服务端:androidvncserver

http://code.google.com/p/android-vnc-server/

本文下载了已经编写好的bin执行文件

 

确保你的手机已经ROOT,

adb push /yourpath/androidvncserver  /sdcard

adb shell

cp /sdcard/androidvncserver /data/

cd /data

chmod 777 androidvncserver,给予该执行文件读写以及执行权限

/data/androidvncserver & 开启vncserver

 

b、在PC侧安装real vnc viewer客户端

下载地址:http://www.realvnc.com/products/free/4.1/index.html

 

PC端用VNC Viewer访问有两种方法:

         一个是如果PC和你的手机都是在同一个局域网内

          busybox ifconfig -a 查看你的手机的IPV4的地址

          然后输入 如192.168.1.100:5901,访问即可,192.168.1.100是你的手机ip

    

 

        另一个访问方式是最多的,就是PC和手机通过USB链接,此时需要通过adb forward映射端口来实现VNC访问

首先进行 adb forward tcp:5901 tcp:5901

紧接着:输入127.0.0.1:5901

以上两种的访问显示结果都是一样,如图所示:

 

后续总结及问题20120807:

1、本案用中兴 U960S实验效果很好(480*800像素),但是实验了中兴U970 和小米效果很不好,检查了一下参数小米是854x480像素,U970是540×960像素都超过了480*800像素;可能是像素太高导致远程效果不尽理想;

2、在通过WIFI PC访问手机,输入的是手机的IPV4地址,U970是android4.0版本,默认是IPV6,需要禁用IPV6启用IPV4才可以WIFI访问,当然了USB链接方法还是访问127.0.0.1不受IPV6的影响。

参考文档:

http://blog.csdn.net/qinlicang/article/details/6188163

http://paralife.blog.163.com/blog/static/1321363082010323113549162/

 

 

2011-06-09 17:25:00 zjujoe 阅读数 11151

因为手机的触屏有问题,需要通过pc来控制手机。

 

试用 vnc viewer, 没有成功。 而且它是基于 wifi 的.

 

然后不放弃,继续查找。 终于发现:androidscreencast, Desktop app to control an android device remotely

 

http://code.google.com/p/androidscreencast/

 

它可以通过adb接口远程控制手机!

 

还可以录像,访问文件系统。

 

不是很精致,但满足了我的需求。

2010-03-10 15:20:00 stevenliyong 阅读数 10327

 

I found a vnc server for G1.

 

1. Original vnc project

http://code.google.com/p/android-vnc/

 

This one could not be used on G1, because it need build a special keyboard driver into kernel

 

2. Forked vnc server

http://code.google.com/p/android-vnc-server/

Forked from android-vnc project on google code.

The original android-vnc need build a special keyboard driver into kernel. It's not necessary. Also the touch event support is added in this version.

 

First download and install the binary

 http://code.google.com/p/android-vnc-server/downloads/list

 

If you have adb on server:

#adb push androidvncserver /data 
#adb shell /data/androidvncserver
Or you can just copy the downloaded file androidvncserver to the /sdcard, 
Then umount the sdcard and launch the terminal from your G1, then type
# su    <-  I have root access, I'm not sure whether the following steps work without root access.
# cp /sdcard/androidvncserver /data/
# chmod a+x /data/androidvncserver
# /data/androidvncserver &
And finally: 

Run vnc viewer from hose PC.

Here I use

http://downloads.sourceforge.net/vnc...3.10-setup.exe 

 

make sure the connection address with port 5901 : 192.168.0.101:5901

 

 

Now, I can remote control my phone from host pc.

 

2015-07-22 18:53:37 qq446282412 阅读数 2705

我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(一)PC服务器端
(地址:http://blog.csdn.net/ouyang_peng/article/details/47004617)
 

我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(二)Android客户端功能展示
(地址:http://blog.csdn.net/ouyang_peng/article/details/47005739)


通过以上两篇文章,我们了解了Android实现用Android手机控制PC端的关机和重启的的大概功能,现在我们来实现Android客户端的代码。


首先来看看整个项目的结构,如下图所示:

                                   

第一步:扫描局域网内所有PC,看是否有PC端的服务器在运行并监听30000端口。

 

此界面的布局文件为:/res/layout/layout_scanip.xml

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <RelativeLayout
        android:id="@+id/ll"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:orientation="vertical" >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:orientation="vertical" >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/scaning" />
            <ProgressBar
                android:id="@+id/progressBar"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="100dp"
                android:layout_height="1dp" />
        </LinearLayout>
    </RelativeLayout>

</RelativeLayout>


扫描IP的Activity为com.oyp.shutdown.ScanActivity,代码如下:

 

 

 

 

package com.oyp.shutdown;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.widget.ProgressBar;
import android.widget.Toast;

public class ScanActivity extends Activity {
	private MyWifiManager myWifiManager = null;
	private String serverIP = "", resultIP = "";
	private ScanIPThread scanThread = null;
	private ProgressBar progressBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		int height = getWindow().getWindowManager().getDefaultDisplay()
				.getHeight();
		int width = getWindow().getWindowManager().getDefaultDisplay()
				.getWidth();
		setContentView(R.layout.layout_scanip);
		progressBar = (ProgressBar) findViewById(R.id.progressBar);
		myWifiManager = new MyWifiManager(ScanActivity.this);
		scanThread = new ScanIPThread();
		scanThread.start();
	}
	
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1000:
				Toast.makeText(ScanActivity.this, getString(R.string.find),
						Toast.LENGTH_SHORT).show(); // 找到可连接PC
				Intent controlIntent = new Intent(ScanActivity.this,
						ControlActivity.class);
				// 将可以连接的ip发过去
				controlIntent.putExtra("ip", (String) msg.obj);
				startActivity(controlIntent);
				finish();
				break;
			case 2000:
				Toast.makeText(ScanActivity.this, getString(R.string.notFind),
						Toast.LENGTH_SHORT).show(); // 没有找到可连接PC
				Intent reScanIntent = new Intent(ScanActivity.this,
						ReScanActivity.class);
				startActivity(reScanIntent);
				finish();
				break;
			default:
				progressBar.setMax(254);
				progressBar.setProgress(msg.what);
				break;
			}
			super.handleMessage(msg);
		}
	};
	

	// 扫描连接的WiFi所在网段开启了30000端口的C类ip
	// 例如,wifi的ip是192.168.1.1 则扫描 192.168.1.1-192.168.1.254
	class ScanIPThread extends Thread {
		@Override
		public void run() {
			serverIP = myWifiManager.getServerIp();
			int t = serverIP.lastIndexOf(".") + 1;
			resultIP = serverIP.substring(0, t);
			boolean flag = false;
			for (int i = 1; i < 255; i++) {
				try {
					Socket socket = new Socket();
					InetSocketAddress s = new InetSocketAddress(resultIP + i,
							30000);
					socket.connect(s, 50);
					Message message = new Message();
					message.what = 1000;
					message.obj = resultIP + i;
					handler.sendMessage(message);
					flag = true;
					socket.close();
					break;
				} catch (IOException e) {
					handler.sendEmptyMessage(i);
				}
			}
			if (!flag) {
				handler.sendEmptyMessage(2000);
			}
			super.run();
		}
	}
}


第二步:如果没有扫描到有PC端的服务器在运行并监听30000端口,则重新扫描或者退出。如上述代码中,当msg.what=2000时,重新进行扫描。

 

此界面的布局文件为:/res/layout/layout_rescan.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <Button
        android:id="@id/btnReScan"
        style="@style/StyleButton"
        android:text="@string/rescan" />

    <Button
        android:id="@id/btnClose"
        style="@style/StyleButtonClose"
        android:text="@string/close" />

</LinearLayout>

 

 

 

重新扫描IP的Activity为com.oyp.shutdown.ReScanActivity,代码如下:

 

package com.oyp.shutdown;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

public class ReScanActivity extends Activity implements OnClickListener {
	private Button btn_rescan, btn_close;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_LEFT_ICON);
		setContentView(R.layout.layout_rescan);
		getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
				R.drawable.acticon);

		btn_rescan = (Button) findViewById(R.id.btnReScan);
		btn_close = (Button) findViewById(R.id.btnClose);

		btn_rescan.setOnClickListener(this);
		btn_close.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnReScan:
			Intent intent = new Intent(ReScanActivity.this,ScanActivity.class);
			startActivity(intent);
			finish();
			break;
		case R.id.btnClose:
			finish();
			break;
		default:
			break;
		}
	}
}


第二步:扫描到了有PC端的服务器在运行并监听30000端口,则控制PC端关机、重启或者取消关机。如上述代码中,当msg.what=1000时,则打开控制PC端界面。

 

 

 

 

  •  

此界面的布局文件为:/res/layout/layout_control.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <Button
        android:id="@id/btnShutdown"
        style="@style/StyleButton"
        android:text="@string/shutdown" />

    <Button
        android:id="@id/btnReboot"
        style="@style/StyleButton"
        android:text="@string/reboot" />

    <Button
        android:id="@id/btnCancel"
        style="@style/StyleButton"
        android:text="@string/cancel" />

    <Button
        android:id="@id/btnClose"
        style="@style/StyleButtonClose"
        android:text="@string/close" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="6.0dip"
        android:text=" "
        android:textSize="12.0dip" />

</LinearLayout>


控制PC端的Activity为com.oyp.shutdown.ControlActivity,代码如下:

 

 

 

 

package com.oyp.shutdown;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

public class ControlActivity extends Activity implements OnClickListener {
	private Button btn_shutdown, btn_restart, btn_cancel, btn_close;
	private Socket clientSocket;// 客户端socket
	private DataOutputStream dataOutput = null;// 客户端发送数据
	private DataInputStream dataInput = null;// 客户端接收数据
	private String connIP = "";
	private ConnThread connThread = null;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Toast.makeText(ControlActivity.this, (String) msg.obj,
						Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_LEFT_ICON);
		setContentView(R.layout.layout_control);
		getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
				R.drawable.acticon);

		btn_shutdown = (Button) findViewById(R.id.btnShutdown);
		btn_restart = (Button) findViewById(R.id.btnReboot);
		btn_cancel = (Button) findViewById(R.id.btnCancel);
		btn_close = (Button) findViewById(R.id.btnClose);

		Intent intent = getIntent();
		connIP = intent.getStringExtra("ip");

		btn_shutdown.setOnClickListener(this);
		btn_restart.setOnClickListener(this);
		btn_cancel.setOnClickListener(this);
		btn_close.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// 连接服务器
		switch (v.getId()) {
		case R.id.btnShutdown:
			final String shutdown = "shutdown";
			if (connThread != null) {
				connThread.interrupt();
			}
			connThread = new ConnThread(connIP, 30000, shutdown);
			connThread.start();
			break;
		case R.id.btnReboot:
			final String reboot = "reboot";
			if (connThread != null) {
				connThread.interrupt();
			}
			connThread = new ConnThread(connIP, 30000, reboot);
			connThread.start();
			break;
		case R.id.btnCancel:
			final String cancel = "cancel";
			if (connThread != null) {
				connThread.interrupt();
			}
			connThread = new ConnThread(connIP, 30000, cancel);
			connThread.start();
			break;
		case R.id.btnClose:
			finish();
			break;
		default:
			break;
		}
	}

	class ConnThread extends Thread {
		private String ip;
		private int port;
		private String content;

		public ConnThread(String ip, int port, String content) {
			this.ip = ip;
			this.port = port;
			this.content = content;
		}

		@Override
		public void run() {
			try {
				clientSocket = new Socket(ip, port);
				while (true) {
					dataOutput = new DataOutputStream(
							clientSocket.getOutputStream());
					dataInput = new DataInputStream(
							clientSocket.getInputStream());
					String msg = "";
					if ((dataOutput != null) && (!content.equals(""))) {
						dataOutput.writeUTF(content);
					}
					msg = dataInput.readUTF();
					if (msg != null && !"".equals(msg)) {
						Message message = new Message();
						message.what = 1;
						message.obj = msg;
						handler.sendMessage(message);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					if (dataOutput != null) {
						dataOutput.close();
					}
					if (dataInput != null) {
						dataInput.close();
					}
					if (clientSocket != null) {
						clientSocket = null;
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			super.run();
		}
	}
}

 

其中MyWifiManager.java代码如下:

 

package com.oyp.shutdown;

import android.content.Context;
import android.net.DhcpInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

public class MyWifiManager {

	private WifiManager wifiManager;
	private WifiInfo wifiInfo;
	private DhcpInfo dhcpInfo;
	public MyWifiManager(Context context){
		wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		wifiInfo = wifiManager.getConnectionInfo();
		dhcpInfo = wifiManager.getDhcpInfo();
	}
	//得到本机ip
	public String getLocalIp(){
		return FormatString(dhcpInfo.ipAddress);
	}
	//得到服务器ip(热点ip)
	public String getServerIp(){
		return FormatString(dhcpInfo.serverAddress);
	}
	//转换ip格式为*.*.*.*
	public String FormatString(int value){
		String strValue="";
		byte[] ary = intToByteArray(value);
		for(int i=ary.length-1;i>=0;i--){
			strValue+=(ary[i]&0xFF);
			if(i>0){
				strValue+=".";
			}
		}
		return strValue;
	}
	public byte[] intToByteArray(int value){
		byte[] b=new byte[4];
		for(int i=0;i<4;i++){
			int offset = (b.length-1-i)*8;
			b[i]=(byte) ((value>>>offset)&0xFF);
		}
		return b;
	}
}

 

 

 

 

 

 

定义控件ID的文件为/res/values/id.xml

 

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item type="id" name="btnAirplane">false</item>
    <item type="id" name="btnShutdown">false</item>
    <item type="id" name="btnReScan">false</item>
    <item type="id" name="btnCancel">false</item>
    <item type="id" name="btnReboot">false</item>
    <item type="id" name="btnRecovery">false</item>
    <item type="id" name="btnBootloader">false</item>
    <item type="id" name="btnClose">false</item>
</resources>


/res/values/strings.xml

 

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">控制PC</string>
    <string name="shutdown">关机</string>
    <string name="rescan">重新扫描</string>
    <string name="reboot">重启</string>
    <string name="cancel">取消</string>
    <string name="please_wait">请稍等......</string>
    <string name="warning">错误</string>
    <string name="root">无法获得 Root 权限!</string>
    <string name="close">退出</string>
    <string name="ok">确定</string>
    <string name="scaning">正在扫描......</string>
    <string name="find">找到可连接PC.</string>
    <string name="notFind">没有找到可连接PC</string>
</resources>

/res/values/styles.xml

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="StyleButton">
        <item name="android:textSize">16.0dip</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textColor">#ffffffff</item>
        <item name="android:layout_gravity">center_horizontal</item>
        <item name="android:background">@drawable/btn_selector</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_marginLeft">30.0dip</item>
        <item name="android:layout_marginRight">30.0dip</item>
        <item name="android:layout_marginBottom">8.0dip</item>
    </style>
    <style name="StyleButtonClose">
        <item name="android:textSize">16.0dip</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textColor">#ffffffff</item>
        <item name="android:layout_gravity">center_horizontal</item>
        <item name="android:background">@drawable/btn_close_selector</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_marginLeft">30.0dip</item>
        <item name="android:layout_marginRight">30.0dip</item>
        <item name="android:layout_marginBottom">8.0dip</item>
    </style>
</resources>


项目描述文件:AndroidManifest.xml

 

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.oyp.shutdown"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <activity
            android:name=".ScanActivity"
            android:theme="@android:style/Theme.Dialog" >
             <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ReScanActivity"
            android:icon="@drawable/icon"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Dialog" >
        </activity>
        <activity
            android:name=".ControlActivity"
            android:icon="@drawable/icon"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Dialog" >
        </activity>
    </application>
</manifest>


代码可以在此处下载:Android实现用Android手机控制PC端的关机和重启的功能  地址:(http://download.csdn.net/detail/qq446282412/8923991)

 

 

 

我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(一)PC服务器端 (地址:http://blog.csdn.net/ouyang_peng/article/details/47004617)

我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(二)Android客户端功能展示 (地址:http://blog.csdn.net/ouyang_peng/article/details/47005739

我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(三)Android客 户端功能实现 (地址:http://blog.csdn.net/ouyang_peng/article/details/47007509)



                            ====================================================================================

  作者:欧阳鹏  欢迎转载,与人分享是进步的源泉!

  转载请保留原文地址http://blog.csdn.net/ouyang_peng

====================================================================================

 

 

 

 

没有更多推荐了,返回首页