Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Widgets & Notifications

MariusBudin
November 17, 2012

Android Widgets & Notifications

Widgets and Notifications presentation for Google Developer Group Barcelona.
Check out the hangout at: http://goo.gl/xohIg

The sample application: http://goo.gl/YTiIm

Take a look at the source code of the application on GitHub: http://goo.gl/OScRK

MariusBudin

November 17, 2012
Tweet

More Decks by MariusBudin

Other Decks in Programming

Transcript

  1. About Create a widget in 5 minutes Widgets 3.0 (stack,

    list, grid) Widgets Notifications About Instant notification Rich notifications 4.1 Notgets? Some code
  2. What is a widget? An app that works on your

    desktop? A fancy icon? A feature of an app?
  3. What is a widget? An app that works on your

    desktop? A fancy icon? A feature of an app? A shortcut?
  4. What is a widget? A quick, easy, pleasant and efficient

    access It’s on your desktop, information is one click away
  5. What is a widget? A quick, easy, pleasant and efficient

    access It’s on your desktop, information is one swipe away
  6. What is a widget? A quick, easy, pleasant and efficient

    access Visually attractive. you might use visually horrible apps but you would never put one one your desktop Press to rule the world
  7. What is a widget? A quick, easy, pleasant and efficient

    access No permanent activity behind.
  8. What is a widget? A quick, easy, pleasant and efficient

    access No permanent activity behind No views refreshing constantly*
  9. What is a widget? A quick, easy, pleasant and efficient

    access No permanent activity behind No views refreshing constantly* It actually lives on your desktop
  10. What is a widget? A quick, easy, pleasant and efficient

    access No permanent activity behind No views refreshing constantly* It actually lives on your desktop Remote views
  11. What is a RemoteView? A view hierarchy that can be

    display in another process UI managed by a BroadcastReceiver
  12. Limitations Vertical swipe and tap events Not all layouts /

    views allowed No animations* No own lifecycle
  13. Limitations Vertical swipe and tap events Not all layouts /

    views allowed Works with PendingIntents No animations* No own lifecycle
  14. When to use Information at a glance (weather, calendar, unread

    messages) Settings involving other apps or services (switch GPS, WiFi, BT)
  15. When to use Information at a glance (weather, calendar, unread

    messages) Settings involving other apps or services (switch GPS, WiFi, BT) Provide control for background apps (music player)
  16. When to use Information at a glance (weather, calendar, unread

    messages) Settings involving other apps or services (switch GPS, WiFi, BT) Provide control for background apps (music player) Custom shortcut
  17. When to use Information at a glance (weather, calendar, unread

    messages) Settings involving other apps or services (switch GPS, WiFi, BT) Provide control for background apps (music player) Custom shortcut really need a widget?
  18. Create a widget in 5 minutes 3.Create a new class

    extending AppWidgetProvider public class BasicWidgetProvider extends AppWidgetProvider{ @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds{ super.onUpdate(context, appWidgetManager, appWidgetIds ); //do something } }
  19. Create a widget in 5 minutes 3.Create a new class

    extending AppWidgetProvider public class BasicWidgetProvider extends AppWidgetProvider{ @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds{ super.onUpdate(context, appWidgetManager, appWidgetIds ); //do something } } onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds ) void onEnabled( Context context ) void onDeleted( Context context, int[] appWidgetIds ) void onDisabled( Context context ) void onReceive( Context context, Intent intent )
  20. Create a widget in 5 minutes 4.Create a layout for

    your widget <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id=”@+id/my_txt” android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:background="@drawable/widget_item_background" android:text="@string/empty_view_text" android:textSize="20sp" /> </LinearLayout>
  21. Create a widget in 5 minutes 5.Declare the appwidget in

    your Manifest <receiver android:name="ExampleAppWidgetProvider" > <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/example_appwidget_info" /> </receiver>
  22. Widgets 3.0 s s Create a StackView / ListView /

    GridView in widget’s XML Create a separate layout for the content Create a WidgetService Implement RemoteViewsService.RemoteViewsFactory will work like an adapter
  23. What is a notification? a message you can display to

    the user outside of your application's UI.
  24. Instant notification NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification")

    .setContentText("Hello World!"); Intent resultIntent = new Intent(this, ResultActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(ResultActivity.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT ); mBuilder.setContentIntent(resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(mId, mBuilder.build());
  25. Instant notification NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification")

    .setContentText("Hello World!"); Intent resultIntent = new Intent(this, ResultActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(ResultActivity.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT ); mBuilder.setContentIntent(resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(mId, mBuilder.build());
  26. What’s new? NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("Event tracker")

    .setContentText("Events received") NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); String[] events = new String[6]; // Sets a title for the Inbox style big view inboxStyle.SetBigContentTitle("Event tracker details:"); . . . // Moves events into the big view for (int i=0; i < events.length; i++) { inboxStyle.addLine(events[i]); } // Moves the big view style object into the notification object. mBuilder.setStyle(inBoxStyle); . . .
  27. What’s new? Notification noti = new Notification.Builder(this) .setContentTitle("11:30am, China room")

    .setContentText("Specs Meeting").setSmallIcon(R.drawable.icon) .setContentIntent(pIntent) .addAction(R.drawable.icon, "Snooze", pIntent).build(); . . .
  28. What’s new? Notification noti = new Notification.Builder(this) .setContentTitle("11:30am, China room")

    .setContentText("Specs Meeting").setSmallIcon(R.drawable.icon) .setContentIntent(pIntent) .addAction(R.drawable.icon, "Snooze", pIntent) .addAction(R.drawable.icon, "More", pIntent) .addAction(R.drawable.icon, "And more", pIntent).build(); . . .
  29. How to display a progress in notification bar? mNotifyManager =

    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mBuilder = new NotificationCompat.Builder(this); mBuilder.setContentTitle("Picture Download") .setContentText("Download in progress") .setSmallIcon(R.drawable.ic_notification); new Thread( new Runnable() { @Override public void run() { // do heavy stuff mBuilder.setProgress(100, incr, false); ... mNotifyManager.notify(0, mBuilder.build()); // update the notification mBuilder.setContentText("Download complete") // Removes the progress bar .setProgress(0,0,false); mNotifyManager.notify(ID, mBuilder.build()); } } ).start();
  30. DO

  31. Notgets ? A widget living in your notification bar A

    widget living in your notification bar Notification + Widget -> Notget
  32. Notgets ? A widget living in your notification bar A

    widget living in your notification bar Notification + Widget -> Notget