Lecture 14 Android Application Development Notification Instructor Muhammad Owais muhammad.owais@riphah.edu.pk Cell: 03215500223
Notifications Used to notify user for events Three general forms of Notifications Toast Dialogs Status Bar Notifications
Toast Notifications Transitory messages that popup on the current window Automatically fade in & out of view No user interaction or response
Standard Toast Notifications Context context = getapplicationcontext(); CharSequence text = Message to Display"; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show();
Custom Toast Notifications <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toast_layout_root" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:background="#7777" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginright="10dp" android:src="@drawable/fire_eye_alien" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textcolor="#fff" android:text="i'm Watching You!" /> </LinearLayout>
Custom Toast Notifications public class NotificationToastActivity extends Activity { @Override public void oncreate(bundle savedinstancestate) { LayoutInflater inflater = getlayoutinflater(); View layout = inflater.inflate(r.layout.custom_toast, (ViewGroup)findViewById( R.id.toast_layout_root)); Toast toast = new Toast(getApplicationContext()); toast.setgravity(gravity.center_vertical, 0, 0); toast.setduration(toast.length_long); toast.setview(layout); toast.show(); }
Custom Toast Notifications Example
Status Bar Notifications
Notification Manager System service that manages Notifications e.g., notify & cancel
Adding Light Notification To use the user's default lighting notification.defaults = Notification.DEFAULT_LIGHTS; To use a different lighting notification.ledargb = 0xff00ff00; notification.ledonms = 300; notification.flags = Notification.FLAG_SHOW_LIGHTS;
Adding Sound and Vibration Notification To use the user's default sound notification.defaults = Notification.DEFAULT_SOUND; To use a different sound notification.sound = Uri.parse("file:///sdcard/media/audio/notifications/roo ster_2.mp3"); To use the user's default vibration pattern notification.defaults = Notification.DEFAULT_VIBRATE; To use a different vibration pattern long[] vibrate = { 0, 100, 200, 300 }; notification.vibrate = vibrate;
Remote View <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toast_layout_root" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#7777" android:padding="3dp" > <ImageView android:id="@+id/image" android:layout_width="44dp" android:layout_height="44dp" android:layout_marginright="10dp" android:contentdescription="pic" android:src="@drawable/notification" /> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:textcolor="#fff" android:textsize="24sp" /> </LinearLayout>
Creating Notifications public class MainActivity extends Activity { // Notification ID to allow for future updates private static final int MY_NOTIFICATION_ID = 1; // Notification count private int mnotificationcount; // Notification Text Elements private final CharSequence tickertext = "This is Really, Really, Super Long Notification Message"; private final CharSequence contenttitle = "Notification"; private final CharSequence contenttext = "You've Been Notified"; // Notification Action Elements private Intent mnotificationintent; private PendingIntent mcontentintent; // Notification Sound and Vibration on Arrival private Uri sounduri = Uri.parse("android.resource:// com.samd.notificationstatusbar/" + R.raw.alarm_rooster); private long[] mvibratepattern = {0,200,200,300}; RemoteViews mcontentviews = new RemoteViews( "com.samd.notificationstatusbar", R.layout.notificationview);
Creating Notifications protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mnotificationintent = new Intent(getApplicationContext(), NotificationActivity.class ); mcontentintent = PendingIntent.getActivity( getapplicationcontext(), 0, mnotificationintent, Intent.FLAG_ACTIVITY_NEW_TASK); final Button btn = (Button)findViewById(R.id.notify); btn.setonclicklistener(new OnClickListener() {
Creating Notifications public void onclick(view arg0) { mcontentviews.settextviewtext(r.id.text, contenttext + "(" + ++mnotificationcount + ")"); // Build the Notification Notification.Builder notificationbuilder = new Notification.Builder(getApplicationContext()).setTicker(tickerText).setSmallIcon(R.drawable.samllnotification).setAutoCancel(true).setContentIntent(mContentIntent).setSound(soundURI).setVibrate(mVibratePattern).setContent(mContentViews); // Pass the Notification to the NotificationManager NotificationManager mnotificationmanager = (NotificationManager) getsystemservice(context.notification_service); mnotificationmanager.notify(my_notification_id, notificationbuilder.build()); } });
Questions?