Learn android<notification notifications from scratch. 44 .>

Source: Internet
Author: User

In Android, some apps are sometimes pushed on the main screen, and some can include pictures, sounds or vibrations, and when you click on these tips, you can sometimes go to the app that sends the hint.


The push of these tips is notification, and of course the notification is essentially a service for you.


The first thing you want to do with notifications is to use the two classes of Notification.builder and Notificationmanager


Use Notification.builder to get notification objects, use Notificationmanager to get action notifications for objects


Of course, we can also use Remoteviews to get custom view to create custom notifications


Now let's use examples to learn the usage of notifications.


Main interface XML

<relativelayout 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 "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <button android:id=" @+id/button1 "android:layout_width=" Wrap_content "Androi        d:layout_height= "Wrap_content" android:layout_alignparenttop= "true" android:layout_centerhorizontal= "true"        android:layout_margintop= "15DP" android:text= "original notification"/> <button android:id= "@+id/button2" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_alignright= "@+id/ Button1 "android:layout_below=" @+id/button1 "       android:layout_margintop= "53DP" android:text= "Custom Notification"/></relativelayout> 

Notification interface

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >    <textview        android:id= "@+id/textview1" android:layout_width= "Wrap_        Content "        android:layout_height=" wrap_content "        android:text=" main interface Oh "        android:textappearance="? android : Attr/textappearancelarge "/></linearlayout>

Customizing the Notification interface


<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "> <imageview Andro Id:id= "@+id/imageview1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android    Oid:layout_alignparentleft= "true" android:layout_alignparenttop= "true" android:src= "@drawable/b12"/> <textview android:id= "@+id/textview1" android:layout_width= "Wrap_content" android:layout_height= "W Rap_content "android:layout_alignparenttop=" true "android:layout_torightof=" @+id/imageview1 "Android: text= "University orange Alert" android:textappearance= "Android:attr/textappearancemedium"/> <textview android:id= " @+id/textview2 "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layo        ut_below= "@+id/textview1"android:layout_torightof= "@+id/imageview1" android:text= "partial area rainfall reaches 500ml"/></relativelayout> 


Master File

Package Com.example.notification1;import Android.app.activity;import Android.app.notification;import Android.app.notificationmanager;import Android.app.pendingintent;import Android.content.intent;import Android.os.bundle;import Android.view.view;import Android.widget.button;import Android.widget.RemoteViews;public Class Mainactivity extends Activity {private Button button1, Button2;private Notification.builder builder;private Notificationmanager Manager; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); button1 = (Button) This.findviewbyid (R.id.button1); Button2 = (Button) This.findviewbyid (r.id.button2); builder = new Notification.builder (this);//create a notification setting Manager = ( Notificationmanager) Getsystemservice (Notification_service); Button1.setonclicklistener (New View.OnClickListener ( {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubpendingintent pendingintent = Pendingintent.get Activity(mainactivity.this, 0, New Intent (Mainactivity.this,main.class), 0);//Gain Pendingintent Intent builder.setcontentintent ( pendingintent); Builder.setsmallicon (R.DRAWABLE.B12); Builder.setticker ("You have a new notice"); Builder.setcontenttitle ("Rain orange warning Builder.setcontenttext ("5 o'clock in the afternoon rain will reach 500ml, please take precautions"); Builder.setcontentinfo ("Rainstorm, Gale"); Builder.setsubtext (" 7 rain at the end of this month "); Builder.setdefaults (notification.default_sound);//Set Default prompt health long[] Vibrate = {100l, 1000l, 100l, 1000l, 10l, 1000l};//Set the frequency of vibration builder.setvibrate (vibrate);//Set custom vibration Notification notification = Builder.build (); Manager.notify ( notification),//Open notification});//Custom Notification Button2.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubremoteviews contentremoteviews = new Remoteviews (Getpackagename (), R.L Ayout.custom);//Get custom layoutintent intent = new Intent (mainactivity.this, main.class); Pendingintent conpendingintent = pendingintent.getactivity (mainactivity.this, 0, intent, 0); Builder.setcontent (conteNtremoteviews); builder.setcontentintent (conpendingintent); Notification Notification = Builder.build (); Manager.notify (1001, Notification);}});}


Notification Interface Java file


Package Com.example.notification1;import Android.app.activity;import Android.os.bundle;public class Main extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Super.setcontentview (R.layout.main);}}





Click Notifications





Click the custom notification





Through this section of learning, you want readers to be able to master the use of raw notifications and the configuration and use of custom notifications.


Next forecast: Bordercast broadcast

The

learns android<notification notifications from scratch. 44.

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.