android Wearable-Creating Wearable Apps and Creating Custom Layouts

>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.
 If you need to show persistent content on versions prior to Android 5.1, create a notification in the context stream instead.

 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:5601
android手持设备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:

  1. In the Configure your Project window, enter a name for your app and a package name.
  2. 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.
  3. In the first Add an Activity window, add a blank activity for mobile.
  4. In the second Add an Activity window, add a blank activity for Wear.
 Note:  The  wear  module also contains a "Hello World" activity that uses a  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:

BoxInsetLayout
FrameLayout object that's aware of screen shape and can box its children in the center square of a round screen.
CardFragment
A fragment that presents content within an expandable, vertically scrollable card.
CircledImageView
An image view surrounded by a circle.
ConfirmationActivity
An activity that displays confirmation animations after the user completes an action.
CrossFadeDrawable
A drawable that contains two child drawables and provides methods to directly adjust the blend between the two.
DelayedConfirmationView
A view that provides a circular countdown timer, typically used to automatically confirm an operation after a short delay has elapsed.
DismissOverlayView
A view for implementing long-press-to-dismiss.
GridViewPager
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.
GridPagerAdapter
An adapter that supplies pages to a  GridViewPager object.
FragmentGridPagerAdapter
An implementation of a  GridPagerAdapter instance that represents each page as a fragment.
DotsPageIndicator
A page indicator for a  GridViewPager implementation that identifies the current page in relation to all available pages on the current row.
WatchViewStub
A class that can inflate a specific layout, depending on the shape of the device's screen.
WearableListView
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.

 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 WearableActivityclass, 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:

  1. Prepare the alarm manager.
  2. Set the frequency of the updates.
  3. Schedule the next update when the activity switches to ambient mode or is currently in ambient mode.
  4. 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" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值