Android周學習Step By Step(9)–Intent之廣播(完)

來源:互聯網
上載者:User

通過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:      @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代碼

   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:  }

單擊第二個按鈕後程式開始廣播,這個廣播被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:  }

Android學習到此告一段落,經過一周的學習(嚴格的說學習的時間外加寫論文的時間是一周,總結、寫部落格的時間遠遠大於一周)只能對Android有個大致的瞭解,還有很多方面由於時間的原因不能夠深入學習,甚至很多都沒有接觸到,以後有時間再學習吧。

Android,有緣我們還會再見!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.