Created
July 16, 2014 15:09
-
-
Save makario/08476850ac87758bca2c to your computer and use it in GitHub Desktop.
Displaying custom notifications for Android Wear
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.makario.wearplay; | |
import android.app.Activity; | |
import android.app.Notification; | |
import android.app.NotificationManager; | |
import android.app.PendingIntent; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.text.Spannable; | |
import android.text.SpannableString; | |
import android.text.style.ForegroundColorSpan; | |
import android.text.style.RelativeSizeSpan; | |
public class NotificationTest extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
// First, create your custom notification activity as directed | |
// in the documentation. Create a PendingIntent for it. | |
Intent notificationIntent = new Intent(this, MainNotification.class); | |
PendingIntent notificationPendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, | |
PendingIntent.FLAG_UPDATE_CURRENT); | |
// Here's where the magic lies: Use various Spans to create | |
// custom peeking notifications. Notification.WearableExtender#setContentIcon | |
// will also allow you to set an icon on inside of the card. | |
String title = "Viola!"; | |
Spannable customTitle = new SpannableString(title); | |
customTitle.setSpan(new RelativeSizeSpan(1.8f), 0, title.length(), 0); | |
customTitle.setSpan(new ForegroundColorSpan(0xff83ae7b), 0, title.length(), 0); | |
Notification notificationBuilder = new Notification.Builder(this) | |
.setSmallIcon(R.drawable.ic_launcher) // you have to include this | |
.setContentTitle(customTitle) // you can do the same for #setContentText() | |
.extend(new Notification.WearableExtender() | |
.setDisplayIntent(notificationPendingIntent) | |
.setContentIcon(R.drawable.ic_launcher) // see also #setContentIconGravity() | |
.setHintHideIcon(true) // use this if you want to hide that small icon | |
.setCustomSizePreset(Notification.WearableExtender.SIZE_XSMALL)) | |
.build(); | |
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); | |
notificationManager.notify(0, notificationBuilder); | |
finish(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi!
I have two questions :