Detailed Android notification notice reminder _android

Source: Internet
Author: User

At the time of message notification, we often use two components toast and notification. Especially important and need long time display information, with notification is the most suitable. When a message is notified, the status bar will display the icon and text of the notification, the Drop-down status bar, you can see the notification information, Android this innovative UI components won the user's praise, even the apple began to imitate.  Today we will combine examples to explore the specific use of notification. First of all, we need to implement the function is: In the program started, issued a notice, this notice in the software running the process has been, the equivalent of the same as QQ tray.

Then we'll show you the normal notifications and custom view notifications, so we'll start with an Android project.

Then edit the/res/layout/main.xml file with the following code:

<linearlayout 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: Background= "@android: Color/black" android:orientation= "vertical" > <textview android:layout_width= "Match_ Parent "android:layout_height=" wrap_content "android:gravity=" Center_horizontal "android:text=" @string/title "Andr Oid:textcolor= "#0f0" android:textsize= "20sp" android:textstyle= "bold"/> <button android:layout_width= "match _parent "android:layout_height=" wrap_content "android:layout_marginleft=" 50DP "android:layout_marginright=" 50DP "a ndroid:layout_margintop= "30DP" android:onclick= "normal" android:text= "@string/notification" android:textcolor= "# 0f0 "android:textsize=" 20sp "/> <button android:layout_width=" match_parent "android:layout_height=" Wrap_cont Ent "android:layout_marginleft=" 50DP "android:layout_marginright="50DP" android:layout_margintop= "30DP" android:onclick= "Custom" android:text= "@string/custom" android:textcolor= "

 0f0 "android:textsize=" 20sp "/> </LinearLayout>

The above layout is simple, with two buttons for starting common notification and custom notification.
Next, customize a layout to display the custom notifications.

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
 android:layout_width=" match_parent "
 android:layout_height=" Match_parent "
 android: Background= "#000"
 android:orientation= "vertical" >

 <imageview
  android:id= "@+id/iv"
  Android:layout_width= "Match_parent"
  android:layout_height= "wrap_content"
  android:contentdescription= " @string/action_settings "
  android:src=" @drawable/ic_launcher "/>

 <textview android:id=
  " @+id /tv "
  android:layout_width=" match_parent "
  android:layout_height=" wrap_content "
  android:gravity=" Center_horizontal "
  android:textcolor=" "#0f0"
  android:textsize= "15sp"/>

</LinearLayout>

The next step is the code.

Package com.itfom.notification;
Import android.app.Activity;
Import android.app.Notification;
Import Android.app.NotificationManager;
Import android.app.PendingIntent;
Import Android.content.Context;
Import android.content.Intent;
Import Android.net.Uri;
Import Android.os.Bundle;
Import android.os.Environment;
Import Android.view.View;

Import Android.widget.RemoteViews;
 public class Mainactivity extends activity {private Notificationmanager Mnotificationmanager;
 private context;
 Private Notification Notification;
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 Setcontentview (R.layout.activity_main);
 }//Normal notification @SuppressWarnings ("deprecation") public void Normal (View v) {//Create notification createnotification ("ordinary notice");
 Put the notice in the running column notification.flags|=notification.flag_ongoing_event;
 Set the default sound notification.defaults|=notification.default_sound;
 Set the default vibration notification.defaults|=notification.default_vibrate; Set the default LED light liftWake Notification.defaults|=notification.default_lights;

 Set Click after the notification automatically clear notification.defaults|=notification.flag_auto_cancel;
 String texttitle= "notification Example";

 String textcontent= "program is running, click here to jump to demo interface";
 Intent it=new Intent (context, mainactivity.class);
 Pendingintent pendintent=pendingintent.getactivity (context, 0, it, 0);
 Notification.setlatesteventinfo (context, Texttitle, textcontent, pendintent);
 Mnotificationmanager.notify (0, notification);
 }//Custom notification public void custom (View v) {//Create notification createnotification ("personalized notifications");
 Custom notification of the sound Notification.sound=uri.parse (environment.getexternalstoragedirectory () + "/non.mp3");
 The custom vibration parameters are how long to start the vibration, the first time of vibration, the time to stop shaking long[] vibrate={0,100,200,300};
 Notification.vibrate=vibrate;
 Customizing the flash mode notification.ledargb=0xff00ff00;
 notification.ledonms=500;
 notification.ledoffms=500;

 Notification.flags|=notification.flag_show_lights;
 Remoteviews contentview=new remoteviews (This.getpackagename (), r.layout.notify); Contentview.settextviewtexT (r.id.tv, "This is personalized notice");
 Specifies the personalized view notification.contentview=contentview;
 Intent it=new Intent (context, mainactivity.class);
 Pendingintent pendintent=pendingintent.getactivity (context, 0, it, 0);
 Specify content view notification.contentintent=pendintent;
 Mnotificationmanager.notify (1, notification);
 ///Customize a method to create notification @SuppressWarnings ("deprecation") public Notification createnotification (String text) {context = this;
 Mnotificationmanager= (Notificationmanager) This.getsystemservice (Context.notification_service);
 int icon=r.drawable.ic_launcher;
 Long When=system.currenttimemillis ();
 return notification = new notification (icon, text, when);
 }//Rewrite onbackpressed event @Override public void onbackpressed () {super.onbackpressed ();
 Finish ();
 Cancellation notice mnotificationmanager.cancel (0);
 Android.os.Process.killProcess (Android.os.Process.myPid ());
 System.exit (0);
 }
}

The above code defines a method createnotification (String text). This method is used to create a notification. Note that this is written because. Whether it's a regular notification or a custom notification. The previous creation process is the same. Then we implemented the click event for two buttons.
The results of the run are as follows:

When you click on the two buttons, the following occurs:

Note: There is a sound effect here. If the phone supports flash, there are led effects.

The above is about the Android notification notice to realize the process of explanation, recently updated a lot about Android notification Notice reminders of the article, I hope to help you learn.

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.