Home>
Effect picture:
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 various information needed for notifications
//The first parameter is the icon
//The second parameter is used to specify the ticker content of the notification
//The third parameter is used to specify the time when the notification is created,In milliseconds
notification notification=new notification (
r.drawable.ic_launcher, "this is ticker text", system.currenttimemillis ());
//Set the jump of clicked activity here
//The first parameter is still context
//The second parameter is generally not used,So use 0 to take the default value
//The third parameter is an intent object
//flag_cancel_current:If an identical pendingintent object already exists in the current system, //Then the existing pendingintent will be cancelled first, and then a pendingintent object will be 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 context
//The second parameter is used to specify the title of the notification
//The third parameter is used to specify the content of the essay for notification
//The fourth parameter is used to pass in the pendingintent object, which is used to set the click effect
notification.setlatesteventinfo (this, "this is content title", "this is content text", pi);
////Set the audio when the notification is issued
//uri sounduri=uri.fromfile (new file ("/system/media/audio/ringtones/basic_tone.ogg"));
//notification.sound=sounduri;
//
////Set the phone to vibrate
////The first one, 0 means how long the phone is at rest,The second one, 1000 means how long the phone vibrates
////The third one, 1000 means the duration of the phone vibration,The fourth, 1000 is the duration of the phone vibration
////This means the phone vibrates for 1 second, then stands still for 1 second, and then shakes for 1 second
//long [] vibrates={0, 1000, 1000, 1000};
//notification.vibrate=vibrates;
//
////Set the blinking of the led indicator
////ledargb sets the color
////ledonms specifies the time that the led light is on
////ledoffms specifies the time that the led lights go out
////flags are used to specify the behavior of the notification
//notification.ledargb=color.green;
//notification.ledonms=1000;
//notification.ledoffms=1000;
//notification.flags=notification.flag_show_lights;
//If you don't want to do so many complicated ones,Can use the default effect of notifications directly
//The sound is set by default,Vibration and light
notification.defaults=notification.default_all;
//Use notify to show the notification
//The first parameter is the id. 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="Send notification"
/>
</linearlayout>
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);
//Turn notification activity off after opening notification activity
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 interface after notification click"
/>
</relativelayout>
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="14"
android:targetsdkversion="19" />
<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>
</manifest>
Related articles
- Android use NotificationListenerService to listen to notification bar messages
- Android Notification Code Notification Example
- Notification pop-up notification implementation code in Android
- Use of Android notification usage rights (NotificationListenerService)
- Android custom notification add click event
- Notification prompt dialog in Android
- Android uses Notification to implement notification management and custom notification bar instances (example 4)
- Android uses Notification to display notification on status bar
- Analysis of Example of Notification Mobile Status Bar Notification Usage in Android Development
Trends
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- python - you may need to restart the kernel to use updated packages error
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- python 3x - typeerror: 'method' object is not subscriptable
- javascript - how to check if an element exists in puppeteer
- xcode - pod install [!] no `podfile 'found in the project directory
- i want to call a child component method from a parent in vuejs
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning