1. 程式人生 > >樹莓派Android Things物聯網開發:建立一個Things專案

樹莓派Android Things物聯網開發:建立一個Things專案

Things apps use the same structure as those designed for phones and tablets. This similarity means you can modify your existing apps to also run on embedded things or create new apps based on what you already know about building apps for Android.

This lesson describes how to prepare your development environment for Android Things, and the required changes to enable app to run on embedded things.

Prerequisites

Before you begin building apps for Things, you must:

  • Create or update your app project In order to access new APIs for Things, you must create a project or modify an existing project that targets Android 8.0 (API level 26) or higher.

Add the library

Android Things devices expose APIs through support libraries that are not part of the Android SDK. To declare the Things Support Library dependency in your app:

  1. Add the dependency artifact to your app-level build.gradle file:

    dependencies {...
        provided 'com.google.android.things:androidthings:0.5.1-devpreview'}
  2. Add the Things shared library entry to your app's manifest file:

    <application ...><uses-libraryandroid:name="com.google.android.things"/>
        ... </application>
Note: Add the provided dependency to ensure that the build tools don't copy the shared library into the APK at compile time. Add <uses-library> to make this prebuilt library available to the app's classpath at run time. Together, these additions allow you to build against an on-device, shared library.

Declare a home activity

An application intending to run on an embedded device must declare an activity in its manifest as the main entry point after the device boots. Apply an intent filter containing the following attributes:

For ease of development, this same activity should include a CATEGORY_LAUNCHER intent filter so Android Studio can launch it as the default activity when deploying or debugging.

<applicationandroid:label="@string/app_name"><uses-libraryandroid:name="com.google.android.things"/><activityandroid:name=".HomeActivity"><!-- Launch activity as default from Android Studio --><intent-filter><actionandroid:name="android.intent.action.MAIN"/><categoryandroid:name="android.intent.category.LAUNCHER"/></intent-filter><!-- Launch activity automatically on boot --><intent-filter><actionandroid:name="android.intent.action.MAIN"/><categoryandroid:name="android.intent.category.IOT_LAUNCHER"/><categoryandroid:name="android.intent.category.DEFAULT"/></intent-filter></activity></application>