Android Things:擼起袖子來創建第一個Things工程


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 在CODE上查看代碼片派生到我的代碼片
  1. apply plugin: 'com.android.application'  
  2. android {  
  3.     ... ...   
  4. }  
  5. dependencies {  
  6.     compile fileTree(dir: 'libs', include: ['*.jar'])  
  7.     ... ...   
  8.     provided 'com.google.android.things:androidthings:0.1-devpreview'  
  9. }  

在你的清單文件中添加things共享庫條目:
ThingsDemo\app\src\main\AndroidManifest.xml

[html] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.           package="com.chengxiang.thingsdemo">  
  4.     <application  
  5.         android:allowBackup="true"  
  6.         android:icon="@mipmap/ic_launcher"  
  7.         android:label="@string/app_name"  
  8.         android:supportsRtl="true"  
  9.         android:theme="@style/AppTheme">  
  10.         <uses-library android:name="com.google.android.things"/>  
  11.         ... ...  
  12.     </application>  
  13. </manifest>  

5.聲明主Activity
一個想運行到嵌入式設備的應用,必須在清單文件中聲明一個Activity,作為系統啟動后的主入口。應用包含下面屬性的Intent Filger;
  Action:ACTION_MAIN
  Category:CATEGORY_DEFAUULT
  Category:IOT_LAUNCHER
ThingsDemo\app\src\main\AndroidManifest.xml

[html] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.           package="com.chengxiang.thingsdemo">  
  4.     <uses-library android:name="com.google.android.things"/>  
  5.   
  6.     <application  
  7.         ... ...  
  8.         android:theme="@style/AppTheme">  
  9.         <activity android:name=".MainActivity">  
  10.             <intent-filter>  
  11.                 <action android:name="android.intent.action.MAIN"/>  
  12.                 <category android:name="android.intent.category.LAUNCHER"/>  
  13.             </intent-filter>  
  14.   
  15.             <!-- Launch activity automatically on boot -->  
  16.             <intent-filter>  
  17.                 <action android:name="android.intent.action.MAIN"/>  
  18.                 <category android:name="android.intent.category.IOT_LAUNCHER"/>  
  19.                 <category android:name="android.intent.category.DEFAULT"/>  
  20.             </intent-filter>  
  21.         </activity>  
  22.     </application>  
  23. </manifest>  

6.編譯運行
這里我們嘗試使用gradle構建任務的方式編譯和運行項目,運行如下:

[plain] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. C:\Users\chengxiang.peng.QUNARSERVERS\AndroidThingsSources\ThingsDemo>gradle app:installDebug  
  2. Starting a Gradle Daemon, 1 incompatible and 2 stopped Daemons could not be reused, use --status for details  
  3. Incremental java compilation is an incubating feature.  
  4. 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.  
  5. :app:preBuild UP-TO-DATE  
  6. ... ...   
  7. :app:installDebug  
  8. Unable to get density for device iot_rpi3 - 7.0  
  9. Installing APK 'app-debug.apk' on 'iot_rpi3 - 7.0' for app:debug  
  10. Installed on 1 device.  
  11. BUILD SUCCESSFUL  
  12. Total time: 1 mins 31.038 secs  
[plain] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. C:\Users\chengxiang.peng.QUNARSERVERS\AndroidThingsSources\ThingsDemo>adb shell am start com.chengxiang.thingsdemo/.MainActivity  
  2. 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。(甭客氣!盡情的掃描或者長按!)

             


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM