Android-custom Notification
For the message in the message bar on June 23, Android enthusiasts know exactly what it is. For example, if we download an application, it may push some messages to our mobile phone at regular intervals, for example, a message may be displayed in the notification bar when it is delivered. This blog post introduces how to customize a Notification, which is very simple. Here, the little witch just sorts it out and provides a demo.
First, let's take a look at: Release/release + Mjk5NDAyMTMzo6jSxravv6q3or/release/xo6zQoc23z/gjrlhqzokjrm1_n21ykos1rg907 + release "brush: java. wwj. custom. notification; import android. app. activity; import android. app. notification; import android. app. icationicationmanager; import android. app. pendingIntent; import android. content. context; import android. content. intent; import android. graphics. bitmapFactory; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button;/*** custom Notification ** @ author wwj **/public class MainActivity extends Activity implements OnClickListener {private Button showNotification; private Button showCustomNotifi; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); showNotification = (Button) findViewById (R. id. button1); showCustomNotifi = (Button) findViewById (R. id. button2); showNotification. setOnClickListener (this); showCustomNotifi. setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. button1: send (); break; case R. id. button2: custom (); break; default: break;}/*** old Method */public void send () {// 1 get notification manager icationicationmanager nm = (icationicationmanager) getSystemService (Context. NOTIFICATION_SERVICE); // 2 build Notification notification Notification = new Notification (android. r. drawable. stat_policy_chat, "this is the prompt message", System. currentTimeMillis (); // 3 sets the notification Click Event Intent intent = new Intent (this, MainActivity. class); PendingIntent contentIntent = PendingIntent. getActivity (this, 100, intent, 0); notification. setLatestEventInfo (this, "notification title", "notification content", contentIntent); notification. flags = Notification. FLAG_AUTO_CANCEL; // automatically disappears after the notification is clicked // 4 send the notification nm. notify (100, notification);}/*** New Method for customizing the Notification method *. The test on the mobile phone will crash. If not, you can continue to use the old build method. After all, the higher version will be compatible with the lower version */public void custom () {// 1 to get the notification manager icationicationmanager manager = (icationicationmanager) getSystemService (Context. NOTIFICATION_SERVICE); // 2 sets the notification Click Event Intent intent = new Intent (this, MainActivity. class); PendingIntent contentIntent = PendingIntent. getActivity (this, 100, intent, 0); // 3 build Notification. builder builder = new Notification. builder (this) // method added by API 11. setContentIntent (contentIntent ). setSmallIcon (R. drawable. icon) // set the title of the status bar. setLargeIcon (BitmapFactory. decodeResource (getResources (), R. drawable. jay) // set the icon in the drop-down list. setWhen (System. currentTimeMillis ()). setTicker ("") // set the information displayed in the status bar. setAutoCancel (true) // The setting can be cleared. setContentTitle ("Notification") // set the title in the drop-down list. setContentText ("Fengjie is coming to tiantuo game, all departments are ready for Lightning Protection"); // you can clear Notification notification = builder. build (); // API 16 Add the method for creating notification // notification manager. Y (110, notification); // 2 build Notification // Notification notification2 = new notification (R. drawable. jay, "tiantuo game", // System. currentTimeMillis (); // 3 set the notification Click Event // Intent intent2 = new Intent (this, MainActivity. class); // PendingIntent contentIntent2 = PendingIntent. getActivity (this, 100, // intent2, 0); // icationication2.setlatesteventinfo (this, "", "", // contentIntent2 ); /// notification2.flags = Notification. FLAG_AUTO_CANCEL; // automatically disappears after the notification is clicked //// 4 send the notification // manager. notify (100, notification2 );}}
A little bit, we all know that the Android SDK version changes and the API will follow. Each version of the API may add or delete some interfaces, when using Android to provide some methods for our developers, we need to pay attention to the differences between versions. If we use a later version of the API, it may cause some errors, if a lower version is used, it may not be applicable in development. It is the minimum guarantee that the program will not crash. What APIs can be used to implement the functions of the program.