ANDROID Notification notification Use instance (vibration, lighting, sound) _android

Source: Internet
Author: User

This article illustrates the use of notification notification, This knowledge point is used as a notice of the display, including vibration, lighting, sound and other effects, share for everyone to reference, the specific content as follows

Effect Chart:



Mainactivity:

Import Java.io.File; 
Import android.app.Activity; 
Import android.app.Notification; 
Import Android.app.NotificationManager; 
Import android.app.PendingIntent; 
Import android.content.Intent; 
Import Android.graphics.Color; 
Import Android.net.Uri; 
Import Android.os.Bundle; 
Import Android.view.View; 
Import Android.view.View.OnClickListener; 
 
Import Android.widget.Button; 
 
 public class Mainactivity extends activity implements Onclicklistener {private Button sendnotice; 
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
  Setcontentview (R.layout.activity_main); 
  Sendnotice = (Button) Findviewbyid (R.id.send_notice); 
 Sendnotice.setonclicklistener (this); @Override public void OnClick (View v) {switch (V.getid ()) {case R.id.send_notice:notificationmanager 
 
   Manager = (Notificationmanager) getsystemservice (Notification_service); Create a notification object to store the various information required for the notification//The first parameter is the icon//second parameter used to specify the notificationTicker content///The third parameter is used to specify when the notification was created, in milliseconds Notification Notification = new Notification (R.drawable.ic_launcher, " 
 
   This is ticker text ", System.currenttimemillis ()); The Jump///The first parameter of the activity you click here is still the context//the second parameter is generally not available, so use 0 for the default value//The third parameter is a intent object//flag_cancel_current: 
   There is already an identical Pendingintent object in the current system,//Then the existing pendingintent is canceled and a Pendingintent object is regenerated. 
   Intent Intent = new Intent (this, notificationactivity.class); 
 
   pendingintent pi = pendingintent.getactivity (this, 0, intent, pendingintent.flag_cancel_current); Set the layout of the notification//The first parameter is the context//second parameter used to specify the caption of the notification//The third parameter is used to specify the essay content of the notification//The fourth parameter is used to pass in the Pendingintent object for setting the Click Effect Noti 
 
Fication.setlatesteventinfo (This, ' This is content title ', ' is content text ', pi); 
Set the audio//Uri Sounduri = Uri.fromfile (New File ("/system/media/audio/ringtones/basic_tone.ogg") when the notification is issued; 
Notification.sound = Sounduri; //Set mobile phone vibration////First, 0 means the cell phone is still long, the second, 1000 means the mobile phoneVibration of the Duration///Third, 1000 indicates the cell phone vibration of the time, the fourth, 1000 means the mobile phone vibration of the duration////Here is the mobile phone vibration 1 seconds, then still 1 seconds, and then vibrate 1 seconds//long[] vibrates = {0, 1000, 100 
0, 1000}; 
Notification.vibrate = vibrates; 
//Set LED blinking////ledargb Set color/////LEDONMS specified LED light time////LEDOFFMS specified LED lights dark time////flags the behavior used to specify notifications 
Notification.ledargb = Color.green; 
NOTIFICATION.LEDONMS = 1000; 
NOTIFICATION.LEDOFFMS = 1000; 
 
   Notification.flags = notification.flag_show_lights; 
 
   If you do not want to do so many complicated this only, you can directly use the default effect of the notification//default settings of the sound, vibration and lighting notification.defaults = Notification.default_all; 
   Use notify to display notifications//The first parameter is ID, to explode the ID specified for each notification is different//the second parameter is the notification object Manager.notify (1, notification); 
  Break 
  Default:break; 
 } 
 } 
 
}

Activity_main:

<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:orientation= "vertical" > 
 
 <button 
  android:id= "@+id/send_notice" 
  android:layout_ Width= "Match_parent" 
  android:layout_height= "wrap_content" 
  android:text= "give notice" 
  /> 
  

Notificationactivity:

Import android.app.Activity; 
Import Android.app.NotificationManager; 
Import Android.os.Bundle; 
 
public class Notificationactivity extends activity { 
 
 @Override 
 protected void OnCreate (Bundle Savedinstancestate) { 
  super.oncreate (savedinstancestate); 
  Setcontentview (r.layout.notification_layout); 
 
  Open Notificationactivity This activity and turn the notice off 
  notificationmanager manager = (Notificationmanager) getsystemservice (Notification_service); 
  Manager.cancel (1); 
 } 
 
} 

Notification_layout:

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= 
"http://schemas.android.com/apk" /res/android " 
 android:layout_width=" match_parent " 
 android:layout_height=" match_parent "> 
  
 < TextView 
  android:layout_width= "wrap_content" 
  android:layout_height= "wrap_content" 
  android:layout_ Centerinparent= "true" 
  android:textsize= "24sp" 
  android:text= "This is the notification-clicked interface" 
  /> 
  

Androidmanifest:

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= 
"http://schemas.android.com/apk/res/" Android " 
 package=" com.example.notificationtest " 
 android:versioncode=" 1 " 
 android:versionname=" 1.0 " > 
 
 <uses-sdk 
  android:minsdkversion= "android:targetsdkversion=" 
  /> 
 
 < Application 
  android:allowbackup= "true" 
  android:icon= "@drawable/ic_launcher" 
  android:label= "@ String/app_name " 
  android:theme=" @style/apptheme "> 
  <activity 
   android:name=" Com.example.notificationtest.MainActivity " 
   android:label=" @string/app_name "> 
   <intent-filter > 
    <action android:name= "Android.intent.action.MAIN"/> 
 
    <category android:name= " Android.intent.category.LAUNCHER "/> 
   </intent-filter> 
  </activity> 
  <activity Android:name= ". Notificationactivity "> 
  </activity> 
 
 </application> 
 

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.