通知和广播

来源:互联网 发布:网络直播平台土豪排名 编辑:程序博客网 时间:2024/06/05 23:07




1,通知
>在屏幕之外  展示给用户信息


1,普通的通知


             //获取通知对象
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);

//必有属性  
builder.setContentTitle("标题");//标题
builder.setContentText("内容hgcdakhgflasfgldsafgldguli iueroewyurhwehfldiufhlaskjfglsadfglksFGLSdjgflasdgflahgjkfdshg;hg;efgh");//内容
builder.setSmallIcon(R.drawable.image1);//图标
//其他属性
builder.setWhen(System.currentTimeMillis());//发送通知的时间
Bitmap bp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
builder.setLargeIcon(bp);
builder.setContentInfo("信息");


Intent intent = new  Intent(MainActivity.this, SecondActivity.class);
/**

* 延迟意图   等待你的点击 
* 参数1:上下文
* 参数2:请求码
* 参数3:跳转的意图
* 参数4:标记
*/
PendingIntent pendIntent = PendingIntent.getActivity
(MainActivity.this, 100, intent, PendingIntent.FLAG_ONE_SHOT);

builder.setContentIntent(pendIntent);

//获取通知的管理者对象
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


//使用管理者让通知展示
manager.notify(1, builder.build());

2,大视图通知  使用样式


            NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setContentText("快放假了");
builder.setContentTitle("十一");
builder.setSmallIcon(R.drawable.f020);

//视图样式
NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
style.addLine("还有好多天");
style.addLine("等等就到了");
style.addLine("是啊");
style.addLine("恩");

builder.setStyle(style);

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(2, builder.build());





                 

               NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setContentTitle("新闻");
builder.setContentText("中彩票了");
builder.setSmallIcon(R.drawable.f020);

//大图片 视图样式
NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle();
//大图片
style.bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.ccc));
style.setSummaryText("菜谱");
builder.setStyle(style);


NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(3, builder.build());


    

3,带进度条的通  明确的进度和模糊的进度


         

                 final NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setContentTitle("昨天晚上下雨了");
builder.setContentText("早上起来 地干了");
builder.setSmallIcon(R.drawable.f020);

final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);


//模仿下载
new Thread(){
public void run() {
//每次下载5%
for(int i=0;i<=100;i+=5){
/**
* 设置进度 
* 参数1:最大的进度
* 参数2:当前进度
* 参数3:标志    进度条的样式    false :有明确进度    true:没有明确进度
*/
builder.setProgress(100, i, false);
manager.notify(4, builder.build());

try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//下载完展示
builder.setContentText("下载完成");
manager.notify(4, builder.build());
};
}.start();

           

4,自定义的通知



              NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());

builder.setContentTitle("疯狗咬人");
builder.setSmallIcon(R.drawable.ic_launcher);
               //
//VIew  将xml布局转换成View对象
RemoteViews rViews = new RemoteViews(getPackageName(), R.layout.rviews_layout);
//自身的方法设置内容
rViews.setTextViewText(R.id.tv, "咬了十几个");
rViews.setImageViewResource(R.id.iv, R.drawable.f020);
builder.setContent(rViews);


NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(5, builder.build());

              一般使用在:消息推送 


2,广播接收者   BroadcastReceiver  


>1,定义  可以接收某一频道(action) 发送的广播  ,发送者 Activity,Service


>2,作用:
>监听系统的广播进行处理  :电量过低 进行提取 网络状态


系统广播:
1,电量的:Intent.ACTION_BATTERY_CHANGED
2,网络的
3,拨打电话的:android.intent.action.NEW_OUTGOING_CALL
4,短信的:android.provider.Telephony.SMS_RECEIVED
5,打电话的状态:android.intent.action.PHONE_STATE
5,....
   
>自定义广播 :在服务中下载数据  使用广播进行发送  


>3,使用:
>1,创建一个class 继承BroadCastReceiver
>2,重写父类里的方法 onReceiver()
>3,注册广播
>静态注册:在清单文件进行注册
>特点:不管程序是否活动  都可以进行监听
>动态注册:在逻辑代码中注册
>一般在OnResume()里进行注册  在OnPause()方法 取消注册  
registerReceiver(myBroadCastReceiver02, intentFilter);
特点:程序活动时  才进行监听 
>4,分类

//普通广播   不能中断
sendBroadcast(intent);
//有序广播   可以中断   abortBroadcast();//中断广播
sendOrderedBroadcast(intent, null);


粘性广播:广播一直存在消息容器里  直到有接收者处理广播(弃用)




>5,广播接收者注意事项:
1,广播接受者的生命周期10s,在接收广播时创建  在onReceiver()执行后销毁
2,在广播接收者内不能做耗时操作   ,在主线程执行
3,在广播接收者内不能开启子线程  ,在广播结束后 ,创建的线程容易变成 空线程,很容易被程序回收
4,耗时操作让服务来执行
0 0