Notification (1)-Basic Knowledge example
Main. xml is as follows:
Another. xml is as follows:
MainActivity is as follows:
Import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. app. activity; import android. app. notification; import android. app. icationicationmanager; import android. app. pendingIntent; import android. content. intent;/*** Demo Description: Send system notifications * Note: * 1 PendingIntent use * 2 permission application **/public class MainActivity extends Activity {private Button mButton; private final int FIRST = 1; private final int SECOND = 2; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); init ();} private void init () {mButton = (Button) findViewById (R. id. button); mButton. setOnClickListener (new listener ();} private class ButtonOnClickListenerImpl implements OnClickListener {@ Overridepublic void onClick (View v) {icationicationmanager manager = (icationicationmanager) getSystemService (icationication_service ); intent firstIntent = new Intent (); PendingIntent firstPendingIntent = PendingIntent. getActivity (MainActivity. this, 0, firstIntent, 0); Notification firstNotification = new Notification (); // set the Notification icon firstNotification. icon = R. drawable. ic_launcher; // set the status bar notification message firstNotification. tickerText = getResources (). getString (R. string. firstNotification_ticker); // sets the notification sending time firstNotification. when = System. currentTimeMillis (); // uses the system default sound, vibrate, and flashlight firstNotification. defaults = Notification. DEFAULT_ALL; // The firstNotification icon disappears after the application is opened. flags | = Notification. FLAG_AUTO_CANCEL; // set the notification title and content, and click the corresponding action after the notification is clicked. no operation is performed here. // Note: if the last parameter of this method is set to null, The firstNotification error is returned. setLatestEventInfo (MainActivity. this, getResources (). getString (R. string. firstNotification_title), getResources (). getString (R. string. firstNotification_content), firstPendingIntent); // sends the first notification manager. optional Y (FIRST, firstNotification); Intent secondIntent = new Intent (MainActivity. this, AnotherActivity. class); PendingIntent secondPendingIntent = PendingIntent. getActivity (MainActivity. this, 0, secondIntent, 0); Notification secondNotification = new Notification (); // set the Notification icon secondNotification. icon = R. drawable. ic_launcher; // set the notification message secondNotification for the status bar. tickerText = getResources (). getString (R. string. secondNotification_ticker); // sets the notification sending time secondNotification. when = System. currentTimeMillis (); // uses the system default sound, vibrate, flash secondNotification. defaults = Notification. DEFAULT_ALL; // The secondNotification icon disappears after the application is opened. flags | = Notification. FLAG_AUTO_CANCEL; // set the notification title and content, and click the corresponding action after the notification is clicked. this is to open the specified Activity // Note: if the last parameter of this method is set to null, The secondNotification error is returned. setLatestEventInfo (MainActivity. this, getResources (). getString (R. string. secondNotification_title), getResources (). getString (R. string. secondNotification_content), secondPendingIntent); // sends the second notification manager. notify (SECOND, secondNotification );}}}
AnotherActivity is as follows:
import android.app.Activity;import android.os.Bundle;public class AnotherActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.another);}}