[Android] In 15.4, for example, 15-2: Basic Notification usage and notification usage

Source: Internet
Author: User

[Android] In 15.4, for example, 15-2: Basic Notification usage and notification usage

Category: C #, Android, VS2015;

Created on: 1. Introduction

The previous section describes the basic content related to Notification. This section uses a simple example to demonstrate the basic usage of creating and releasing local notifications. The next section demonstrates more usage. Ii. Example 2 run

After you click the publish notification button on the ch1502MainActivity screen (left-side chart), a notification icon is displayed in the upper left corner of the screen, the notification area is displayed ). Click the sample notification in the notification area to display the ch1502SecondActivity screen (not) and the number of times the button is clicked on the ch1502MainActivity screen.

3. Main design steps

1. Add a notification icon

Upload a ch1502statbutton.png image under the drawablefolder as the notification icon. Or create an icon by yourself.

2. Add ch1502_Main.axml

Add the file in the layout folder.

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <Button android: id = "@ + id/ch1502_btn1" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = ""/> </LinearLayout>

3. Add the ch1502_Second.axml file.

Add the file in the layout folder.

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:minWidth="25px"    android:minHeight="25px">    <TextView        android:text=""        android:textAppearance="?android:attr/textAppearanceLarge"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:id="@+id/ch1502_textView1" /></LinearLayout>

4. Add the ch1502SecondActivity. cs file.

Add the file in the SrcDemos folder.

Using Android. app; using Android. content; using Android. OS; using Android. widget; namespace MyDemos. srcDemos {[Activity (Label = "[Example 15-2] basic Notification usage")] public class ch1502SecondActivity: Activity {protected override void OnCreate (Bundle savedInstanceState) {base. onCreate (savedInstanceState); // obtain the count value passed in MainActivity. If no count exists,-1 int count = Intent is returned. extras. getInt ("count",-1); // if no count is passed , Directly return if (count <= 0) {return;} SetContentView (Resource. layout. ch1502_Second); TextView txtView = FindViewById <TextView> (Resource. id. textView1); txtView. text = string. format ("You clicked {0} Times. ", Count );}}}

5. Add the ch1502MainActivity. cs file.

Add the file in the SrcDemos folder.

Using System; using Android. app; using Android. content; using Android. OS; using Android. widget; namespace MyDemos. srcDemos {[Activity (Label = "ch1502MainActivity")] public class ch1502MainActivity: Activity {// unique ID of the notification private static readonly int ButtonClickNotificationId = 1000; // number of times the button is clicked private int count = 1; protected override void OnCreate (Bundle savedInstanceState) {base. onCreate (savedInstanc EState); SetContentView (Resource. layout. ch1502_Main); Button button = FindViewById <Button> (Resource. id. ch1502_btn1); button. click + = Button_Click;} private void Button_Click (object sender, EventArgs e) {// pass count to the next activity: Bundle valuesForActivity = new Bundle (); valuesForActivity. putInt ("count", count); // when the user clicks the notification, start SecondActivity Intent resultIntent = new Intent (this, typeof (ch1502SecondA Ctient); resultIntent. putExtras (valuesForActivity); // constructs back stack TaskStackBuilder stackBuilder = TaskStackBuilder for cross-task navigation. create (this); stackBuilder. addParentStack (Java. lang. class. fromType (typeof (ch1502SecondActivity); stackBuilder. addNextIntent (resultIntent); // create PendingIntent resultPendingIntent = stackBuilder for the back stack. getPendingIntent (0, PendingIntentFlags. updateCurrent ); // Create a Notification. builder builder = new Notification. builder (this ). setAutoCancel (true) // cancel the notification when you click the notification (to make the notification disappear ). setContentIntent (resultPendingIntent) // start the activity when you click Intent. setContentTitle ("simple notification example") // Title. setNumber (count) // display the value of count in Content Info. setDefaults (icationicationults. sound | icationicationdefaults. vibrate) // play the sound and Vibrate when the notification is displayed. setSmallIcon (Resource. drawable. ch1502statButton) // display the notification Graph Standard. SetContentText (string. Format ("You have clicked the {0} button. ", Count); // displayed message // publish notification icationicationmanager icationmanager = GetSystemService (icationicationservice) as icationicationmanager; NotificationManager. Y (ButtonClickNotificationId, builder. build (); // The number of buttons plus 1 count ++ ;}}}

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.