【转载请注明出处: http://blog.csdn.net/leytton/article/details/77854144】
《树莓派Android Things物联网开发》系列文章专栏 http://blog.csdn.net/column/details/17741.html
树莓派Android Things物联网开发:入门及资料汇总
树莓派Android Things物联网开发:创建一个Things项目
树莓派Android Things物联网开发:树莓派GPIO引脚图
树莓派Android Things物联网开发:按键中断及消抖
树莓派Android Things物联网开发:已知Bug发行说明
树莓派Android Things物联网开发:GitHub案例程序汇总
Android Things应用程序结构与Android手机或平板电脑相同。这意味着你可以修改现有的App直接在Android Things中运行,或者你可以基于之前掌握的Android技术来开Things应用。
本课程介绍了Android Things开发环境准备工作,以及一些必要的修改,以使应用在Android Things中运行。
准备工作
在开始构建Things应用程序的之前,你必须:
- 更新SDK工具到25.0.3或更高版本 更新版本的SDK工具,可以帮您构建和测试应用应用。
- 更新SDK到Android 8.0(API 26)或更高版本 更新版本的SDK平台提供新的Things应用API。
- 创建或更新您的应用项目, 为了在新的Things应用API,创建的项目或者已有项目的SDK版本必须升级到Android 8.0(API26)或更高。
添加依赖库
Android Things的API不包含在Android SDK中。我们要声明Things的依赖库:
在build.gradle文件中添加依赖库:
dependencies { ... provided 'com.google.android.things:androidthings:0.5.1-devpreview' }
在AndroidManifest.xml添加共享库:
<application ...> <uses-library android:name="com.google.android.things"/> ... </application>
声明入口Activity
想要应用程序在Android Things嵌入式设备中开机启动,那么就必须在其AndroidManifest.xml文件中声明作为主入口点,并且添加intent-filter包含以下属性:
- Action: ACTION_MAIN
- Category: CATEGORY_DEFAULT
- Category: IOT_LAUNCHER
为了便于开发,这个Activity也应该包括 CATEGORY_LAUNCHER 这个intent-filter,Android Studio在部署或调试时会启动它作为默认入口。
<application
android:label="@string/app_name">
<uses-library android:name="com.google.android.things"/>
<activity android:name=".HomeActivity">
<!-- Android Studio 默认启动的Activity -->
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<!-- 开机后自动启动的Activity -->
<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>
PS:如果本文对您有帮助,请点个赞让我知道哦~
原文阅读需要科学上网
摘自https://developer.android.com/things/training/first-device/create-studio-project.html
Create an Android Things Project
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:
- Update your SDK tools to version 25.0.3 or higher The updated SDK tools enable you to build and test apps for Things.
- Update your SDK with Android 8.0 (API 26) or higher The updated platform version provides new APIs for Things apps.
- 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:
Add the dependency artifact to your app-level
build.gradle
file:dependencies { ... provided 'com.google.android.things:androidthings:0.5.1-devpreview' }
Add the Things shared library entry to your app's manifest file:
<application ...> <uses-library android:name="com.google.android.things"/> ... </application>
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:
- Action: ACTION_MAIN
- Category: CATEGORY_DEFAULT
- Category:
IOT_LAUNCHER
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.
<application
android:label="@string/app_name">
<uses-library android:name="com.google.android.things"/>
<activity android:name=".HomeActivity">
<!-- Launch activity as default from Android Studio -->
<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>