【转】Pro Android学习笔记(九八):BroadcastReceiver(2):接收器触发通知

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

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

小例子

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

Pro <wbr>Android学习笔记(九八):BroadcastReceiver(2):接收器触发通知

代码

关于通知,可以参考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即可。在安装是会显示:

Pro <wbr>Android学习笔记(九八):BroadcastReceiver(2):接收器触发通知

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

创建自己的Content View风格:RemoteView

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

Pro <wbr>Android学习笔记(九八):BroadcastReceiver(2):接收器触发通知

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

Pro <wbr>Android学习笔记(九八):BroadcastReceiver(2):接收器触发通知

相关的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

原文地址:https://www.cnblogs.com/blongfree/p/5048121.html