`
renzhelife
  • 浏览: 667761 次
文章分类
社区版块
存档分类
最新评论

Android周学习Step By Step(9)--Intent之广播(完)

 
阅读更多

通过broadcast Intent机制可以将一个Intent发送给任何对这个Intent感兴趣的BroadcastReceiver

image

通过new IntentACTION_1)新建ActionACTION_1Intent

通过sendBroadcastintent)将这个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:      @Override
   9:      public void onReceive(Context context, Intent intent) {
  10:          // TODO Auto-generated method stub
  11:          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:      @Override
  12:      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:      @Override
   9:      public void onReceive(Context context, Intent intent) {
  10:          // TODO Auto-generated method stub
  11:          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,有缘我们还会再见!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics