Generally when we receive SMS, ah, or some app reminders. We will be in the notification bar to receive a simple day message, and then click on the message into the app, in fact, there is a dedicated notification class in Android to complete the work, here to achieve this function.
Start by creating a new notificationtestproject and then adding a button to trigger the notification. Then write code such as the following:
Package Com.example.jared.notificationtest;import Android.app.notificationmanager;import Android.os.Bundle;import Android.support.v4.app.notificationcompat;import Android.support.v7.app.appcompatactivity;import Android.view.view;import Android.widget.button;public class Mainactivity extends Appcompatactivity {private Button sen DNOTIFICATIONBTN; private int mId = 1; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); SENDNOTIFICATIONBTN = (Button) Findviewbyid (r.id.sendnotification); Sendnotificationbtn.setonclicklistener (New Myonclicklistener ()); } Private class Myonclicklistener implements View.onclicklistener {@Override public void OnClick (View VI EW) {switch (View.getid ()) {case R.id.sendnotification:setsendnotification Btn (); Break Default Break }}} public void Setsendnotificationbtn () {notificationcompat.builder notification = new Notificati Oncompat.builder (This). Setsmallicon (R.mipmap.ic_launcher). Setcontenttitle ("My Notification "). Setcontenttext (" Hello Notification "); Notificationmanager manager = (Notificationmanager) getsystemservice (Notification_service); Manager.notify (MId, Notification.build ()); }}
here is the use of Notificatoncompat.builder to create a simple Notification,setsmallicon is specified in the icon, Setcontenttitle method is to specify the title, setcontenttext the specified content , and then through the getsystemservice to get notification of the management class, through the Notify method to send notifications, mid is an ID number, each notification has its unique notification number. cannot be repeated.
Performance effects such as the following are seen:
We then go to the corresponding activity after clicking on the notification and then dismiss the notification. Create a second activity, such as the following:
<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " tools:context= "Com.example.jared.notificationtest.Notification" > <textview android:layout_ Width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Welcome to click Notification Event!" Android:layout_margin= "20DP" android:textsize= "20DP"/></linearlayout>
here is a textview used to display the next message, and then write code such as the following:
Package Com.example.jared.notificationtest;import Android.app.notificationmanager;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;public class Notification extends appcompatactivity { private int mId = 1; @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_notification); Notificationmanager manager = (Notificationmanager) getsystemservice (notification_service); Manager.cancel (mId); }}
after entering the activity, the notice is cleared, and then the Mainactivity code is changed:
Package Com.example.jared.notificationtest;import Android.app.notificationmanager;import Android.app.pendingintent;import Android.content.intent;import Android.os.bundle;import Android.support.v4.app.notificationcompat;import Android.support.v7.app.appcompatactivity;import Android.view.view;import Android.widget.button;public class Mainactivity extends Appcompatactivity {private Button sen DNOTIFICATIONBTN; private int mId = 1; private int nummessage = 0; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); SENDNOTIFICATIONBTN = (Button) Findviewbyid (r.id.sendnotification); Sendnotificationbtn.setonclicklistener (New Myonclicklistener ()); } Private class Myonclicklistener implements View.onclicklistener {@Override public void OnClick (View VI EW) {switch (View.getid ()) {case r.id.sendnotification: SETSENDNOTIFICATIONBTN (); Break Default:break; }}} public void Setsendnotificationbtn () {notificationcompat.builder notification = new Notificati Oncompat.builder (This). Setsmallicon (R.mipmap.ic_launcher). Setcontenttitle ("My Notification "). Setcontenttext (" Hello Notification "). Setnumber (++nummessage); Intent Intent = new Intent (this, notification.class); Pendingintent pendingintent = pendingintent.getactivity (this, 0, intent, pendingintent.flag_cancel_current) ; Notification.setcontentintent (pendingintent); Notificationmanager manager = (Notificationmanager) getsystemservice (Notification_service); Manager.notify (MId, Notification.build ()); }}
The Setnumber method is added here. The main is to show a few notifications, for example, need to know, and then instantiate a intent. Then instantiate a pendingintent. Get activity, setcontentintent in Notificationcompat.builder. After that we can achieve our results, execute and click on the notifications as seen below:
Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "width=" "height=" >
As seen above received 6 notifications, and then click on the notification is also eliminated.
Generally in the download song Ah. Picture ah time. There will be a progress bar indicating the download process, here to simulate the implementation of this function. Change the Mainacitivy code such as the following:
Package Com.example.jared.notificationtest;import Android.app.notificationmanager;import Android.app.pendingintent;import Android.content.intent;import Android.os.bundle;import Android.support.v4.app.notificationcompat;import Android.support.v7.app.appcompatactivity;import Android.util.log;import Android.view.view;import Android.widget.button;public class MainActivity extends appcompatactivity {private static final String TAG = "mainactivity"; Private Button sendnotificationbtn; private int mId = 1; private int nummessage = 0; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); SENDNOTIFICATIONBTN = (Button) Findviewbyid (r.id.sendnotification); Sendnotificationbtn.setonclicklistener (New Myonclicklistener ()); } Private class Myonclicklistener implements View.onclicklistener {@Override public void OnClick (View VI EW) {switch(View.getid ()) {case r.id.sendnotification:setsendnotificationbtn (); Break Default:break; }}} public void Setsendnotificationbtn () {final Notificationcompat.builder notification = new not Ificationcompat.builder (This). Setsmallicon (R.mipmap.ic_launcher). Setcontenttitle ("The Music do Wnload "). Setcontenttext (" Burning.mp3 "). Setnumber (++nummessage); Intent Intent = new Intent (this, notification.class); Pendingintent pendingintent = pendingintent.getactivity (this, 0, intent, pendingintent.flag_cancel_current) ; Notification.setcontentintent (pendingintent); Final Notificationmanager manager = (Notificationmanager) getsystemservice (Notification_service); New Thread (New Runnable () {@Override public void run () { for (int cnt=0; cnt<=100; cnt++) {notification.setprogress (n, CNT, F Alse); Manager.notify (MId, Notification.build ()); try {thread.sleep (200); } catch (Interruptedexception e) {log.d (TAG, "Sleep failure"); }} notification.setcontenttext ("Download complete"); Notification.setprogress (0, 0, false); Manager.notify (MId, Notification.build ()); }}). Start (); }}
This is done here by the Setprogress method, where a thread is opened and the content is set again once the download is complete.
The results of the execution are as follows:
Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "width=" "height=" >
Figure 1 shows that the running progress bar is walking, and Figure 2 is finished with the download function.
Generally received notice, the phone will have a sound. Add vibration. Then come up with this feature. , assuming the download is complete, put a piece of music and shake, change the code such as the following:
Package Com.example.jared.notificationtest;import Android.app.notificationmanager;import Android.app.pendingintent;import Android.content.intent;import Android.net.uri;import Android.os.Bundle;import Android.support.v4.app.notificationcompat;import Android.support.v7.app.appcompatactivity;import Android.util.log;import Android.view.view;import Android.widget.button;import Java.io.File;public class Mainactivity extends Appcompatactivity {private static final String TAG = "mainactivity"; Private Button sendnotificationbtn; private int mId = 1; private int nummessage = 0; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); SENDNOTIFICATIONBTN = (Button) Findviewbyid (r.id.sendnotification); Sendnotificationbtn.setonclicklistener (New Myonclicklistener ()); } Private class Myonclicklistener implements View.onclicklistener {@Override public vOID OnClick (view view) {switch (View.getid ()) {case r.id.sendnotification: SETSENDNOTIFICATIONBTN (); Break Default:break; }}} public void Setsendnotificationbtn () {final Notificationcompat.builder notification = new not Ificationcompat.builder (This). Setsmallicon (R.mipmap.ic_launcher). Setcontenttitle ("The Music do Wnload "). Setcontenttext (" Burning.mp3 "). Setnumber (++nummessage); Intent Intent = new Intent (this, notification.class); Pendingintent pendingintent = pendingintent.getactivity (this, 0, intent, pendingintent.flag_cancel_current) ; Notification.setcontentintent (pendingintent); Final Notificationmanager manager = (Notificationmanager) getsystemservice (Notification_service); New Thread (New Runnable () {@OverrIDE public void Run () {for (int cnt=0; cnt<=100; cnt++) { Notification.setprogress (n, CNT, false); Manager.notify (MId, Notification.build ()); try {thread.sleep (100); } catch (Interruptedexception e) {log.d (TAG, "Sleep failure"); }} notification.setcontenttext ("Download complete"); Notification.setprogress (0, 0, false); Uri Sounduri = uri.fromfile (New File ("/system/media/audio/animationsounds/bootsound.ogg")); Notification.setsound (Sounduri); Long[] vibrates = {0, 1000, 1000, 1000}; Notification.setvibrate (vibrates); Manager.notify (MId, Notification.build ()); }}). Start (); }}
The Setsound and Setvibrate methods are added here, and you need to add permissions to the Androidmanifest:
Android : Name= "Android.permission.VIBRATE" />
The song name Here is the music that looks at the system's presence through the ADB shell:
after downloading to the phone, you can observe the effect.
Of course, can also control LED lights, I do not know why LED light effect has not been, online browsing a lot of information also did not find the problem, if a friend know. I'm not very grateful for the trouble telling one or two.
Notification.setsound (Ringtonemanager.getdefaulturi (ringtonemanager.type_notification)); Long[] vibrates = {0, +, +, +}; Notification.setvibrate (vibrates); Notification.setlights (Color.green, 1000, 1000);
about notification basically learn here.
Android Development Learning Path--notification First Experience