Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Sample code for a blog post about the Android Wear Developer Preview.
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.preview.support.wearable.notifications.WearableNotifications;
import android.support.v4.app.NotificationCompat;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import com.google.common.collect.Lists;
import com.inch.android.sandbox.R;
import java.util.List;
public class WearableActivity extends BaseActivity {
private static final int NOTIFICATION_ID = 255;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final FrameLayout root = new FrameLayout(this);
setContentView(root);
final Button button = new Button(this);
button.setText("Notify!");
button.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER));
button.setTextSize(40);
button.setGravity(Gravity.CENTER_HORIZONTAL);
button.setPadding(100, 20, 100, 20);
root.addView(button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final List<Card> fakeCards = Card.getFakeCards();
final List<Notification> notifications = Lists.newArrayList();
final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(WearableActivity.this)
.setSmallIcon(R.drawable.inch_logo)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.caller_picture))
.setContentTitle("Incoming call")
.setContentText("Gyula Voros");
for (Card fakeCard : fakeCards) {
final NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.setBigContentTitle(fakeCard.title)
.bigText(fakeCard.description);
notifications.add(new NotificationCompat.Builder(WearableActivity.this)
.setStyle(bigTextStyle)
.build()
);
}
Notification twoPageNotification =
new WearableNotifications.Builder(notificationBuilder)
.addPages(notifications)
.build();
((NotificationManager) WearableActivity.this.getSystemService(Context.NOTIFICATION_SERVICE)).notify(NOTIFICATION_ID, twoPageNotification);
}
});
}
public static final class Card {
public String title;
public String description;
public Card(String title, String description) {
this.title = title;
this.description = description;
}
public static List<Card> getFakeCards() {
final List<Card> fakeCards = Lists.newArrayList();
fakeCards.add(new Card("Re: What about the email cards?", "Today at 4:20 PM You could easily show multiple cards, just create a multi-page notification."));
fakeCards.add(new Card("Meeting at CMI", "Yesterday at 10:00 AM\n Weekly kickoff meeting."));
fakeCards.add(new Card("Message", "Yesterday at 9:32 AM\n I'll be late."));
fakeCards.add(new Card("Missed call", "Yesterday at 9:30 AM"));
return fakeCards;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment