Pro Android学习笔记(九八):BroadcastReceiver(2):notification

文章转载只能用于非商业性质,且不能带有虚拟货币、积分、注册等附加条件。转载须注明出处:http://blog.sina.com.cn/flowingflying或作者@恺风Wei-傻瓜与非傻瓜

广播接受可用于本地,也可以用于不同的进程(应用)间。广播还常用于后台服务,当接收器收到某个广播消息时,通常会在通知栏中提示用户,用户点击通知,可以进入某个Activity中进行处理。

小例子

接收器应用为本小例子,发送广播应用利用上一学习的小例子。

代码

关于通知,可以参考Android学习笔记(五五):通知Notification(下)

public class NotificationReceiver extendsBroadcastReceiver{
    privatestatic int NOTIFY_ID = 1000; 
    private static final String tag ="NotificationReceiver";
    @Override
    public voidonReceive(Context context, Intent intent) { 
       Utils.logThreadSignature(tag);
       Log.d(tag,"intent = " + intent);

       
       String message = intent.getStringExtra("message");
       Log.d(tag,message);
       sendNotification(context,message);
    }
   
    private voidsendNotification(Context context, String message){
       //【1】获取Notification管理器的参考 
       NotificationManager notifyMgr=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);         
       //【2】设置通知。PendingIntent表示延后触发,是在用户下来状态栏并点击通知时触发,触发时PendingIntent发送intent,本例为打开浏览器到指定页面。
           
       Intent intent = new Intent(Intent.ACTION_VIEW);
       intent.setData(Uri.parse("http://www.google.com"));
       PendingIntent pi = PendingIntent.getActivity(context, 0, intent,0);         
       Notification notification = new Notification.Builder(context)
                                   .setSmallIcon(R.drawable.ic_launcher)
                                   .setTicker("Hello")
                                   .setContentTitle("Title")
                                   .setContentText("Content text")
                                   .setContentIntent(pi)
                                   .build();
       notification.flags |= Notification.FLAG_AUTO_CANCEL; //点击后删除,如果是FLAG_NO_CLEAR则不删除,FLAG_ONGOING_EVENT用于某事正在进行,例如电话,具体查看参考。 
       
//【3】发送通知到通知管理器。第一个参数是这个通知的唯一标识,通过这个id可以在以后cancel通知,更新通知(发送一个具有相同id的新通知)。这个id在应用中应该是唯一的。
       notifyMgr.notify(NOTIFY_ID, notification);
    }
}

XML

在AndroidManifest.xml中同样要进行receiver的声明,标明所关注的广播。我们的小例子无需有activity,只需receiver即可。在安装是会显示:

由于在manifest中以告知系统所关心的广播,无需有一个App正在运行,同样也可以正确地实现触发。

创建自己的ContentView风格:RemoteView

上面我们使用了系统缺省的Content View。有时我们希望自行定义contentview的风格,如图所示,由一个Textview和一个Button组成。

我们首先定义自己的layout,在res/layout/中加入相关的xml文件,本例为content_view.xml,如下:

相关的notification生成代码如下:

private void sendNotification2(Context context, Stringmessage){ 
    NotificationManager notifyMgr=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
               
   Intent intent = new Intent(Intent.ACTION_VIEW);
   intent.setData(Uri.parse("http://www.google.com"));
   PendingIntent pi = PendingIntent.getActivity(context, 0, intent,0);
 
    
    //创建RemoteViews对象。参数1为包名,参数2为对应的layout文件ID。然后设置remoteViews中的text,icon等等。home page中的widget views也是remoteviews,我们将在以后学习。 
    RemoteViewsremoteViews = newRemoteViews(context.getPackageName(),R.layout.content_view);
   //对当中的TextView进行设置
   remoteViews.setTextViewText(R.id.title, "My Custom Title");
   remoteViews.setTextColor(R.id.title, Color.RED);
   //对当中的非TextView,例如Button进行设置,参数2对应方法名称,本例button.setText(),因此取“setText”,参数3对应传递到button.setText(value)中的value。
   remoteViews.setCharSequence(R.id.button, "setText", "My customcontent text");
   
    Notification notification = newNotification.Builder(context)
                               .setSmallIcon(R.drawable.ic_launcher)
                               .setTicker("Hello")

                               .setContent(remoteViews) //在notification中设置content view
                               .setContentIntent(pi)
                               .build();

   notification.flags |=Notification.FLAG_AUTO_CANCEL; 
   notifyMgr.notify(NOTIFY_ID + 1, notification);

}

打开Activity

前面,我们使用了通知管理器,实际上我们可以在收到广播时直接通过startActivity开启activity,但要带有下面的flag:Intent.FLAG_ACTIVITY_NEW_TASK,Inetent.FLAG_FROM_BACKGROUND,或者Intent.FLAG_ACTIVITY_SINGLETOP。

小例子代码在:Pro Android学习:Broadcast小例子

相关链接: 我的Android开发相关文章

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值