Android一句話區分sendBroadcast與sendStickyBroadcast

來源:互聯網
上載者:User

小例實現步驟:

①.在MainActivity裡面發送兩種類型的廣播:sendBroadcast和sendStickyBroacat。

②在ReceverActivity裡面通過BroadcastReceiver來接收這兩個訊息,這裡是通過代碼來註冊Recevier而不是在Manifest裡面註冊的。

③結論:通過sendBroadcast中發出的intent在ReceverActivity不處於onResume狀態是無法接受到的,即使後面再次使其處於該狀態也無法接受到。而sendStickyBroadcast發出的Intent當ReceverActivity重新處於onResume狀態之後就能重新接受到其Intent。這就是the Intent will be held to be re-broadcast
to future receivers這句話的表現。就是說sendStickyBroadcast發出的最後一個Intent會被保留,下次當Recevier處於活躍的時候,又會接受到它。

④在wifi中就是發送的粘性廣播。

 

下面是兩個類的代碼部分。

MainActivity:

public class MainActivity extends Activity {       Button btnSendi;       Button btnSends;       Button btnStart;       Context mContext;       /** Called when the activity is first created. */      @Override      public void onCreate(Bundle savedInstanceState) {           super.onCreate(savedInstanceState);           setContentView(R.layout.main);           btnSendi=(Button) findViewById(R.id.sendi);           btnSends=(Button) findViewById(R.id.sends);           btnStart=(Button) findViewById(R.id.start);           mContext=getApplicationContext();           btnSendi.setOnClickListener(new OnClickListener(){                 @Override              public void onClick(View v) {                   // TODO Auto-generated method stub                   Intent intent = new Intent();                   intent.setAction("com.android.my.action");                   intent.setFlags(1);                   mContext.sendBroadcast(intent);               }                          });                      btnStart.setOnClickListener(new OnClickListener(){                 @Override              public void onClick(View v) {                   // TODO Auto-generated method stub                   Intent intent = new Intent(MainActivity.this,ReceiverActivity.class);                                     startActivity(intent);               }                          });                      btnSends.setOnClickListener(new OnClickListener(){                 @Override              public void onClick(View v) {                   // TODO Auto-generated method stub                   Intent intent = new Intent();                   intent.setAction("com.android.my.action.sticky");                   intent.setFlags(2);                   mContext.sendStickyBroadcast(intent);               }                          });       }   }  

ReceiverActivity :

public class ReceiverActivity extends Activity {        private IntentFilter mIntentFilter;              /** Called when the activity is first created. */      @Override      public void onCreate(Bundle savedInstanceState) {           super.onCreate(savedInstanceState);           setContentView(R.layout.main);           mIntentFilter = new IntentFilter();           mIntentFilter.addAction("com.android.my.action");           mIntentFilter.addAction("com.android.my.action.sticky");                        }       private BroadcastReceiver mReceiver = new BroadcastReceiver() {             @Override          public void onReceive(Context context, Intent intent) {               final String action = intent.getAction();               System.out.println("action"+action);                          }       };              @Override      protected void onResume() {           // TODO Auto-generated method stub           super.onResume();           registerReceiver(mReceiver, mIntentFilter);       }              @Override      protected void onPause() {           // TODO Auto-generated method stub           super.onPause();           unregisterReceiver(mReceiver);       }                 }  

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.