用来通过发生的事件提醒用户,出现一个状态条,可以手机背光闪烁,还可以发出声音或震动,当用户选择时候可以启动到app。
android.app.NotificationManager;
android.support.v4.app.NotificationCompat;
常用个方法:
cancel(int id) 取消之前显示出来的notification ,id用用来识别哪个消息。
cancel(String tag, int id)
cancelAll() 取消所有的显示的notification
notify(int id, Notification notification)把一个notification) 展示到状态条
notify(String tag, int id, Notification notification)
例如:
NotificationManager manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification=new NotificationCompat.Builder(this)
.setContentTitle("消息通知来了")
.setContentText("消息通知的内容")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher_round))
.build();
manager.notify(1,notification);
实例:状态栏通知条控制音乐播放
给notification添加视图并且绑定事件,PendingIntent这个是可以延时的意图,然后点击状态栏的notification中的按钮就能发送信息到广播接收器。广播接收器控制后台进程控制音乐的播放状态改变。
状态栏的notification 与服务通信---》服务控制音乐在后台的播放状态的改变。
package com.lht.liuhaitao;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RemoteViews;
import android.widget.TextView;
public class MainActivity extends Activity {
static final String TAG="MainActivity";
private Button but;
MusicService.MusicBinder binder=null;
ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
binder= (MusicService.MusicBinder) service;
//存储为全局对象,共享给MusicBroadcastReceiver中使用
MyApplication.musicBinder=binder;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but=findViewById(R.id.but_ok);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,MusicService.class);
intent.putExtra("action","play");
startService(intent);
bindService(intent,connection,BIND_AUTO_CREATE);
notification();
if(binder!=null){
binder.play();
}
}
});
}
private void notification(){
NotificationManager manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
MyApplication.notificationManager=manager;//就为了让它变为全局,方便用。
RemoteViews remoteView= new RemoteViews(getPackageName(),R.layout.controll_music);
Notification notification=new NotificationCompat.Builder(this)
.setContentTitle("消息通知来了")
.setContentText("消息通知的内容")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setContent(remoteView)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher_round))
.build();
//播放
Intent intentPlay=new Intent(this,MusicService.class);
intentPlay.putExtra("action","play");
PendingIntent pendingIntent=PendingIntent.getService(this,1,intentPlay,PendingIntent.FLAG_UPDATE_CURRENT);
remoteView.setOnClickPendingIntent(R.id.play,pendingIntent);
//暂停
Intent intentPause=new Intent(this,MusicService.class);
intentPause.putExtra("action","pause");
PendingIntent pendingIntentPause=PendingIntent.getService(this,2,intentPause,PendingIntent.FLAG_UPDATE_CURRENT);
remoteView.setOnClickPendingIntent(R.id.pause,pendingIntentPause);
//显示消息控件
manager.notify(1,notification);
}
}
controll_music.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/ic_launcher"
android:text="播放"/>
<ImageView
android:background="@mipmap/ic_launcher"
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停"/>
<ImageView
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/ic_launcher"
android:text="停止"/>
<ImageView
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/ic_launcher"
android:text="取消"/>
</LinearLayout>
package com.lht.liuhaitao;
import android.app.Service;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.MediaStore;
import android.util.Log;
import java.io.File;
public class MusicService extends Service {
MusicBinder binder=new MusicBinder();
public MusicService(){
}
public class MusicBinder extends Binder{
MediaPlayer player=null;
public void play(){
//Uri musicTableForSD = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
//Uri uri = Uri.withAppendedPath(musicTableForSD,"/" + "音乐名字");
//Uri uri=Uri.fromFile(new File("file:///android_assets/1.mp3"));
if(player==null ){
player= MediaPlayer.create(getApplication(),R.raw.a);
}
if(!player.isPlaying()){
player.start();
}
}
public void stop(){
if(player!=null && player.isPlaying()){
player.stop();
player.stop();
player.release();
player=null;
}
}
public void pause(){
if( player!=null && player.isPlaying()){
player.pause();
}
}
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
@Override
public void unbindService(ServiceConnection conn) {
super.unbindService(conn);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String action=intent.getStringExtra("action");
switch (action){
case "play":
binder.play();
break;
case "pause":
binder.pause();
break;
}
Log.e("wwww", "onStartCommand: "+action+""+binder );
return super.onStartCommand(intent, flags, startId);
}
}
package com.lht.liuhaitao;
import android.app.Application;
import android.app.NotificationManager;
import android.content.Context;
public class MyApplication extends Application {
private static Context context;
public static MusicService.MusicBinder musicBinder;
public static NotificationManager notificationManager;
public MyApplication(){
context=getApplicationContext();
}
public static Context getContext(){
return context;
}
}