AndroidLib库在.Net中的使用

来源:互联网 发布:iphone的照片导入mac 编辑:程序博客网 时间:2024/06/05 23:41

AndroidLib

AndroidLib是一个使用C#编写的开源Android控制库,是XDA论坛大神regaw_leinad基于adb开发的,下面介绍一下它的强大功能。

GitHub下载地址:https://github.com/regaw-leinad/AndroidLib
XDA论坛地址:http://forum.xda-developers.com/showthread.php?t=1512685
GitHub示例地址:https://github.com/regaw-leinad/AndroidLib-Samples-C-

简介

AndroidLib支持Android设备与PC端程序的通信,其中AndroidController类是ADB(Android Debug Bridge)的封装类,另外还有一些辅助的类,比如Device类,用于描述设备的硬件、电池状态、挂载点、root状态、busybox等信息,另外也支持设备重启、挂载文件系统、Push/Pull/Install文件等。前提是设备已经通过USB连接到PC。

使用方法

在.Net工程中添加AndroidLib.dll的引用即可开始使用。

要求

.Net 3.5或更高版本

测试

我们直接使用GitHub下载下来的示例进行测试。

下载示例,使用VS2010打开,可以看到下面两个工程:
这里写图片描述

SimpleButtonCheck的核心代码如下:

namespace SimpleButtonCheck{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        AndroidController android;        Device device;        private void Form1_Load(object sender, EventArgs e)        {            //Usually, you want to load this at startup, may take up to 5 seconds to initialize/set up resources/start server            android = AndroidController.Instance;        }        private void Button1_Click(object sender, EventArgs e)        {            string serial;            //Always call UpdateDeviceList() before using AndroidController on devices to get the most updated list            android.UpdateDeviceList();            if (android.HasConnectedDevices)            {                serial = android.ConnectedDevices[0];                device = android.GetConnectedDevice(serial);                this.TextBox1.Text = device.SerialNumber;            }            else            {                this.TextBox1.Text = "Error - No Devices Connected";            }        }        private void Form1_FormClosed(object sender, FormClosedEventArgs e)        {            //ALWAYS remember to call this when you're done with AndroidController.  It removes used resources            android.Dispose();         }    }}

可以看到,首先获取AndroidController的示例,AndroidController使用了单例模式。
之后,点击按钮,AndroidController会更新设备列表UpdateDeviceList(),然后判断是否存在已连接的手机,如果存在,则获取第一个手机设备的串号,并用这个串号创建一个Device对象,并在文本框显示设备的串号。
关闭窗口时,释放占用的非托管资源。

这个例子比较简单,我们可以很轻松地添加一些代码实现其他的功能,比如获取电量的功能:

this.TextBox1.Text = device.SerialNumber+","+device.Battery.Level+"%电量";

运行效果如下:

这里写图片描述

运行前,确保手机开启USB调试,以及屏幕解锁。

另外一个工程BuildPropDemo类似,只是使用Device.BuildProp获取build.prop信息。

核心代码如下:

        private void button1_Click(object sender, EventArgs e)        {            string serial;            android.UpdateDeviceList();            listBox1.Items.Clear();            label2.Text = "-null-";            if (android.HasConnectedDevices)            {                serial = android.ConnectedDevices[0];                device = android.GetConnectedDevice(serial);                //Adds all of the build.prop keys to the listbox                foreach (string key in device.BuildProp.Keys)                {                    listBox1.Items.Add(key);                }                // OR you could do                // listBox1.Items.AddRange(device.BuildProp.Keys.ToArray());                //So no items are selected right away                listBox1.SelectedIndex = -1;            }        }        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)        {            label2.Text = device.BuildProp.GetProp(listBox1.SelectedItem.ToString());        }

运行效果如下:
这里写图片描述

AndroidLib源码结构

如下图:
这里写图片描述

其他Android debug库

- MadBee

Nad Bee

其他

源码的分析,下次再写篇文章。

0 0
原创粉丝点击