Notification
in Status Bar
The status bar across the
top of the device screen shows pending notifications for the user to read at a convenient
time. In general, because an activity mostly interacts with the user, services are more likely
to utilize this feature. As a rule, notifications should be concise and minimal for the best
user experience.
The steps for creating a
status bar notification are
1.Declare a notification
and specify how it displays on the status bar:
String ns = Context.NOTIFICATION_SERVICE;
mNManager = (NotificationManager)
getSystemService(ns);
final Notification msg = new
Notification(R.drawable.icon,
"New event of importance",
System.currentTimeMillis());
2. Define how it looks
when the status bar is expanded for details and the action
taken when clicked (this
future action is defined by a PendingIntent class):
Context context = getApplicationContext();
CharSequence contentTitle = "ShowNotification
Example";
CharSequence contentText = "Browse Android
Cookbook Site";
Intent msgIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.pearson.com"));
PendingIntent intent =
PendingIntent.getActivity(ShowNotification.this,
0, msgIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
3. Add any further
configurable information, such as whether to blink an LED, play a
sound, or automatically
cancel the notification after it is selected.The latter two are
shown here:
msg.defaults |= Notification.DEFAULT_SOUND;
msg.flags |= Notification.FLAG_AUTO_CANCEL;
4. Set the info for the
notification event to the system:
msg.setLatestEventInfo(context,
contentTitle, contentText, intent);
5. On the event of
interest, trigger notification with a unique identifier:
mNManager.notify(NOTIFY_ID, msg);
6. Upon completion,
clear notification as needed with the same identifier.
NotificationActivity.java Code
package
android.example.notification;
import
android.app.Activity;
import
android.app.Notification;
import
android.app.NotificationManager;
import
android.app.PendingIntent;
import android.content.Context;
import
android.content.Intent;
import
android.os.Bundle;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
public class
NotificationActivity extends Activity {
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final
NotificationManager notiManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
final Notification
noti = new Notification(R.drawable.ic_launcher,
"Notification
Text", System.currentTimeMillis());
Intent intent = new Intent(this,
DestinationActivity.class);
PendingIntent destIntent =
PendingIntent.getActivity(this, 0, intent, 0);
noti.setLatestEventInfo(this, "Notification
Title", "Notification Content", destIntent);
Button show1 =
(Button)findViewById(R.id.show1);
show1.setOnClickListener(new
OnClickListener() {
public void onClick(View v)
{
notiManager.notify(1,
noti);
}
});
Button show2 =
(Button)findViewById(R.id.show2);
show2.setOnClickListener(new
OnClickListener() {
public void onClick(View v)
{
noti.defaults = Notification.DEFAULT_SOUND;
notiManager.notify(2,
noti);
}
});
Button show3 =
(Button)findViewById(R.id.show3);
show3.setOnClickListener(new
OnClickListener() {
public void onClick(View v)
{
noti.defaults |=
Notification.DEFAULT_SOUND;
noti.defaults |=
Notification.DEFAULT_VIBRATE;
notiManager.notify(3,
noti);
}
});
}
}
main.xml Code
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#498545"
android:orientation="vertical"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:textColor="#FF0000"
android:text="Notification
Example"
android:textSize="30dp"
/>
<Button
android:id="@+id/show1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Notification
Bar" />
<Button
android:id="@+id/show2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Notification
Bar with Sound" />
<Button
android:id="@+id/show3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Notification
Bar with Sound and Vibrate" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="200dp"
android:text="main.xml"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#0000FF"
android:textSize="26dp"
/>
</LinearLayout>
AndroidNotificationManifest.xml
<?xml version="1.0"
encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.example.notification"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk android:minSdkVersion="7"
/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
>
<activity
android:label="@string/app_name"
android:name=".NotificationActivity"
>
<intent-filter >
<action android:name="android.intent.action.MAIN"
/>
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<activity
android:name="android.example.notification.DestinationActivity"
>
</activity>
</application>
<uses-permission android:name="android.permission.VIBRATE"
/>
</manifest>
Download Android Notification Example Code
More Info
Android Toast Notification
-->
Android Control ควบคุมอุปกรณ์ต่างๆ ด้วย Android
สอนเขียน Android สอนเขียนโปรแกรม Android
http://androidcontrol.blogspot.com/2012/01/beginning-android-training-android.html
ติดต่อ amphancm@gmail.com
ติดต่อ amphancm@gmail.com