Android Things:撸起袖子来创建第一个Things工程

mac2022-06-30  24

http://blog.csdn.net/p106786860/article/details/60161020

——————————————————————————————————————————————————————————————————————————————————————

在前面的章节,我们利用官方的sample-simplepio项目的blink模块,给大家演示了如何导入和运行现成的Android Things工程。那么这个章节,我们来手把手的给大家演示,如何从零开始创建、开发和运行一个Android Things项目。1.前期准备在你开始创建你的Android Things项目之前,你必须完成以下事情:  更新你的SDK工具为24或者更高版本,更新的SDK工具可以让你能构建和测试Things应用。  更新你的SDK为Android 7.0(API 24)或者更高,更新的平台版本为Things应用提供了新的API。Android Studio->Tools->Android->SDK Manager,如下图:2.创建项目前面我们已经看到了Android Things项目的开发环境、开发工具、项目结构和Android项目都是一致的,其实它就是一个Android项目。那么我们新创建一个Andoird项目ThingsDemo。注意:创建和更新你的应用项目,为了访问Things新的API,你必须创建一个项目或者修改一个已存在的项目,它的目标为Android7.0(API 24)或者更高;4.添加库Android Things设备通过support library公开API,它并不是Android SDK的一部分。在你的app中声明Things Support Library依赖。在你的应用级别的build.gradle文件中添加依赖映射:ThingsDemo\app\build.gradle

[plain] view plain copy apply plugin: 'com.android.application'  android {      ... ...   }  dependencies {      compile fileTree(dir: 'libs', include: ['*.jar'])      ... ...       provided 'com.google.android.things:androidthings:0.1-devpreview'  }  

在你的清单文件中添加things共享库条目:ThingsDemo\app\src\main\AndroidManifest.xml

[html] view plain copy <?xml version="1.0" encoding="utf-8"?>  <manifest xmlns:android="http://schemas.android.com/apk/res/android"            package="com.chengxiang.thingsdemo">      <application          android:allowBackup="true"          android:icon="@mipmap/ic_launcher"          android:label="@string/app_name"          android:supportsRtl="true"          android:theme="@style/AppTheme">          <uses-library android:name="com.google.android.things"/>          ... ...      </application>  </manifest>  

5.声明主Activity一个想运行到嵌入式设备的应用,必须在清单文件中声明一个Activity,作为系统启动后的主入口。应用包含下面属性的Intent Filger;  Action:ACTION_MAIN  Category:CATEGORY_DEFAUULT  Category:IOT_LAUNCHERThingsDemo\app\src\main\AndroidManifest.xml

[html] view plain copy <?xml version="1.0" encoding="utf-8"?>  <manifest xmlns:android="http://schemas.android.com/apk/res/android"            package="com.chengxiang.thingsdemo">      <uses-library android:name="com.google.android.things"/>        <application          ... ...          android:theme="@style/AppTheme">          <activity android:name=".MainActivity">              <intent-filter>                  <action android:name="android.intent.action.MAIN"/>                  <category android:name="android.intent.category.LAUNCHER"/>              </intent-filter>                <!-- Launch activity automatically on boot -->              <intent-filter>                  <action android:name="android.intent.action.MAIN"/>                  <category android:name="android.intent.category.IOT_LAUNCHER"/>                  <category android:name="android.intent.category.DEFAULT"/>              </intent-filter>          </activity>      </application>  </manifest>  

6.编译运行这里我们尝试使用gradle构建任务的方式编译和运行项目,运行如下:

[plain] view plain copy C:\Users\chengxiang.peng.QUNARSERVERS\AndroidThingsSources\ThingsDemo>gradle app:installDebug  Starting a Gradle Daemon, 1 incompatible and 2 stopped Daemons could not be reused, use --status for details  Incremental java compilation is an incubating feature.  The TaskInputs.source(Object) method has been deprecated and is scheduled to be removed in Gradle 4.0. Please use TaskInputs.file(Object).skipWhenEmpty() instead.  :app:preBuild UP-TO-DATE  ... ...   :app:installDebug  Unable to get density for device iot_rpi3 - 7.0  Installing APK 'app-debug.apk' on 'iot_rpi3 - 7.0' for app:debug  Installed on 1 device.  BUILD SUCCESSFUL  Total time: 1 mins 31.038 secs   [plain] view plain copy C:\Users\chengxiang.peng.QUNARSERVERS\AndroidThingsSources\ThingsDemo>adb shell am start com.chengxiang.thingsdemo/.MainActivity  Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.chengxiang.thingsdemo/.MainActivity }  

提示1:运行adb shell am start com.chengxiang.thingsdemo/.MainActivity命令的时候,报错error:unknown host service分析1:因为运行adb命名需要使用5037端口,应该是某个程序占用的该端口,故报错。处理1:找到占用端口的程序,并杀死。1.新技术,新未来!欢迎大家关注“1024工场”微信服务号,时刻关注我们的最新的技术讯息。2.抛弃各种找元器件的烦恼,来“1024工场”微店,一次性买到你所想要的。3.加入“Android Things开发”QQ讨论群,一起学习一起Hi。(甭客气!尽情的扫描或者长按!)

             

转载于:https://www.cnblogs.com/cuizhf/p/6681377.html

最新回复(0)