>Creating Wearable Apps:
These are the main differences between handheld and wearable apps:
- Wearable apps are relatively small in size and functionality compared to handheld apps. They contain only what makes sense on the wearable, which is usually a small subset of the corresponding handheld app. In general, you should carry out operations on the handheld when possible and send the results to the wearable.
- Users don't download apps directly onto the wearable. Instead, you bundle the wearable app inside the handheld app. When users install the handheld app, the system automatically installs the wearable app. However, for development purposes, you can still install the wearable app directly to the wearable.
-
Wearable apps can access much of the standard Android APIs, but don't support the following APIs:
> The following describes the two modes of operation for always-on apps:
-
Interactive
- Use full color with fluid animation in this mode. The app is also responsive to input. Ambient
- Render the screen with grayscale graphics and do not present any input cues in this mode. This display mode is only supported on devices running Android 5.1 or higher.
Note: We recommend using Android Studio for Android Wear development, as it provides project setup, library inclusion, and packaging conveniences. The rest of this training assumes you're using Android Studio.
> Wearable apps run directly on the wearable device, giving you access to low-level hardware such as sensors, activities, services, and more, right on the wearable.
Before you begin building wearable apps, you must:
- Update your SDK tools to version 23.0.0 or higher
The updated SDK tools enable you to build and test wearable apps. - Update your SDK with Android 4.4W.2 (API 20) or higher
The updated platform version provides new APIs for wearable apps.
adb -d forward tcp:5601 tcp:5601android手持设备AVD的端口号:5037为adb默认端口
Note: If you can not connect your wearable to your machine via USB, you can try connecting over Bluetooth.
>As you follow the wizard, enter the following information:
- In the Configure your Project window, enter a name for your app and a package name.
- In the Form Factors window:
- Select Phone and Tablet and select API 9: Android 2.3 (Gingerbread) under Minimum SDK.
- Select Wear and select API 20: Android 4.4 (KitKat Wear) under Minimum SDK.
- In the first Add an Activity window, add a blank activity for mobile.
- In the second Add an Activity window, add a blank activity for Wear.
WatchViewStub
. This class inflates a layout based on whether the device's screen is round or square. The
WatchViewStub
class is one of the UI widgets that the
wearable support library
provides.
The Android v4 support library (or v13, which includes v4) contains the APIs to extend your existing notifications on handhelds to support wearables.
> Creating Custom Layouts
You should create custom layouts only when necessary. Read the design guidelines for information on how to design great wearable apps.
Note: When the notification is peeking on the homescreen, the system displays it with a standard template that it generates from the notification's semantic data. This template works well on all watchfaces. When users swipe the notification up, they'll then see the custom activity for the notification.
> Here are some of the major classes in the Wearable UI Library:
-
A
FrameLayout
object that's aware of screen shape and can box its children in the center square of a round screen. - A fragment that presents content within an expandable, vertically scrollable card.
- An image view surrounded by a circle.
- An activity that displays confirmation animations after the user completes an action.
- A drawable that contains two child drawables and provides methods to directly adjust the blend between the two.
- A view that provides a circular countdown timer, typically used to automatically confirm an operation after a short delay has elapsed.
- A view for implementing long-press-to-dismiss.
-
A layout manager that allows the user to navigate both vertically and horizontally through pages of data. You supply an implementation of a
GridPagerAdapter
instance to generate the pages that the view shows. -
An adapter that supplies pages to a
GridViewPager
object. -
An implementation of a
GridPagerAdapter
instance that represents each page as a fragment. -
A page indicator for a
GridViewPager
implementation that identifies the current page in relation to all available pages on the current row. - A class that can inflate a specific layout, depending on the shape of the device's screen.
-
An alternative version of a
ListView
object that is optimized for ease of use on small screen wearable devices. It displays a vertically scrollable list of items, and automatically snaps to the nearest item when the user stops scrolling.
BoxInsetLayout
CardFragment
CircledImageView
ConfirmationActivity
CrossFadeDrawable
DelayedConfirmationView
DismissOverlayView
GridViewPager
GridPagerAdapter
FragmentGridPagerAdapter
DotsPageIndicator
WatchViewStub
WearableListView
Android Wear apps can control what’s displayed on the wearable device screen while the device is in a low-power ambient mode. Wear apps that run in both ambient and interactive mode are called always-onapps.
After you complete the project configuration, extend the WearableActivity
class, which provides all the methods you need to enable ambient mode in your app.
To realize battery savings, updates should be no more than once every 10 seconds. In practice, however, you should update your app less frequently than that.
》 For apps that require more frequent updates, such as a fitness, time-keeping, and travel information apps, use anAlarmManager
object to wake the processor and update the screen more frequently.
To implement an alarm that updates content more frequently in ambient mode, follow these steps:
- Prepare the alarm manager.
- Set the frequency of the updates.
- Schedule the next update when the activity switches to ambient mode or is currently in ambient mode.
- Cancel the alarm when the activity switches to interactive mode or the activity is stopped
Note: The alarm manager may create new instances of your activity as they are triggered. To prevent this situation, ensure that your activity is declared with the android:launchMode="singleInstance"
parameter in the manifest.
private AlarmManager mAmbientStateAlarmManager; private PendingIntent mAmbientStatePendingIntent; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setAmbientEnabled(); mAmbientStateAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent ambientStateIntent = new Intent(getApplicationContext(), MainActivity.class); mAmbientStatePendingIntent = PendingIntent.getActivity( getApplicationContext(), 0, ambientStateIntent, PendingIntent.FLAG_UPDATE_CURRENT); ... }
@Override public void onDestroy() { mAmbientStateAlarmManager.cancel(mAmbientStatePendingIntent); super.onDestroy(); }
When the device switches to interactive mode, cancel the alarm in the onExitAmbient()
method:
@Override public void onExitAmbient() { super.onExitAmbient(); mAmbientStateAlarmManager.cancel(mAmbientStatePendingIntent); }
If your app should not be installed or updated on devices with Android versions prior to 5.1, update your manifest with the following:
<uses-library android:name="com.google.android.wearable" android:required="true" />