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

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

小例子

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

代码

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

public class NotificationReceiver extends BroadcastReceiver{
    private static int NOTIFY_ID = 1000; 
    private static final String tag = "NotificationReceiver";
    @Override
    public void onReceive(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 void sendNotification(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正在运行,同样也可以正确地实现触发。

创建自己的Content View风格:RemoteView

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

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

相关的notification生成代码如下:

private void sendNotification2(Context context, String message){ 
    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也是remote views,我们将在以后学习。 
    RemoteViews remoteViews = new RemoteViews(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 custom content text");
   
    Notification notification = new Notification.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。

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

转自:http://blog.csdn.net/flowingflying/article/details/6212512

【转】Pro Android学习笔记(九八):BroadcastReceiver(2):接收器触发通知的更多相关文章

  1. 【转】 Pro Android学习笔记(八二):了解Package(1):包和进程

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 在之前,我们已经学习了如何签发apk,见P ...

  2. 【转】 Pro Android学习笔记(八九):了解Handler(3):延迟执行小例子

    目录(?)[-] 小例子 Handler的处理 Activity的代码片段 后台线程和UI的互动 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://b ...

  3. 【转】 Pro Android学习笔记(八十):服务(5):访问远程服务

    目录(?)[-] Client的AIDL文件 Client的代码 建立连接 请求服务 断开连接 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://bl ...

  4. 【转】 Pro Android学习笔记(八八):了解Handler(2):什么是Handler

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 之前我们有一篇很好的博文<Andro ...

  5. 【转】 Pro Android学习笔记(八六):了解Package(5):使用lib

    目录(?)[-] 在项目中使用lib 源代码 了解一些机制 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowin ...

  6. 【转】 Pro Android学习笔记(八四):了解Package(3):包间数据共享

    目录(?)[-] 共享User ID的设置 共享资源例子 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowing ...

  7. 【转】 Pro Android学习笔记(八五):了解Package(4):lib项目

    目录(?)[-] 什么是lib项目 小例子 Lib的实现 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowing ...

  8. 【转】 Pro Android学习笔记(八三):了解Package(2):包签名过程

    目录(?)[-] 类比例子 数字签名 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 在W ...

  9. 【转】 Pro Android学习笔记(八七):了解Handler(1):组件和线程

    目录(?)[-] 主线程消息队列和线程池 查看线程信息 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingf ...

  10. 【转】Pro Android学习笔记(八):了解Content Provider(下中)

    在之前提供了小例子BookProvider,我们回过头看看如何将通过该Content Provider进行数据的读取. (1)增加 private void addBook(String name , ...

随机推荐

  1. Django REST framework快速入门指南

    项目设置 创建一个名为tutorial的新Django项目,然后开始一个名为quickstart的新应用程序. # Create the project directory mkdir tutoria ...

  2. JNIjw01

    1.VC6(CPP)的DLL代码: #include<stdio.h> #include "jniZ_JNIjw01.h" JNIEXPORT void JNICALL ...

  3. ceph 测试

    FIO用法: 随机读: fio -filename=/dev/sdb1 -direct=1 -iodepth 1 -thread -rw=randread -ioengine=psync -bs=16 ...

  4. mysql 出现Host 'localhost' is not allowed to connect to this MySQL server 错误

    MySql数据库:Host 'localhost' is not allowed to connect to this MySQL server 修改mysql的root密码后,出现Host 'loc ...

  5. 【LABVIEW到C#】3》String的操作之Match Pattern Funtion.vi

    C#实现如下 using System; using System.Collections.Generic; using System.Text; using System.Text.RegularE ...

  6. C++程序设计之提高效率

    设计C++程序时,总结起来可以从如下几点提高效率: 1.并发 2.异步 3.缓存

  7. 【scala】模式匹配

    Scala的模式匹配是通过match表达式从若干可选项中选择,类似Java中的switch. 例子: val firstArg = if(args.length>0) args(0) else ...

  8. ZOJ 3488 Conic Section

    The conic sections are the nondegenerate curves generated by the intersections of a plane with one o ...

  9. kalman 滤波 演示与opencv代码

    在机器视觉中追踪时常会用到预测算法,kalman是你一定知道的.它可以用来预测各种状态,比如说位置,速度等.关于它的理论有很多很好的文献可以参考.opencv给出了kalman filter的一个实现 ...

  10. 利用struts2的json返回方式来控制jquery.validate的remote框架,进行表单验证