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

來源:互聯網
上載者:User

標籤:puts   iss   pos   manage   roi   htm   main   detail   ati   

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
  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
  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
  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
  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
  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。(甭客氣!盡情的掃描或者長按!)

            

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.