package com.losileeya.wifiadb;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
/**
* User: Losileeya (847457332@qq.com)
* Date: 2016-08-20
* Time: 09:19
* 类描述:adb命令对手机执行相关操作(必须先root)
*
* @version :
*/
public class AdbUtil {
//是否已经root
private static boolean mHaveRoot = false;
private static Context context;
private final String TAG = this.getClass().getSimpleName();
//是否已连接
public static boolean isConn = false;
public AdbUtil(Context context) {
this.context = context;
}
/**
* 打开adb连接
*
* @param tv
*/
public static void openAdb(TextView tv) {
if (!isConn) {
tv.setText(" adb connect" + getLocalIpAddress());
execShell("setprop service.adb.tcp.port 5555");// Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c", "setprop service.adb.tcp.port 5555"});
try {
Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "start adbd"}); // execShell("start adbd");
} catch (IOException e) {
e.printStackTrace();
}
isConn = true;
}
}
/**
* 断开adb连接
*/
public static void closeAdb() {
try {
Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "stop adbd"});
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @return 获取局域网的ip地址形式(32位整型IP地址转成本地ip)
*/
private static String getLocalIpAddress() {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
// 获取32位整型IP地址
int ipAddress = wifiInfo.getIpAddress();
// 返回整型地址转换成“*.*.*.*”地址
return String.format("%d.%d.%d.%d", (ipAddress & 0xff),
(ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff),
(ipAddress >> 24 & 0xff));
}
/**
* 在su文件中写入命令
*
* @param str
*/
public static void execShell(String str) {
try {
// 权限设置
Process p = Runtime.getRuntime().exec("su");
// 获取输出流
OutputStream outputStream = p.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(
outputStream);
// 将命令写入
dataOutputStream.writeBytes(str);
// 提交命令
dataOutputStream.flush();
// 关闭流操作
dataOutputStream.close();
outputStream.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
/**
* @return 获取ip地址
*/
public static String getIpAddress() {
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()
&& inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// 判断机器Android是否已经root,即是否获取root权限
public boolean haveRoot() {
if (!mHaveRoot) {
int ret = execRootCmdCilent("echo test"); // 通过执行测试命令来检测
if (ret != -1) {
Toast.makeText(context, "已经root", Toast.LENGTH_LONG).show();
mHaveRoot = true;
} else {
Toast.makeText(context, "not root", Toast.LENGTH_LONG).show();
Log.i(TAG, "not root!");
}
} else {
Toast.makeText(context, "已经root", Toast.LENGTH_LONG).show();
Log.i(TAG, "mHaveRoot = true, have root!");
}
return mHaveRoot;
}
// 执行命令但不关注结果输出
public int execRootCmdCilent(String cmd) {
int result = -1;
DataOutputStream dos = null;
try {
Process p = Runtime.getRuntime().exec("su");
dos = new DataOutputStream(p.getOutputStream());
Log.i(TAG, cmd);
dos.writeBytes(cmd + "\n");
dos.flush();
dos.writeBytes("exit\n");
dos.flush();
p.waitFor();
result = p.exitValue();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
* 卸载apk
*/
public static void uninstallApk(String packageName) {
try {
Process p= Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "pm uninstall " + packageName});
if (p == null) {
Toast.makeText(context, "app 卸载失败", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "app 已成功卸载", Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 重启adb
*/
public static void restartAdb() {
try {
Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "adb kill-server"});
Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "adb start-server"});
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 重启手机
*/
public static void rebootPhone() {
try {
Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "reboot"});
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 关闭手机
*/
public static void closePhone() {
try {
Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "reboot -p"});
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 杀死app
*
* @param packageName 包名
*/
public static void killApp(String packageName) {
try {
Process p=Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", " am force-stop " + packageName});
if (p == null) {
Toast.makeText(context, "app 关闭失败", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "app 关闭成功", Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
e.printStackTrace();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
自己动手做一个adb的wifi连接
共58个文件
xml:32个
png:8个
java:5个
5星 · 超过95%的资源 需积分: 2 217 下载量 20 浏览量
2016-08-21
12:23:51
上传
评论 4
收藏 127KB RAR 举报
温馨提示
自己动手做一个adb的wifi连接,当你忘记带数据线的时候可以连接wifi调试,关机和重启也很方便
资源推荐
资源详情
资源评论
收起资源包目录
WifiAdb.rar (58个子文件)
WifiAdb
.gitignore 105B
app
.gitignore 8B
build.gradle 847B
src
androidTest
java
com
losileeya
wifiadb
ApplicationTest.java 364B
main
AndroidManifest.xml 941B
res
mipmap-xhdpi
ic_launcher.png 5KB
mipmap-mdpi
ic_launcher.png 2KB
layout
activity_connect.xml 2KB
activity_main.xml 10KB
values-w820dp
dimens.xml 364B
mipmap-xxhdpi
ic_launcher.png 8KB
drawable
card_foreground_selector.xml 569B
wifiadb.png 51KB
adbmiss.png 12KB
back.png 286B
mipmap-hdpi
ic_launcher.png 3KB
mipmap-xxxhdpi
ic_launcher.png 10KB
values
strings.xml 73B
colors.xml 214B
dimens.xml 216B
styles.xml 394B
java
com
losileeya
wifiadb
MainActivity.java 4KB
AdbUtil.java 11KB
ConnectActivity.java 2KB
test
java
com
losileeya
wifiadb
ExampleUnitTest.java 328B
libs
proguard-rules.pro 662B
app.iml 12KB
settings.gradle 16B
build.gradle 587B
build
generated
gradlew 5KB
WifiAdb.iml 938B
gradlew.bat 2KB
gradle.properties 872B
.idea
misc.xml 2KB
encodings.xml 159B
libraries
support_vector_drawable_24_2_0.xml 640B
appcompat_v7_24_2_0.xml 761B
support_v4_24_2_0.xml 420B
support_annotations_24_2_0.xml 495B
support_media_compat_24_2_0.xml 973B
butterknife_annotations_8_0_1.xml 591B
support_fragment_24_2_0.xml 945B
cardview_v7_24_2_0.xml 580B
support_compat_24_2_0.xml 931B
junit_4_12.xml 477B
butterknife_8_0_1.xml 608B
support_core_ui_24_2_0.xml 938B
support_core_utils_24_2_0.xml 777B
hamcrest_core_1_3.xml 526B
animated_vector_drawable_24_2_0.xml 645B
workspace.xml 155KB
gradle.xml 821B
copyright
profiles_settings.xml 76B
runConfigurations.xml 564B
compiler.xml 686B
.name 7B
modules.xml 351B
local.properties 428B
共 58 条
- 1
zy9011
- 粉丝: 488
- 资源: 38
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页