Android [basic tutorial] Chapter 1 Application of notification

Source: Internet
Author: User

In this chapter, we will learn the application of notification. Many people ask what notification is? Let me give it an analogy. for travel to the West, Tang Miao is caught by monsters. Then Wukong needs to know which monster has caught his master. He has to become some animals (flies or mosquitoes) let's inform his master and inform Tang sengwukong to save him. Here, the notification is notification. Then the Tang Monk knows, and he is relieved to wait for Wukong to save his work. haha, let me take a look at main. XML

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "match_parent" Android: layout_height = "match_parent"> <button Android: TEXT = "notice the master Wukong to save him" Android: Id = "@ + ID/Wukong" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </button> <button Android: text = "" Android: Id = "@ + ID/Bajie" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </button> <button Android: text = "notice the Master Sha Seng to save him" Android: id = "@ + ID/shaseng" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </button> <button Android: text = "" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Id = "@ + ID/policy_cancal"/> </linearlayout>

There is nothing special here, only four buttons are defined, and the main activity. Java

Import android. app. activity; import android. app. notification; import android. app. icationicationmanager; import android. app. pendingintent; import android. content. intent; import android. OS. bundle; import android. view. view; import android. widget. button; public class icationicationdemo extends activity {private final static int notifycation_id = 0x11; @ overrideprotected void oncreate (bundle savedinstancestate) {// todo auto-generated method stubsuper. oncreate (savedinstancestate); setcontentview (R. layout. notification); // search for Main. in XML, the button control button Wukong = (button) findviewbyid (R. id. wukong); Wukong. setonclicklistener (New clickevent (); button Bajie = (button) findviewbyid (R. id. bajie); Bajie. setonclicklistener (New clickevent (); button shaseng = (button) findviewbyid (R. id. shaseng); shaseng. setonclicklistener (New clickevent (); button policy_cancal = (button) findviewbyid (R. id. notify_cancal); yy_cancal.setonclicklistener (New clickevent ();} class clickevent implements view. onclicklistener {@ overridepublic void onclick (view v) {// obtain the system's icationicationmanager service icationicationmanager yymanager = (icationicationmanager) getsystemservice (notification_service ); // create an intentintent intent = new intent (icationicationdemo. this, otheractivity. class); // sets the class of pendingintent Pi = pendingintent when the content is displayed when the notification is clicked. getactivity (icationicationdemo. this, 0, intent, 0); // create the notification object Notification every Y = new notification (); // set the notification sending time every Y. when = system. currenttimemillis (); // set the default sound for notification, default vibration, default flashlight, but these need to be in androidmanifest. add the corresponding permissions to the XML file. Otherwise, notify is reported. defaults = notification. default_all; Switch (v. GETID () {case R. id. wukong: // set the notification icon policy. icon = R. drawable. wukong; // set the notification event information notify. setlatesteventinfo (icationicationdemo. this, "Wukong", "Wukong to save the master's notice", Pi); // sets the text content of notification, which will be displayed in the status bar notify. tickertext = "Wukong saved the master"; // send a notification to policymanager. Y (notifycation_id, notify); break; case R. id. bajie: // same as policy. icon = R. drawable. bajie; policy. setlatesteventinfo (icationicationdemo. this, "", "", Pi); policy. tickertext = "eight rings to save the master"; policymanager. Y (notifycation_id, notify); break; case R. id. shaseng: // same as above. icon = R. drawable. shaseng; policy. setlatesteventinfo (icationicationdemo. this, "Sha Seng", "Sha Seng to save the master's notice", Pi); policy. tickertext = "sashan has helped the master"; policymanager. Y (notifycation_id, notify); break; case R. id. yy_cancal: // cancel the notification to yymanager. cancel (notifycation_id );}}}}

OK. We add the corresponding permissions and the activity to be started in androidmanifest. xml.

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.kang.button_demo"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="10" />    <uses-permission android:name="android.permission.FLASHLIGHT"></uses-permission>    <uses-permission android:name="android.permission.VIBRATE"></uses-permission>    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:label="@string/app_name" android:name=".NotificationDemo">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>                <activity android:name=".otherActivity"></activity>    </application></manifest>

Okay, it's all done. Run it ,:

The main points here are the creation of notification and icationicationmanager. In fact, it is very useful to know how to develop Android applications. Well, this chapter is over. Thank you.

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.