Android Wear development-how to create a Notification and androidwear associated with wearable devices

Source: Internet
Author: User

Android Wear development-how to create a Notification and androidwear associated with wearable devices

Create notification

Icationicationcompat. Builder can be used to create notifications that can be displayed on both mobile phones and wearable devices. If a notification is created using this type, the system determines whether the notification is displayed on a mobile phone or on a wearable device.

 

Import necessary Class Libraries

Before development, import the following class libraries.

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 allows developers to use the latest notification features, such as action button or large icons. The compilation environment must be above 4.

To use notifications that support library development, you need to create icationicationcompat. Builder instance and use notify () to publish notifications, as shown in the following code:

int notificationId = 001;// Build intent for notification contentIntent viewIntent = new Intent(this,ViewEventActivity.class);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());


If the notification appears on the mobile phone, you can specify PendingIntent to trigger the notification by using setContentIntent (). When the notification appears on the mobile phone, why do users Slide left to destroy the notifications.

 

 

Add Action Button

Apart from defining the behavior of setcontentintent () main content, you can add other actions by passing a PendingIntent to the addaction () method.

For example, the following code shows notifications of the same type as the above Code, but adds an action on The view.

// Build an intent for an 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 mobile phone, this action is appended to the notification in the form of a button. In the wearable device, this action appears in the form of a large button. When you click this button, the intent content will be called in the phone.

 

Actions unique to wearable devices

If you want the behavior of a wearable device to be different from that of a mobile phone, use WearableExtender. addAction (). when this method is used, the use of icationicationcompat is no longer displayed on the wearable device. builder. addAction (). is displayed only on the wearable device.

// Create an intent for the reply actionIntent actionIntent = new Intent(this, ActionActivity.class);PendingIntent actionPendingIntent =        PendingIntent.getActivity(this, 0, actionIntent,                PendingIntent.FLAG_UPDATE_CURRENT);// Create the actionNotificationCompat.Action action =        new NotificationCompat.Action.Builder(R.drawable.ic_action,                getString(R.string.label, actionPendingIntent))                .build();// Build the notification and add the action via WearableExtenderNotification notification =        new NotificationCompat.Builder(mContext)                .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 to the notification page. on mobile phones, users can expand the notification to View the Big View. On wearable devices, the Big View is visible by default.

Add this expandable content as a notification and use the setStyle () method in icationicationcompat. Builder object to pass it to the BigTextStyle or InboxStyle style of the instance.

For example, the following code adds BigTextStyle to the notification.

// Specify the 'big view' content to display the long// event description that may 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);


Add wearable device features for notifications

You can use the icationicationcompat. WearableExtender class to add special characteristics of wearable devices, such as voice input.

1. Create a WearableExtender instance and set the unique features of the wearable device.

2. Create icationicationcompat. Builder instance and set it as described above

3. Call the extend () method and pass it to WearableExtender.

4 call build () to create a notification

The following code uses setHintHideIcon () to remove the icon from the notification card

// 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.Builder(mContext)        .setContentTitle("New mail from " + sender)        .setContentText(subject)        .setSmallIcon(R.drawable.new_mail);        .extend(wearableExtender)        .build();


If you want to read the unique feature values, you can use the following code:

NotificationCompat.WearableExtender wearableExtender =        new NotificationCompat.WearableExtender(notif);boolean hintHideIcon = wearableExtender.getHintHideIcon(); 

Send notification

To send notifications, replace icationicationmanager with the icationmanagercompat API:

// Get an instance of the NotificationManager serviceNotificationManagerCompat notificationManager =        NotificationManagerCompat.from(mContext);// Issue the notification with notification manager.notificationManager.notify(notificationId, notif);


When icationicationmanager is used, some icationicationcompat. WearableExtender features cannot work.

NotificationCompat.WearableExtender wearableExtender =        new NotificationCompat.WearableExtender(notif);boolean hintHideIcon = wearableExtender.getHintHideIcon();



How to Set Notification time in Android Development

Record the Notification content and other information when the time is set. When the time is reached, send the Notification
PendingIntent notiPend = PendingIntent. getActivity (context, 0, intent, PendingIntent. FLAG_CANCEL_CURRENT );
Notification = new Notification (android. R. drawable. stat_policy_sync, policyscroll, System. currentTimeMillis ());
Notification. setLatestEventInfo (context, policytitle, policycontent, notiPend );
Required imgr. Policy (policyid, notification );

Q: What are the differences between processes, tasks, and services in the android system?

Processes are a general term, including third-party applications, system applications, and underlying system modules. The task is the third-party application process you have installed. Services are system module processes.

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.