Android Status bar Notification notification

Source: Internet
Author: User

Notification can display an icon notification on the status bar at the top of the screen, can play sound at the same time as a notification, and vibrate to prompt the user, and click Notifications to return to the specified activity.
Examples of today's:

Layout Main.xml:
<?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/bt1"
android:layout_height= "Wrap_content"
Android:layout_width= "Fill_parent"
android:text= "Notification Test"
/>
<button android:id= "@+id/bt2"
android:layout_height= "Wrap_content"
Android:layout_width= "Fill_parent"
android:text= "Clear Notification"
/>
</LinearLayout>

Java code:
package com.pocketdigi.Notification;

import android.app.Activity;
Import android.app.Notification;
Import Android.app.NotificationManager;
Import android.app.PendingIntent;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;

public class Main extends Activity {
/** called when the activity is first created. */
int notification_id=19172439;
Notificationmanager nm;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Nm= (Notificationmanager) Getsystemservice (Notification_service);
Button bt1= (button) Findviewbyid (R.ID.BT1);
Bt1.setonclicklistener (Bt1lis);
Button bt2= (button) Findviewbyid (R.ID.BT2);
Bt2.setonclicklistener (Bt2lis);

}
Onclicklistener bt1lis=new Onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Shownotification (R.drawable.home, "text on the edge of the icon", "title", "Content");
}

};
Onclicklistener bt2lis=new Onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Shownotification (R.drawable.home, "text on the edge of the icon", "title", "Content");
Nm.cancel (notification_id);
}

};
public void shownotification (int icon,string tickertext,string title,string content) {
Set a unique ID, casually set

Notification Manager
Notification notification=new Notification (Icon,tickertext,system.currenttimemillis ());
The following parameters are displayed in the top notification bar of the small icon, the text next to the small icon (briefly displayed, automatically disappear) system current time (do not understand what is the use of this)
Notification.defaults=notification.default_all;
This is the setting notification whether the sound or vibration is played simultaneously, and the sound is Notification.default_sound
Vibration is notification.default_vibrate;
Light is notification.default_lights, and I don't seem to have any reaction on my milestone.
All for Notification.default_all
If it is vibrating or all, you must add the vibration permission in the Androidmanifest.xml
Pendingintent pt=pendingintent.getactivity (this, 0, new Intent (This,main.class), 0);
Click on the notification after the action, here is back to main this acticity
Notification.setlatesteventinfo (THIS,TITLE,CONTENT,PT);
Nm.notify (notification_id, notification);

}
}

androidmanifest.xml Join permissions:
<uses-permission android:name= "Android.permission.VIBRATE"/>
<!--allow Vibration--

Android Status bar Notification notification

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.