Getting Started with Android Wear-How to create a mobile phone to associate with a wearable device (Notification)

Source: Internet
Author: User

Create a notification

To create notifications that can be displayed in both the phone and wearables, you can use notificationcompat.builder. Notifications created by this class will be processed to see if the notification appears on the phone or on the wearable device.

Import the necessary class libraries

Before development, you need to import the following class libraries first

Importandroid.support.v4.app.notificationcompat;importandroid.support.v4.app.notificationmanagercompat; Importandroid.support.v4.app.NotificationCompat.WearableExtender;


Create a notification using Notification Builder

the V4 Support Library Library allows developers to use the latest notification features, such as action button or large icons, which require more than 4 of the compilation environment.

Using a notification that supports library development, you need to create an Notificationcompat.builder instance and publish the notification using notify (), as shown in the following code:

int Notificationid = 001;//Build Intent for notification contentintent viewintent = new Intent (THIS,VIEWEVENTACTIVITY.CLA SS); Viewintent.putextra (extra_event_id, eventId); Pendingintent viewpendingintent =        pendingintent.getactivity (this,0, viewintent,0); notificationcompat.buildernotificationbuilder=        New Notificationcompat.builder (This)        . Setsmallicon ( r.drawable.ic_event)        . Setcontenttitle (eventtitle).        setcontenttext (eventlocation).        setcontentintent (viewpendingintent);//Get An instance of the Notificationmanager Servicenotificationmanagercompat Notificationmanager =        Notificationmanagercompat.from (this);//Build the notification and issues it with notification Manager.notificationManager.notify (Notificationid,notificationbuilder.build ());


When this notification appears on the phone, the user can specify that the Pendingintent trigger the notification by using the setcontentintent () method, and when the notification appears on the phone, the user will swipe left to destroy the notification that appears.

Add an action Button

except defined by setcontentintent () The main content of the behavior that you can pass through a pendingintent to the addaction () method to add Additional actions .

For example, the following code shows the same type of notification as the above code, but adds an action to View the top.

Build an intent-a action to view a mapintent mapintent = new Intent (Intent.action_view); Uri Geouri = Uri.parse ("geo:0,0?q=" + uri.encode (location)); Mapintent.setdata (Geouri); Pendingintent mappendingintent =        pendingintent.getactivity (this, 0, mapintent, 0); Notificationcompat.builder Notificationbuilder =        new Notificationcompat.builder (This)        . Setsmallicon ( r.drawable.ic_event)        . Setcontenttitle (eventtitle).        setcontenttext (eventlocation).        setcontentintent (viewpendingintent)        . Addaction (R.drawable.ic_map,                getString (R.string.map), mappendingintent);


On the phone, the action is attached as a button to the notification, and in the wearable device, the action appears as a large button, and when the user clicks the button, the contents of the intent are picked up in the phone.

Specifies the behavior that is unique to the wearable device

If you want the wearable device to behave differently from your phone, use wearableextender.addaction () . When using this method, the wearable device will no longer show use notificationcompat.builder.addaction () , which is to show only that it is in a wearable device.

//Create an intent for the reply actionintent Actionintent = new Intent (this, actionactivity.class); Pendingintent actionpendingintent = pendingintent.getactivity (this, 0, actionintent, PENDINGINTENT.F lag_update_current);//Create the actionnotificationcompat.action Action = new NotificationCompat.Action.Builder (R. Drawable.ic_action, GetString (R.string.label, Actionpendingintent)). build ();//Build the No Tification and add the action via wearableextendernotification notification = new Notificationcompat.builder (mconte                XT). Setsmallicon (R.drawable.ic_message). Setcontenttitle (GetString (R.string.title))                . Setcontenttext (GetString (r.string.content)). Extend (New Wearableextender (). Addaction (Action)) . build (); 


Add Big View

Developers can insert a big view of the content into the notification, on the phone, the user can expand the notification to view the big view, on the wearable device, the big view is visible by default.

Add this expandable content as a notification, using the SetStyle () method in the Notificationcompat.builder object to pass to the instance Bigtextstyle or Inboxstyle style.

For example, the following code adds a Bigtextstyle to the notification.

Specify the ' Big view ' content to display the long//event description, the not fit the normal content text. Bigtextstyle Bigstyle = new Notificationcompat.bigtextstyle (); Bigstyle.bigtext (eventdescription); Notificationcompat.builder Notificationbuilder =        new Notificationcompat.builder (This)        . Setsmallicon ( r.drawable.ic_event)        . Setlargeicon (Bitmapfractory.decoderesource (                getresources (), r.drawable.notif_ Background)        . Setcontenttitle (eventtitle).        setcontenttext (eventlocation).        Setcontentintent ( viewpendingintent)        . Addaction (R.drawable.ic_map,                getString (R.string.map), mappendingintent)        . SetStyle (Bigstyle);


To add wearables features to notifications

If you want to add features specific to wearables, such as voice input, you can use the Notificationcompat.wearableextender class, which is divided into the following steps.

1 Creating Wearableextender instances to set features specific to wearable devices

2 Create a notificationcompat.builder instance, set as described earlier

3 Call the Extend () method, passed to Wearableextender

4 Call Build () to create a notification

The following code removes the icon from the notification card using Sethinthideicon ()

Create a wearableextender to add functionality for Wearablesnotificationcompat.wearableextender Wearableextender =
   new Notificationcompat.wearableextender ()        . Sethinthideicon (TRUE);//Create a notificationcompat.builder to Build a standard notification//then extend it with the wearableextendernotification Notif = new Notificationcompat.builde R (Mcontext)        . Setcontenttitle ("New mail from" + sender)        . Setcontenttext (subject).        Setsmallicon ( R.drawable.new_mail);        . Extend (Wearableextender)        . Build ();


If you want to read the value of a specific attribute later, you can see the following code

Notificationcompat.wearableextender Wearableextender =        

Delivery Notifications

Use the Notificationmanagercompat API instead of Notificationmanagerwhen you need to deliver notifications:

Get an instance of the Notificationmanager servicenotificationmanagercompat Notificationmanager =        Notificationmanagercompat.from (Mcontext);//Issue the notification with notification Manager.notificationManager.notify (Notificationid, Notif);


When using Notificationmanager, some notificationcompat.wearableextender features do not work

notificationcompat.wearableextender Wearableextender = new Notificationcompat.wearableextender (Notif); Boolean Hinthideicon = Wearableextender.gethinthi Deicon (); 


Related Article

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.