Show Notification in StatusBar
What you will learn: You will learn how to show Notifications in the StatusBar.
Difficulty: 2 of 5

What it will look like:

Description:
Description follows tomorrow... 

The full source:
"/res/layout/main.xml"
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:id="@+id/btn_showsample"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Show sample Notification." />
- <Button
- android:id="@+id/btn_clear"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Clear sample Notification." />
- </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
"/src/your_package_structure/StatusbarNotificator.java"
Using java Syntax Highlighting
- package org.anddev.android.statusbarnotificator;
- import android.app.Activity;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- public class StatusbarNotificator extends Activity {
- private NotificationManager mNotificationManager;
- private int YOURAPP_NOTIFICATION_ID;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- setContentView(R.layout.main);
- // Get the notification manager service.
- mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- /* Show a sample notification when the first button was clicked. */
- findViewById(R.id.btn_showsample).setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- StatusbarNotificator.this.showNotification(
- R.drawable.tinyandroid,
- R.string.notif_short,
- R.string.notif_detailed,
- false);
- }
- });
- /* Clear the notification. */
- findViewById(R.id.btn_clear).setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- StatusbarNotificator.this.mNotificationManager.cancel(YOURAPP_NOTIFICATION_ID);
- }
- });
- }
- private void showNotification(int statusBarIconID, int statusBarTextID, int detailedTextID, boolean showIconOnly) {
- // This is who should be launched if the user selects our notification.
- Intent contentIntent = new Intent(this, StatusbarNotificator.class);
- // This is who should be launched if the user selects the app icon in the notification.
- Intent appIntent = new Intent(android.content.Intent.VIEW_ACTION,
- Uri.parse("http://www.anddev.org"));
- // choose the ticker text
- String tickerText = showIconOnly ? null : this.getString(statusBarTextID);
- mNotificationManager.notify(
- YOURAPP_NOTIFICATION_ID, // we use a string id because it is a unique
- // number. we use it later to cancel the
- // notification
- new Notification(
- this, // our context
- statusBarIconID, // the icon for the status bar
- tickerText, // the text to display in the ticker
- System.currentTimeMillis(), // the timestamp for the notification
- "anddev.org - Notification", // the title for the notification
- getText(detailedTextID), // the details to display in the notification
- contentIntent, // the contentIntent (see above)
- R.drawable.icon, // the app icon
- "anddev", // the name of the app
- appIntent)); // the appIntent (see above)
- }
- }
Parsed in 0.042 seconds, using GeSHi 1.0.8.4
That's it 

Regards,
plusminus








