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 = "notify the master wukong to save him" android: id = "@ + id/wukong"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </Button>
<Button android: text = "notify the master to save him" android: id = "@ + id/bajie"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </Button>
<Button android: text = "notify the master to save him" android: id = "@ + id/shaseng"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </Button>
<Button android: text = "Notification canceled" 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 policycation_id = 0x11;
@ Override
Protected void onCreate (Bundle savedInstanceState)
{
// TODO Auto-generated method stub www.2cto.com
Super. onCreate (savedInstanceState );
 
SetContentView (R. layout. notification );

// Find the Button control in main. xml
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. policy_cancal );
Notify_cancal.setOnClickListener (new ClickEvent ());
}
 
Class ClickEvent implements View. OnClickListener
{
 
@ Override
Public void onClick (View v)
{
// Obtain the icationicationmanager service of the system.
Icationicationmanager policymanager = (NotificationManager) getSystemService (NOTIFICATION_SERVICE );

// Create an Intent to start other activities
Intent intent = new Intent (icationicationdemo. this,
OtherActivity. class );
// Set the class to start when the notification is clicked.
PendingIntent pi = PendingIntent. getActivity (icationicationdemo. this,
0, intent, 0 );

// Create a Notification object
Notification every Y = new Notification ();

// Set the Notification sending time
Notify. when = System. currentTimeMillis ();
// Set the default sound for Notification, the default vibration, and the default flashlight. However, you must add the corresponding permissions to AndroidManifest. xml. Otherwise, an error is reported.
Y. defaults = Notification. DEFAULT_ALL;

Switch (v. getId ())
{
Case R. id. wukong:
// Set the Notification icon
Optional Y. icon = R. drawable. wukong;
// Set Notification event information
Notify. setLatestEventInfo (icationicationdemo. this, "Wukong ",
"Notice of Wukong saving the master", pi );
// Set the text content of Notification, which will be displayed in the status bar
Policy. tickerText = "Wukong has helped the master ";
// Send a notification
Yymanager. Y (NOTIFYCATION_ID, notify );
 
Break;
 
Case R. id. bajie:

// Same as above
Optional Y. icon = R. drawable. bajie;
Notify. setLatestEventInfo (icationicationdemo. this, "eight rings ",
"Notice on saving the master", pi );
Policy. tickerText = "eight rings to save the master ";
Yymanager. Y (NOTIFYCATION_ID, notify );
 
Break;
 
Case R. id. shaseng:

// Same as above
Optional Y. icon = R. drawable. shaseng;
Notify. setLatestEventInfo (icationicationdemo. this, "sashan ",
"Notice from Sha Seng to save the master", pi );
Policy. tickerText = "sashan has helped the master ";
Yymanager. Y (NOTIFYCATION_ID, notify );
 
Break;

Case R. id. policy_cancal:

// Cancel notification
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" type = "codeph" text = "/codeph">
<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 = ". icationicationdemo">
<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.

From: kangkangz4 Column

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.