文章转载只能用于非商业性质,且不能带有虚拟货币、积分、注册等附加条件。转载须注明出处:http://blog.sina.com.cn/flowingflying或做者@恺风Wei-傻瓜与非傻瓜html
广播接受可用于本地,也能够用于不一样的进程(应用)间。广播还经常使用于后台服务,当接收器收到某个广播消息时,一般会在通知栏中提示用户,用户点击通知,能够进入某个Activity中进行处理。android
小例子浏览器
接收器应用为本小例子,发送广播应用利用上一学习的小例子。app
关于通知,能够参考Android学习笔记(五五):通知Notification(下)。ide
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);
}
}学习
XMLui
在AndroidManifest.xml中一样要进行receiver的声明,标明所关注的广播。咱们的小例子无需有activity,只需receiver便可。在安装是会显示:google
因为在manifest中以告知系统所关心的广播,无需有一个App正在运行,一样也能够正确地实现触发。spa
建立本身的Content View风格:RemoteView.net
上面咱们使用了系统缺省的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);
}
前面,咱们使用了通知管理器,实际上咱们能够在收到广播时直接经过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