Android Notification-notification,-Notification

Source: Internet
Author: User

Android Notification-notification,-Notification

In android, when the app needs to send some notifications so that the user can notice the information you want to notify, you can use Notification. next, let's discuss the usage of Notification, which we will learn from a small example.

1. Create a new project and write two buttons in the layout. One is used to enable notifications and the other is used to disable notifications. Directly layout the code below.

<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:gravity="center"    >    <Button        android:id="@+id/bt_up"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:clickable="true"        android:onClick="openNotify"        android:text="open" />    <Button        android:id="@+id/bt_down"        android:layout_below="@id/bt_up"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="closeNotify"        android:text="close" /></RelativeLayout>

Then there is the implementation of the Code. It is very easy to go directly to the code. I believe you will understand it at a glance.

Package com. example. demo; 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;/***** @ author jianww * 2015-12-16 **/public class MainActivity extends Activity {/*** 1. create a notification manager and get the Notification Service * 2. create a notification object and send a notification. * 3. * // 1. Create a notification manager and private icationicationmanager policymanager; // 2. Declare the notification object variable. Private Notification y; // 3. Create intent. When you click Notification, open the corresponding meaning object and jump to the corresponding class. Private Intent intent;/** Intent is started in time, and the intent disappears with the activity. PendingIntent can be considered as a packaging of intent. Generally, the pendingintent instance is obtained through getActivity, getBroadcast, and getService. The current activity cannot start the intent contained in it immediately, instead, intent is called when pendingintent is executed externally. Because pendingintent stores the Context of the current App, it gives the external App the ability to execute the Intent in pendingintent just like the current App, even if the current App does not exist during execution, the Intent can still be executed through the Context in pendingintent. You can also process operations performed by intent. It is often used with alermanger and icationicationmanager. Intent is generally used to transmit data between activities, Sercvice, and BroadcastReceiver, while Pendingintent is generally used in Notification. It can be understood as a delayed execution intent, and PendingIntent is a packaging of Intent. In this example, pendingIntent can be used to open objects in the app to be opened from the notification. */Private PendingIntent pendIntent; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); yymanager = (icationicationmanager) getSystemService (icationication_service);} // open public void openNotify (View v) {// create a notification object instance and input the default notification object image, notification title, the time when the notification was issued. Optional Y = new Notification (R. drawable. ic_launcher, "Notification", System. currentTimeMillis (); // create intent. This intent will not be executed immediately. The intent passed in will be executed only when pendingIntent is executed. Intent = new Intent (getApplicationContext (), MainActivity. class); // get the pendintent object instance. Set the flag of this delay intent. PendIntent = PendingIntent. getActivity (getApplicationContext (), 100, intent, 0); // you can specify the title and content of a notification. Notify. setLatestEventInfo (getApplicationContext (), "notification", "Notification content", pendIntent); // set the notification flag to default. Y. flags = Notification. FLAG_AUTO_CANCEL; // start Notification. Yymanager. Y (100, notify);} // close the notification. Public void closenostrap (View v) {// close the notification. PendIntent. cancel ();}}

That's just a simple review of basic knowledge. Pai_^

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.