Basic usage of notification

Keywords: Android MediaPlayer iOS Mobile


Notification is a special function of Android system. When an application needs to send a reminder to users, it can be realized by notification. Most Android users like this function. Even IOS has added similar functions after version 5.0

1. You need a notification manager to manage notifications. You can call Content's getSystemService() method to get it. The getSystemServrce method accepts a string parameter to determine the service that gets the system. Here we pass in Content.NOTIFICATION_SERVRCE, which can be written as follows

NofiticationManager manager = (NotificationManager)
//To run on all Android versions, add the following code:
Nofitication nofitication = new NotificationCompat.Builder(content).bulid;

2. Basic settings of Notification

Nofitication nofitication = new NotificationCompat.Builder(content)
.setContentTitle("Set the title here")
.setContentText("Here is the main content")
.setWhen(System.currentTimeMillis())//Display time is set here
.setSmallIcon(R.drawable.Small_icon)//Set small icons here
.setAutoCancel(true)//Cancel display

3. Show notifications

manager.notify(1,notification);//The first parameter is id, and the second parameter is Notification object

4. Advanced techniques of notification

.setSound(Uri.frommFile()new File("Route"))
.setVibrate(new long[]{0,1000,1000,1000})//Subscript 0 is the static duration, subscript 1 is the vibration duration, subscript 2 is the static duration, and so on. You need to declare permission to control the vibration of the mobile phone
<uesr-permission android:name = "android.permission.VIBRATE"/>
.setLights(Color.GREEN,1000,1000)//Set the color of the LED light, the second parameter sets the time when it is on, the third parameter sets the time when it is dark, and the notice comes
//Time is a flash effect
.setDefaults(NotificationCompat.DEFAULT_ALL)//default setting

5. Advanced features of notifications

.setContentText("When the text content is too much and the notice column cannot be displayed, the extra part is replaced by an ellipsis")
.setStyle(new NotificationCompat.BigTextStyle().bigText("So you can show it all"))
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.big_image)))//Set display picture

6. Importance of notice

//Five values in all
 Priority? Default
 Priority min
 Low priority
 High priority
 Priority? Max

Start up camera

Intent intent = new Intent("");

Open Album

Intent intent = new Intent("android.intent.action.GET_CONTENT");

Play audio

    File file = new File(Environment.getExternalStorageDirectory(),"file name.Extension name");
    mediaPlayer.setDataSource(file.getPath());//specify the path to a file
    mediaPlayer.prepare();//Make mediaPlayer ready
    mediaPlayer.start();//Start playing
    mediaPlayer.pause();//Pause playback
    mediaPlayer.reset();//stop playing
}catch(Exception e){

Play video

File file = new File(Environment.getExternalStorageDirectory(),"file name.Extension name");
    videoView.start();//Start playing
    videoView.pause();//Pause playback

You need permission to play both video and audio

<user - permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE"/>

Posted by stbalaji2u on Sun, 26 Jan 2020 09:59:40 -0800