Notification:系统通知
notification与broadcastreciver结合使用
用系统的布局来提示
步骤:
首先创建一个类(接收器)MyReceiver继承BroadcastReceiver重写onReceive方法在方法里
需要得到通知的管理(NotificationManger)
NotificationManger manager=(NotificationManger)context.getSystemService(Context.NOTIFICATION_SERVICE);
创建一个通知
Notification notification=new Notification(提示图片,提示文字,提示时间);
Notifacation.flafs|Notification.FLAG_AUTO_CANCEL;//点开之后让其消失
设置事件
需要pendingIntent等待意图而pedingintent 需要一个intetnt
所以我们要创建一个意图
Intent intetnt=new Intent(this,other.class);
PendingIntent pendingIntent=PendingIntent.getActivity(context,0,intent,)
设置最新的提示消息 参数一是一个context的对象-----》Context 是描述应用程序环境的信息 提示的信息 还有等待意图
通过notification.setLatestEventinfo(context, "飓风出品必数经典", "欢迎使用", pendingIntent);
然后发送
manager.notify(1, notification);
manger.cancel(1);关闭当前通知
manger.cancelAll;关闭所有通知
必须注册方式有两种
、一:
清单文件(Android.Mainfest.xml)中注册与注册activity同级
<receiver android:name="com.example.notifiaction.MyReciver">
<intent-filter >意图过滤器
<action android:name="android.intent.action.AIRPLANE_MODE"/>当系统是飞行模式的时候发送广播 接收器里接收后执行相应的代码
</intent-filter>
</receiver>
二:
在activity中注册
首先你要创建一个你自定义的接收器的实例
MyReceiver receiver=new MyReceiver()
创建一个意图过滤器
IntentFilter filter=new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
注册
registerReceiver(reciver, filter);
在activity中注册必须在ondestory方法中解除注册
unregisterReceiver(reciver);
自定义布局来实现提示
步骤:
创建一个类MyReceiver来继承BroadcastReceiver重写onReceive方法在方法里
首先要拿到通知的管理类(NotificationManger类)
NotificationManger manger=(NotificationManger)context.getSystemService(Context.NOTIFICATION_SERVICE);
创建一个通知
Notification notification=new Notification(提示图片,提示文字,提示时间);时间一般写getSystemCurrentTime();当前系统时间
--------------------------------------------------------------->包名--------------------------------------》布局
RemoviteView contentView=new RemoviteVIew(context.getPackageName(), R.layout.notification);
contentView.setTextViewText(R.id.textView1, "飓风出品");//设置好布局上的控件要显示的信息
contentView.setTextViewText(R.id.textView2, "welcome");
//将removiteView设置给notification.contentView();
notification.contentView=contentView;//需要一个removiteView所以我们需要在上面写一个removiteVIew
Intent intent2=new Intent(context, MainActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(context, 0, intent2, 0);
notification.contentIntent=pendingIntetn;需要一个pendingIntent(等待意图)
最后发送
manager.notify(0, notification);
注册方式也有两种
像第一种一样一种在清单文件中注册一种在activity中注册
也可以发送一个通知然后再清单文件中注册
public void showNotification(){
manager=(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
//创建通知
Notification notification=new Notification(R.drawable.ic_launcher, "音乐播放", System.currentTimeMillis());
////设置提示音
//notification.defaults(de)
//设置是否消除
notification.flags|=Notification.FLAG_ONGOING_EVENT;//必须自己消除
//设置布局
RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.notification);
//设置开始按钮的监听
PendingIntent intent=PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent("com.music.play"), 0);
remoteViews.setOnClickPendingIntent(R.id.bu1, intent);
//设置暂停
PendingIntent intent_pause=PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent("com.music.pause"), 0);
remoteViews.setOnClickPendingIntent(R.id.bu2, intent_pause);
//设置退出
PendingIntent intent_exit=PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent("com.music.exit"), 0);
//将remoteVIew设置给notification
notification.contentView=remoteViews;
//给notiffication设置意图
Intent intent_noti=new Intent(getApplicationContext(),MainActivity.class);
//设置启动模式
intent_noti.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pending=PendingIntent.getActivity(getApplicationContext(), 0, intent_noti, 0);
notification.contentIntent=pending;
manager.notify(1, notification);
注册
<receiver android:name="com.example.android_servicessimplemusic.MyMusicService$reciver">
<intent-filter >
<action android:name="com.music.play"/>
<action android:name="com.music.pause"/>
<action android:name="com.music.exit"/>
</intent-filter>
</receiver>