Sharing Knowledge

« Hide the Status and... | Main | Android Font »
Friday Jun 25, 2010

Android Notification

Notification atau pemberitahuan digunakan untuk memperingatkan user tentang peristiwa yang terjadi dalam aplikasi. Ada beberapa macam notification, namun yang akan kita bahas sekarang adalah Toast notification dan status bar notification.


1. Toast Notification
adalah pemberitahuan yang berupa pesan pendek di sebuah layar popup dengan durasi singkat. Untuk menampilkan notifikasi toast ini pertama kita harus instansiasi object Toast dengan fungsi makeText(). Fungsi ini mengambil 3 parameter yaitu aplikasi konteks, pesan text, dan durasi notifikasi. Kemudian akhiri dengan fungsi show() agar pesan text bisa tampil. Letakkan kode di dalam sebuah method onCLick untuk mengecek jika button toast telah di klik.

public void onClick(View view) {

        // Check if the Toast button has been clicked

        if (view == findViewById(R.id.toastbutton)) {

            // display the toast popup window

   Toast.makeText(this, "This is a sample Toast Notification message", Toast.LENGTH_LONG).show();

        }

    }

 



Gambar 1 Toast Notification


2. Status Bar Notification

Status Bar notification adalah pemberitahuan yang ditampilkan di status bar. Untuk menampilkan status bar notification ini kita membutuhkan NotificationManager. Lihat contoh code dibawah ini :

 private void GeneratNotification(){

  myNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

  CharSequence NotificationTicket = "*** Notification";

  CharSequence NotificationTitle = "Attention Please!";

  CharSequence NotificationContent = "- Notification is coming -";

  long when = System.currentTimeMillis();

 Notification notification =

new Notification(android.R.drawable.btn_star_big_on,NotificationTicket, when);

  Context context = getApplicationContext();

  Intent notificationIntent = new Intent(this,Main.class);

  PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

  notification.setLatestEventInfo(context, NotificationTitle,NotificationContent, contentIntent);

  myNotificationManager.notify(NOTIFICATION_ID, notification);
}

 


Gambar 2 Status Bar Notification




netoya_meruvian

Comments:

Post a Comment:
  • HTML Syntax: Allowed