Getting Started with Android Development (iv) Send notification 4.2 notification notification

Source: Internet
Author: User
Tags xmlns

So far, presumably everyone is already familiar with the use of toast to display information to users. Although it is convenient to use toast, the notifications displayed by toast are not stored permanently. It only appears for a short time on the screen and then disappears. If it contains some particularly important information, if the user does not observe the screen, then the user can easily miss it.

For those important information, a more persistent approach should be adopted. In this case, you should use the Notificationmnanger (Message manager) to display a long message that is displayed on the StatusBar (status bar) and can be easily seen by the user.

Next shows how to send a notification notification.

1. Create a project: notifications.

2. Create a new class named Notificationview in the package, and create a new name for the Notification.xml file under the Res/layout folder, which will act as a Notificationview view.

3. Documents in the Notification.xml.

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=    
"http://schemas.android.com/apk/" Res/android " 
    android:layout_width=" fill_parent " 
    android:layout_height=" fill_parent " 
    android:o" rientation= "vertical" >    
       
    <textview 
        android:layout_width= "Fill_parent" 
        Wrap_content " 
        android:text=" Here are the details for the notification ... "/>    
       
</LinearLayout>

Code in the 4.notificationview.java.

public class Notificationview extends activity {    
    @Override public 
    void OnCreate (Bundle savedinstancestate) {    
        super.oncreate (savedinstancestate);    
        Setcontentview (r.layout.notification);    
       
        ---Look up the Notification Manager service---    
        notificationmanager nm = (Notificationmanager) getsystemservice ( Notification_service);  

  
       
        ---Cancel the notification that we started---    
        nm.cancel (getintent (). Getextras (). GetInt ("Notificationid"));    
}

5. Code in the Androidmanifest.xml.

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "Http://schemas.android.com/apk/res/androi"    
       
    D "package=" Net.learn2develop.Notifications "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "/> <uses-permission android:name=" Android.permission.VIBRAT E "/> <application android:icon=" @drawable/ic_launcher "android:label=" @string/app_ The name "> <activity android:label=" @string/app_name "Android:name=". Notificationsactivity "> <intent-filter > <action android:name=" android.in    
            Tent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/>" </intent-filter> </activity> <activity android:name= ". Notificationview "Android:label= "Details of notification" > <intent-filter> <action android:name= "Android."     
            Intent.action.MAIN "/> <category android:name=" Android.intent.category.DEFAULT "/>" </intent-filter> </activity> </application> </man Ifest>

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.