Use of Notification and NotificationManager in Android

Source: Internet
Author: User

Basic Steps for using Notification:

Use of noication ication and icationicationmanager

1. Obtain the system-level service NofifiactionManager

String Service = NOFICATION_SERVICE

Icationicationmanager nm = (NotificationManager) getSystemService (Service );

2. instantiate the Notificatoin object and set its attributes:

 

Notification n = new noication ();

// Set the display icon, which will be displayed in the status bar

Int icon = R. drawable. icon;

// Set the prompt information, which will be displayed in the status bar

String tickerText = "Test icationicationmanager ";

// Display time

Long when = system. currentTimeMills ();

N. icon = icon;

N. ticker = tickerText

N. when = when

You can also use the constructor to set

Icationicationmanager nm = new NotificationManager (icon, ticker, when );

3. Call setLatestEventInfo () to set the icon and time in the view.

// Instantiate Intent

Intent intent = new Intent (this, MainActivity. class );

// Get PendingIntent

PendingIntent pi = PendingIntent. getActivity (this, 0, intent, 0 );

// Set event information

N. setLastestEventInfo (this, "my title", "my Content", pi );

4. Send a notification

. // Indicates the notification ID

Int ID = 1;

Nm. notify (ID, n );

 

 

Example: register a broadcast in an activity and use the broadcast to display the Notification .:

Package com. king. android. controls;

Import android. app. Activity;
Import android. app. Service;
Import android. content. Intent;
Import android. content. IntentFilter;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;

Import com. king. android. R;

/**

* Description: initiates a broadcast.
* Author: Andy. Liu
* Time: 08:31:49
**/
Public class extends ityactiivty extends Activity {
Private static final String MY_SERVICE = "com. king. android. controls. MY_NOTIFITY_SERVICE"; // The specified Notification action.
// Declare the IPerson Interface
Private IPerson iPerson;
Private Button btn;
MyReceiver2 receiver ER = new MyReceiver2 ();

@ Override
Protected void onResume (){
Super. onResume ();
IntentFilter filter = new IntentFilter ();
// Instantiate MYReceiver2
RegisterReceiver (receiver, filter );
}
@ Override
Protected void onPause (){
Super. onPause ();
UnregisterReceiver (receiver );
}

@ Override
Protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Btn = (Button) findViewById (R. id. btn_voice );
Btn. setText ("send notification ");
Btn. setVisibility (View. VISIBLE );
Btn. setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View v ){
Intent intent = new Intent ();
Intent. setAction (MY_SERVICE );
SendBroadcast (intent );
}
});
}

}

Package com. king. android. controls;

Import android. content. BroadcastReceiver;
Import android. content. Context;
Import android. content. Intent;

/**

* Description: broadcasts notifications and initiates notification events.
* Author: Andy. Liu
* Time: 08:34:20
**/
Public class equalityreciver extends BroadcastReceiver {

@ Override
Public void onReceive (Context context, Intent intent ){
Intent I = new Intent ();
I. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
I. setClass (context, DisplayActivity. class );
Context. startActivity (I );
}

}

Package com. king. android. controls;

Import android. app. Activity;
Import android. app. Notification;
Import android. app. icationicationmanager;
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;
Import android. widget. TextView;

Import com. king. android. R;

/**

* Description: displays notifications.
* Author: Andy. Liu
* Time: 08:36:31
**/
Public class DisplayActivity extends Activity {
Public static final int NOFITY_ID = 100;
@ Override
Protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );
String Service = icationication_service;
Final NotificationManager nm = (NotificationManager) getSystemService (Service );
SetContentView (R. layout. main );

TextView TV = (TextView) findViewById (R. id. TV _info );
TV. setVisibility (View. VISIBLE );
Button btnCancel = (Button) findViewById (R. id. btn_voice );
BtnCancel. setVisibility (View. VISIBLE );
BtnCancel. setText ("cancel broadcast ");
BtnCancel. setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View v ){
Nm. cancel (NOFITY_ID );
}
});

Notification n = new Notification ();
Int icon = R. drawable. ic_launcher;
String tickerText = "Test notifiManager ";
Long when = System. currentTimeMillis ();
N. icon = icon;
N. tickerText = tickerText;
N. when = when;
// Instantiate Intent
Intent intent = new Intent (this, MainActivity. class );
// Get PendingIntent
PendingIntent pi = PendingIntent. getActivity (this, 0, intent, 0 );
// Set event information
N. setLatestEventInfo (this, "My title", "My content", pi );
Nm. notify (NOFITY_ID, n );

}

}

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.