<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "Horizontal"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:gravity= "Center_horizontal" ><ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Send Notification"Android:onclick= "Send" /><ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Delete Notification"Android:onclick= "Del" /> </LinearLayout>
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:gravity= "Center_horizontal"android:orientation= "vertical" ><!--Define a ImageView -<ImageViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:src= "@drawable/java"android:layout_gravity= "Center_horizontal" /></LinearLayout>
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.notification"Android:versioncode= "1"Android:versionname= "1.0" > <USES-SDKandroid:minsdkversion= "+"android:targetsdkversion= "+" /> <!--add permission to operate the Flash - <uses-permissionAndroid:name= "Android.permission.FLASHLIGHT" /> <!--permission to add an action vibrator - <uses-permissionAndroid:name= "Android.permission.VIBRATE" /> <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <ActivityAndroid:name= "Com.example.notification.MainActivity"Android:label= "@string/app_name" > <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity> <ActivityAndroid:name=". Otheractivity "Android:label= "@string/other_activity"> </Activity> </Application></Manifest>
When you click Notification, implement Otheractivity
Packagecom.example.notification;Importandroid.app.Activity;Importandroid.app.Notification;ImportAndroid.app.NotificationManager;Importandroid.app.PendingIntent;Importandroid.content.Intent;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.view.View; Public classMainactivityextendsActivity {Static Final intNotification_id=0x123; Notificationmanager nm; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); NM=(Notificationmanager) Getsystemservice (Notification_service); } //defines an event-handling method for the Click event of a button that sends notifications Public voidSend (View source) {//Create a intent that initiates other activityIntent Intent =NewIntent (mainactivity. This, otheractivity.class); Pendingintent Pi=pendingintent.getactivity (mainactivity. This, 0, intent, 0); Notification Notify=NewNotification.builder ( This) //set to turn on the notification, the notification disappears automatically. Setautocancel (true) //set up a notification prompt that appears in the status bar. Setticker ("New News") //to set the icon for a notification. Setsmallicon (r.drawable.notify)//set the title of the notification content. Setcontenttitle ("A new notice") //Set notification content. Setcontenttext ("Congratulations, you've got a raise, you're paying 20% more!")) // //set Use system default sound, default LED light//. SetDefaults (Notification.default_sound// | Notification.default_lights)//set a custom sound for notifications. Setsound (Uri.parse ("android.resource://com.example.notification/" +r.raw.mylove1)). Setwhen (System.currenttimemillis () )//change notification to start the program intent. Setcontentintent (PI). build (); //Send NotificationsNm.notify (notification_id, notify); } //defines the event handling method for the Click event of the button that deletes the notification Public voiddel (View v) {//Cancel NotificationNm.cancel (notification_id); } }
package com.example.notification; import android.app.Activity; import Android.os.Bundle; public class otheractivity extends activity{@Override
public void OnCreate (Bundle savedinstancestate) { super< /span>.oncreate (Savedinstancestate); // Set the page that the activity displays Setcontentview (r.layout.other); }}
Android 33rd Lesson--notification