Android Programming Custom Notification Instance analysis _android

Source: Internet
Author: User
Tags ip number

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

Notification is the best way for your application to alert users without using an activity, notification is an invisible program component that alerts users to events that require attention.

As part of the UI, notification is the best fit for mobile devices. Users may have their phones with them at all times. In general, users will open several programs in the background, but they will not notice them. In such a case, it is important to be able to notify the user when there are events that need attention.

Notification is managed by Notificationmanger, and currently includes the following capabilities:

Create a status bar icon.

Displays additional information (and starts a intent) in the Extended Status bar window.

Flashing lights or LEDs.

Telephone vibration.

Audible warning sounds (ringtones, saved sound files).

Custom Notification Effect Chart:

Custom layout file:

<?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" >
 <textview
 android:id= "@+id/tv_rv" android:layout_width= "Wrap_content"
 "
 android:layout_height=" wrap_content "
 android:text=" haha "
 />
<progressbar
 Style = "@android: style/widget.progressbar.horizontal"
 android:id= "@+id/pb_rv"
 android:layout_width= "Wrap_ Content "
 android:layout_height=" wrap_content "
 />
</LinearLayout>

Create notification:

public class Customnotificationactivity extends activity {Notificationmanager Notificationmanager;
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main); Gets the Notificationmanager Notificationmanager = (Notificationmanager) Getsystemservice of the system (Context.notification_
  SERVICE);
     public void Click (View view) {//Instantiate a notification String tickertext = "IP number set complete";
     Long when = System.currenttimemillis ();
     Notification Notification = new Notification (R.drawable.icon, Tickertext, when);
     Can not manually clean//notification.flags= notification.flag_no_clear;
     Add music//notification.sound = Uri.parse ("/sdcard/haha.mp3");
     Set user clicks Notification Action//Pendingintent deferred intent Intent Intent = new Intent (this,bactivity.class);
     Pendingintent pendingintent = pendingintent.getactivity (this, 0, intent, 0);
     Notification.contentintent = pendingintent;
    Custom interface Remoteviews RV = new Remoteviews (Getpackagename (), r.layout.noti_layout);
     Rv.settextviewtext (R.ID.TV_RV, "I am a custom notification");
     Rv.setprogressbar (R.ID.PB_RV, n, false);
     Notification.contentview = RV;
  Pass the defined notification to Notificationmanager notificationmanager.notify (0, notification);

 }
}

I hope this article will help you with your Android programming.

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.