Notification usage of Android programming development _android

Source: Internet
Author: User

This example describes the notification usage of Android programming. Share to everyone for your reference, specific as follows:

Notification is the meaning of the notice, Andong refers to the notice bar, generally used in the telephone, sms, mail, Alarm bell, in the mobile phone status bar will appear a small icon, prompting users to deal with this newsletter, when the hand from the top sliding status bar can be expanded and processed this newsletter.

In the help document, it is said that the notification class represents a persistent notification that will be submitted to the user using Notificationmanager. Added Notification.builder to make it easier to build notifications.

Notification is an alert user that lets your application run without opening or running in the background. It is an invisible program component (broadcast Receiver,service and inactive activity) that alerts users to the best way to take notice of events.

First, distinguish between the following status bars and the status bar:

1, the status bar is the top of the mobile phone screen a shape of the region;

In the status bar has a lot of information: such as USB connection icon, cell phone signal icon, battery power icon, time icon and so on;

2, the status bar is the hand from the state bar sliding down the view can be telescopic;

There are generally two classes in the status bar (using the flag_ tag):

(1) the procedure under way; (2) is the notification event;

A notification transmission of information is:

1, a status bar icon;

2, in the Stretched Status bar window display with a large title, small title, icon information, and have to handle the Click event: such as calling the program's entry class;

3, Flash, LED, or vibration;

The following is a brief introduction to some constants, fields, and methods in the notification class:
Constant:

Default_all uses all default values, such as sound, vibration, splash screen, etc.
Default_lights Use the default flash hint
Default_sounds Use the default prompt sound
Default_vibrate Use default phone vibration

Note: When adding mobile phone vibration effect must be in the project list file to add mobile Vibration permission:

Copy Code code as follows:
<uses-permission android:name= "Android.permission.VIBRATE"/>

The following is a simple example of a small amount to achieve the notification function:

Mainactivity.java:

Package Com.example.lession16_notifi;
Import android.app.Activity;
Import android.app.Notification;
Import Android.app.NotificationManager;
Import android.app.PendingIntent;
Import android.content.res.Resources.NotFoundException;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Toast;
  public class Mainactivity extends activity {private Notificationmanager Notificationmanager;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_main); The first step: The Notificationmanager object is obtained by Getsystemservice () method, Notificationmanager = (Notificationmanager) getsystemservice (
  Notification_service); //Test public void Test1 (View v) {shownotification ("SMS", "5557", "hello!
  ", R.drawable.ic_launcher, R.drawable.ic_launcher); //Step Two: Set some properties of notification such as: content, icon, title, corresponding notification action to handle, etc. public void shownotification (String tickertext, S
      Tring Contenttitle,String contenttext, int iconid, int notiid) {//Create a Notification Notification Notification = new Notification ();
    Set notification message icon Notification.icon = iconID;
    Set the content that emits the message Notification.tickertext = Tickertext;
    Set the time to send notification Notification.when = System.currenttimemillis (); Sets the default sound, vibration, and light effects when a notification is displayed notification.defaults = notification.default_vibrate;//vibration//Notification Notificatio
    n = new Notification (r.drawable.ic_launcher, "message", System.currenttimemillis ());
    Step 3: The Pendingintent Android system is responsible for maintaining pendingintent pendingintent = Pendingintent.getactivity (this, 0,getintent (), 0);
    4 steps: Set more detailed information Notification.setlatesteventinfo (this, contenttitle, contenttext,pendingintent);  Step 5: Use the Notify method of the Notificationmanager object to display notification messages that need to be developed//notification identification Notificationmanager.notify (NOTIID,
  notification); //6 Step: Use the Cancelall () method of the Notificationmanager object to cancel public void Clearnoti (View v) {notificationmanager.cancelall ();//Clear All}}

 

Layout file Activity_main.xml:

<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: paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <button android:id=" @+id/button1 "android:layout_width=" Wrap_content "Android:layout_h" eight= "Wrap_content" android:layout_alignparentleft= "true" android:layout_alignparentright= "true" Android:layo Ut_alignparenttop= "true" android:layout_margintop= "22DP" android:onclick= "test1" android:text= "@string/text_no Tifi "/> <button android:id=" @+id/button2 "android:layout_width=" Match_parent "android:layout_height="
 Wrap_content "android:layout_alignleft=" @+id/button1 "   android:layout_below= "@+id/button1" android:layout_margintop= "60DP" android:onclick= "Clearnoti" Android:tex

 t= "@string/text_clear"/> </RelativeLayout>

Layout effect:

Remember to implement the vibration effect add permissions to the manifest file:

Copy Code code as follows:
<uses-permission android:name= "Android.permission.VIBRATE"/>

The implementation effect is as follows:

I hope this article will help you with the Android program.

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.