Android Wear Development-card notifications-first section: Adding Android Wear notification features

Source: Internet
Author: User
Tags set background

I. Preface description
Android Wear most of the display form is the form of cards, and the simplest way to support Android Wear is to use the notification **notification**. To achieve the simplest, non-highly customizable notifications, you only need to do some processing on the phone side, do not need to develop the watch end application, it can be said that the development cost is particularly low. This section focuses on simple wear feature card notifications.
Two. Environment Configuration
    1. The project cites the new support-v4.
    2. The following 3 classes are used primarily.

Import Android.support.v4.app.notificationcompat;import Android.support.v4.app.notificationmanagercompat;import Android.support.v4.app.NotificationCompat.WearableExtender;
three. General Notification bar

    1. Mobile : The normal notification bar on the phone should not be unfamiliar with the effect, here does not expand the description
    2. Watch : The effect of the watch is made up of 2 cards, the first is the mobile phone notification bar composition, the second is to click on the development of mobile phone application, the specific effect and the phone notification bar Click events Consistent, that is, if the notification bar does not set the Click event, then there will be no second card. In addition, the default background color is determined by the app icon, which is the primary color value.
Code Implementation
 Public voidsendclassicnotify () {Notification.builder Builder=NewNotification.builder ( This); //1. Setting Display informationBuilder.setcontenttitle ("title"); Builder.setcontenttext ("content");    Builder.setsmallicon (R.drawable.ic_launcher); //2. Set the click Jump event, if not set, the watch has no second cardIntent Intent=NewIntent ( This, Phoneactivity.class); Pendingintent pendingintent= Pendingintent.getactivity ( This,0, Intent,0);    Builder.setcontentintent (pendingintent); //3. Set other properties of the notification barBuilder.setautocancel (true);    Builder.setdefaults (Notification.default_all); Notificationmanager Manager=(Notificationmanager) Getsystemservice (Context.notification_service); Manager.notify (0, Builder.build ());}
problem

The above is the most original notification bar effect, does not carry on the watch end adaptation processing. It looks good, but what's the problem?
For example, the following effect, if the content is too long, it will be very difficult to see, this is only part of it can also continue to scroll.

Four. Add a notification bar for wear extended properties

Extended Properties
    • Multiple cards: As the second card of the first picture
    • Custom action buttons: as the second card of the first picture
    • Set background: As the background of the first picture
    • Stacking multiple cards: As the first card of the second picture
    • Voice Reply: Second card as second picture
Code Implementation
 Public voidsendwearnotify () {Notificationcompat.builder Builder=NewNotificationcompat.builder ( This); //1. Setting Display contentBuilder.setcontenttitle ("Message title"); Builder.setcontenttext ("Message Content"); //If only SmallIcon is set and no LargeIcon is set, the SmallIcon settings icon is displayed to the left of the notification bar, and if LargeIcon is set, the left side displays LargeIcon and the right side shows SmallIconBuilder.setsmallicon (R.drawable.ic_launcher); //if LargeIcon is set, the wear background becomes largeicon. //Builder.setlargeicon (Bitmapfactory.decoderesource (Getresources (),//r.drawable.default_background_sunny_day_bg)); //2. Set Jump PropertiesIntent Intent=NewIntent ( This, Phoneactivity.class); Pendingintent pendingintent= Pendingintent.getactivity ( This,0, Intent,0); //after setting the contentintent, the notification bar has a click effect, and wear slide to the far right, there is a more open on phone pagebuilder.setcontentintent (pendingintent); //3. Setting Notification PropertiesBuilder.setautocancel (true);    Builder.setdefaults (Notification.default_all); //4. Set watch-specific propertiesbuilder.extend (Extendwear (builder)); Mnotificationmanager.notify (1, Builder.build ());}

Next, the above code 4th (4. Setting the unique properties of the Watch) is extended.

0. Method structure
Private notificationcompat.wearableextender extendwear (        notificationcompat.builder Builder) {    new  Notificationcompat.wearableextender ();     // TODO: Specific property setting code    return

The next feature points are inserted behind the TODO.

1. Setting the background
Bitmapfactory.options Options =Newbitmapfactory.options (); //Official documentation Tips:http://developer.android.com/training/wearables/notifications/creating.html     /** * * note:the bitmap that you use with setbackground () should has a * resolution of 400x400 for non-scrolling Backgrounds and 640x400 for * backgrounds this support parallax scrolling.     Place these bitmap * images in the res/drawable-nodpi directory of your handheld app. * Place and Non-bitmap resources for wearable notifications, such as * those used with the Setcontenticon () method,     In the res/drawable-hdpi * Directory of your Handheld app. */    //scrollable, background is 640x400, otherwise 400x400. //If background is set, the LargeIcon fails at the wear endOptions.outwidth=640; Options.outheight= -; Bitmap Wearbgbitmap=Bitmapfactory.decoderesource (Getresources (), r.drawable.wallpaper_1, options); //when BG is set, the LargeIcon fails.Wearableextender.setbackground (wearbgbitmap); Picture storage location: Universal icon on Res/hdpi directory, wear unique images placed in res/nodpi background image size: Scrollable background is 640x400, non-scrollable size 400x400 (px). Attribute conflicts: Using the SetBackground method, At the same time set the Builder.setlargeicon method, then the mobile phone display is Setlargeicon, and the watch is displayed for the SetBackground. 2. Add a unique action button//2. Add a unique action to the wearIntent Intent=NewIntent ( This, Phoneactivity.class); Pendingintent pendingintent= Pendingintent.getactivity ( This,0, Intent,0); Notificationcompat.action Action=Newnotificationcompat.action (r.drawable.action,"the new action", pendingintent); Wearableextender.addaction (action);
3. Adjust the home page icon
 //  3. Adjust the icon on the home page  //  hide default app icon   true     );  //  Set the icon followed by ContentText   Wearableextender.setcontenticon (r.drawable.mycolors);  //  only start and end tags are supported, default is end   wearableextender.setcontenticongravity (gravity.start);  
    • When set to True, the app icon in the upper-right corner of the Sethinthideicon is hidden.
    • Gravity can only set the start and end tags, which correspond to left, which corresponds to right.

      I have previously done RTL (right-to-left language) adaptation, such as Arabic, Persian, etc., is right-to-left language, to the entire interface to the left to the right alignment, and Android 4.2 provides the start and end tags, used to automatically match the alignment, in the Ltr (left to right) language, Start→left,end→right, and in RTL (right-to-left) languages, Start→right,end→left. We do not expand here to talk about, then have time to upload notes to the blog, interested students can then refer to the next.

4. Add a second page card
    // 4. Add the second page      of the card new  notificationcompat.builder (            this). Setcontenttitle ("page 2")            . Setcontenttext ("  Now display the wear notification content, test can show how much content, try, hahaha. Continue to a ")            . Build ();    Wearableextender.addpage (secondpagenotification);

On the stack of multiple cards and voice reply content, because less use, the author did not continue to study.
If the reader is interested, can be on the developer's website to learn, in fact, it is not difficult.
Stacking Multiple card addresses
Voice Reply Address

The above is the author in the simple card adaptation summary, more property use, you can read the Samples for SDK directory of the Wear/notifications project, this sample in the notification bar basically all the functions are involved.

Android Wear Development-card notifications-first section: Adding Android Wear notification features

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.