Android Phone multimedia--Notification

Source: Internet
Author: User

Create a phone alert

1. Create a Notificationmanager

Create a Notificationmanager to manage notifications on the line. The general call to the context's Getsystemservice () method gets the object, which passes in the service to get, and returns a value of that type. As follows:

/* get to a notificationmanager to manage notifications, * using the Getsystemservice method, passing in a service, returning a manager */  = (notificationmanager) getsystemservice (Notification_service);

2. Create a Notification object

You can use the constructors in the Notificationcompat class to--builder the constructor to create notification objects, which is good stability.

New Notificationcompat.builder (context). build ();

But this is just an empty notification, and you need to add as many settings as you want before build () to create a notification object, as follows:

1Notification Notification =NewNotificationcompat.builder ( This)2. Setcontenttitle ("This is content title")//set the notification title3. Setcontenttext ("This is content text")//Set notification content4. Setwhen (System.currenttimemillis ())//set the time when the notification was created5. Setsmallicon (R.mipmap.ic_launcher)//set Small icons for notifications6. Setlargeicon (Bitmapfactory.decoderesource (Getresources (), R.mipmap.ic_launcher))//set Large icons for notifications7. Setcontentintent (PI)//set the content of the notification the jump parameter is a pendingintent8. Setautocancel (true)//set reminders to automatically cancel when clicked9. build ();

These are some of the basic settings to be added.

3. Create a notification click Jump--pendingintent

Pendingintent is equivalent to a deferred execution of intent, and the usage is simple.

The static methods for getting Pendingintent instances are: getactivity (), Getbroadcast (), GetService () three, the parameters they accept are the same,

The first parameter is content, the second is generally set to 0, the third is a intent object, and the fourth is used to determine pendingintent behavior, with Flag_oneshot,flag_no_create,flag_cancel_ Current and flag_update_current four values can be selected, usually passed in 0.

                /* Create a time-lapse intent pendingintent,getactivity pass in four parameters,                * After the new notification will also have to setcontentintent this delay intent* / New Intent (This, notificationacitivity.                    Class);                 = Pendingintent.getactivity (This,0, intent,0);

This pi is also the object of the previous step to invoke Pendingintent.

4. Show notifications

You can then call the Notify () method in Notificationmanager to make the notification appear.

Manager.notify (1// parameters are ID and notification to be sent, respectively.) Each notification corresponds to an ID

5. More Notification settings

Notifications can also be set to sound, vibration, LED lighting and other effects, as follows:

1                         //sets the audio to receive a URI parameter when a notification is sent2. Setsound (Uri.fromfile (NewFile ("/system/media/audio/ringtones/luna.ogg")))3                         //set the vibration when the notification is sent, is an array of long integer, corresponding to the phone still time, vibration time, alternating, in milliseconds4. Setvibrate (New Long[]{0, +, +, +})5                         //set the LED light, three parameters are the color of the lamp, the lamp on the length of time, the lamp anxia length6. Setlights (Color.green, +, +)7. build ();

Where you need to claim permission to get the vibrate function

<uses-permission android:name="android.permission.VIBRATE"/>

If you don't want to set it yourself, you can use the default effect directly.

. SetDefaults (Notificationcompat.default_all)

6. Advanced features of notifications

Using the SetStyle () method , you can pass in the notification long text and pictures and other content

Pass in long text: Pass the Setsyle method, in the parameter, use the Bigtextstyle method in the Notificationcompat class, pass in the long text

1 . SetStyle (new Notificationcompat.bigtextstyle (). BigText ("aaaaaaaaaaaaaaaaaaaaa  " +2                                  "  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +3                                  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) 4 . Build ();

Incoming picture: Through the Setsyle method, the parameter, using the Bigpicturestyle method in the Notificationcompat class, passes in the picture. It uses the Decoderesource method in the Bitmapfactory class to convert the picture to bitmap format.

1 . SetStyle (new  Notificationcompat.bigpicturestyle (). BigPicture ( Bitmapfactory.decoderesource (Getresources (), R.drawable.big_image))2 . Build ();

To set notification permissions:

. SetPriority (Notificationcompat.priority_max)

There are five types of parameters passed in as follows: corresponding to different weights.

7. Example

A button is used to send notifications, and you can jump to another activity by notification. In addition, the key trigger in this instance is written in a different switch mode.

1  Public classMainactivity extends Appcompatactivity implements View.onclicklistener {2 3 @Override4     protected voidonCreate (Bundle savedinstancestate) {5 super.oncreate (savedinstancestate);6 Setcontentview (r.layout.activity_main);7 8Button Sendnotice =(Button) Findviewbyid (r.id.send_notice);9Sendnotice.setonclicklistener ( This);Ten     } One  A  - @Override -      Public voidOnClick (view view) { the         Switch(View.getid ()) { -              CaseR.id.send_notice: -                 /*Create a time-lapse intent pendingintent,getactivity pass in four parameters, - * After the new notice also to setcontentintent this delay intent*/ +Intent Intent =NewIntent ( This, Notificationacitivity.class); -pendingintent pi = pendingintent.getactivity ( This,0, Intent,0); +                 /*get to a notificationmanager to manage notifications, A * Using the Getsystemservice method, passing in a service, returning a manager*/ atNotificationmanager Manager =(Notificationmanager) Getsystemservice (notification_service); -Notification Notification =NewNotificationcompat.builder ( This) -. Setcontenttitle ("This is content title")//set the notification title -. Setcontenttext ("This is content text")//Set notification content -. Setwhen (System.currenttimemillis ())//set the time when the notification was created -. Setsmallicon (R.mipmap.ic_launcher)//set Small icons for notifications in. Setlargeicon (Bitmapfactory.decoderesource (Getresources (), R.mipmap.ic_launcher))//set Large icons for notifications -. Setcontentintent (PI)//set the content of the notification the jump parameter is a pendingintent to. Setautocancel (true)//set reminders to automatically cancel when clicked + . Build (); -Manager.notify (1, notification);//each notification corresponds to an ID the                  Break; *             default: $                  Break;Panax Notoginseng         } -     } the}

Android Phone multimedia--Notification

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.