Android周学习Step By Step(9)--Intent之广播(完)
16lz
2021-01-26
通过broadcast Intent机制可以将一个Intent发送给任何对这个Intent感兴趣的BroadcastReceiver。
通过new Intent(ACTION_1)新建Action为ACTION_1的Intent。
通过sendBroadcast(intent)将这个intent进行广播。代码如下
ActivityMain代码:
1: package com.eoeandroid.broadcastReceiver;
2: import android.app.NotificationManager;
3: import android.content.BroadcastReceiver;
4: import android.content.Context;
5: import android.content.Intent;
6: public class EoeAndroidReceiver2 extends BroadcastReceiver {7: Context context;
8: @Override9: public void onReceive(Context context, Intent intent) {10: // TODO Auto-generated method stub11: this.context = context;12: DeleteNotification();
13: }
14: private void DeleteNotification() {15: NotificationManager notificationManager = (NotificationManager) context
16: .getSystemService(android.content.Context.NOTIFICATION_SERVICE);
17: notificationManager.cancel(EoeAndroidReceiver1.NOTIFICATION_ID);
18:
19: }
20: }
当单击MENU的第一项后,程序执行到EoeAndroidReceiver1,通过OnRecievie方法将一个Notification显示在了状态栏中。其中showNotification()负责显示一个Notification。代码如下:
EoeAndroidReceiver1代码
<style type="text/css"> <!-- .csharpcode, .csharpcode pre {font-size:small; color:black; font-family:consolas,"Courier New",courier,monospace; background-color:#ffffff} .csharpcode pre {margin:0em} .csharpcode .rem {color:#008000} .csharpcode .kwrd {color:#0000ff} .csharpcode .str {color:#006080} .csharpcode .op {color:#0000c0} .csharpcode .preproc {color:#cc6633} .csharpcode .asp {background-color:#ffff00} .csharpcode .html {color:#800000} .csharpcode .attr {color:#ff0000} .csharpcode .alt {background-color:#f4f4f4; width:100%; margin:0em} .csharpcode .lnum {color:#606060} --> </style>1: package com.eoeandroid.broadcastReceiver;
2: import android.app.Notification;
3: import android.app.NotificationManager;
4: import android.app.PendingIntent;
5: import android.content.BroadcastReceiver;
6: import android.content.Context;
7: import android.content.Intent;
8: public class EoeAndroidReceiver1 extends BroadcastReceiver {9: Context context;
10: public static int NOTIFICATION_ID = 21321;11: @Override12: public void onReceive(Context context, Intent intent) {13: this.context = context;14: showNotification();
15: }
16: private void showNotification() {17: NotificationManager notificationManager = (NotificationManager) context
18: .getSystemService(android.content.Context.NOTIFICATION_SERVICE);
19: Notification notification = new Notification(R.drawable.icon,20: "在EoeAndroidReceiver1中", System.currentTimeMillis());21: PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
22: new Intent(context, ActivityMain.class), 0);23: notification.setLatestEventInfo(context, "在EoeAndroidReceiver1中", null,24: contentIntent);
25: notificationManager.notify(NOTIFICATION_ID, notification);
26: }
27: }
<style type="text/css"> <!-- .csharpcode, .csharpcode pre {font-size:small; color:black; font-family:consolas,"Courier New",courier,monospace; background-color:#ffffff} .csharpcode pre {margin:0em} .csharpcode .rem {color:#008000} .csharpcode .kwrd {color:#0000ff} .csharpcode .str {color:#006080} .csharpcode .op {color:#0000c0} .csharpcode .preproc {color:#cc6633} .csharpcode .asp {background-color:#ffff00} .csharpcode .html {color:#800000} .csharpcode .attr {color:#ff0000} .csharpcode .alt {background-color:#f4f4f4; width:100%; margin:0em} .csharpcode .lnum {color:#606060} --> </style>单击第二个按钮后程序开始广播,这个广播被EoeAndroidReceiver2截获然后开始执行EoeAndroidReceiver2里的OnReceive方法,其中DeleteNotification()方法负责将刚才生成的Notification从状态栏中删除。代码如下:
EoeAndroidReceiver2代码
1: package com.eoeandroid.broadcastReceiver;
2: import android.app.NotificationManager;
3: import android.content.BroadcastReceiver;
4: import android.content.Context;
5: import android.content.Intent;
6: public class EoeAndroidReceiver2 extends BroadcastReceiver {7: Context context;
8: @Override9: public void onReceive(Context context, Intent intent) {10: // TODO Auto-generated method stub11: this.context = context;12: DeleteNotification();
13: }
14: private void DeleteNotification() {15: NotificationManager notificationManager = (NotificationManager) context
16: .getSystemService(android.content.Context.NOTIFICATION_SERVICE);
17: notificationManager.cancel(EoeAndroidReceiver1.NOTIFICATION_ID);
18: }
19: }
<style type="text/css"> <!-- .csharpcode, .csharpcode pre {font-size:small; color:black; font-family:consolas,"Courier New",courier,monospace; background-color:#ffffff} .csharpcode pre {margin:0em} .csharpcode .rem {color:#008000} .csharpcode .kwrd {color:#0000ff} .csharpcode .str {color:#006080} .csharpcode .op {color:#0000c0} .csharpcode .preproc {color:#cc6633} .csharpcode .asp {background-color:#ffff00} .csharpcode .html {color:#800000} .csharpcode .attr {color:#ff0000} .csharpcode .alt {background-color:#f4f4f4; width:100%; margin:0em} .csharpcode .lnum {color:#606060} --> </style>
Android学习到此告一段落,经过一周的学习(严格的说学习的时间外加写论文的时间是一周,总结、写博客的时间远远大于一周)只能对Android有个大致的了解,还有很多方面由于时间的原因不能够深入学习,甚至很多都没有接触到,以后有时间再学习吧。
Android,有缘我们还会再见!
更多相关文章
- java化改造--xml布局文件转成java——学习整理
- Android周学习Step By Step(8)--Intent之启动新的Activity
- Android5.0 新特性学习总结
- Android(安卓)通过 intent传值获得null
- mars老师的googleMap(一)
- Android利用ViewPager仿微信主界面-android学习之旅(78)
- Android(安卓)Internet应用实现获取天气预报的示例代码
- 【转】【Android游戏开发十五】关于Android(安卓)游戏开发中 OnT
- android 菜瓜笔记之GD-GUI逆向代码结构调整
随机推荐
- Android下setLatestEventInfo警告、Handl
- Android数据库SQLite使用详解一 : SQLite
- Android(安卓)Calendar一闪而过 无法创建
- android 之 handler
- xmlns:android="http://schemas.android.
- Android(安卓)4.0 SDK的离线方式安装
- Android(安卓)UI之对话框(dialog)
- 2011.06.29(2)——— android 完全退出
- Introduction to Model View Presenter o
- Android中的Environment.getExternalStor