Notification (1)-Basic Knowledge example

Source: Internet
Author: User

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);}}

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.