Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 廣播的使用

android 廣播的使用

編輯:關於Android編程

在Activity中,注冊廣播的一個Demo。

總共分3步

第一步:定義一個BroadcastReceiver廣播接收類:

[java 
  1. private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){
  2. @Override
  3. public void onReceive(Context context, Intent intent) {
  4. String action = intent.getAction();
  5. if(action.equals(ACTION_NAME)){
  6. Toast.makeText(Test.this, 處理action名字相對應的廣播, 200);
  7. }
  8. }
  9.  
  10. };

     

    第二步:注冊該廣播:

    [java] 
    1. public void registerBoradcastReceiver(){
    2. IntentFilter myIntentFilter = new IntentFilter();
    3. myIntentFilter.addAction(ACTION_NAME);
    4. //注冊廣播
    5. registerReceiver(mBroadcastReceiver, myIntentFilter);
    6. }

       

      第三步:觸發響應

       

      [java] 
      1. mBtnMsgEvent = new Button(this);
      2. mBtnMsgEvent.setText(發送廣播);
      3. mBtnMsgEvent.setOnClickListener(new OnClickListener() {
      4. @Override
      5. public void onClick(View v) {
      6. Intent mIntent = new Intent(ACTION_NAME);
      7. mIntent.putExtra(yaner, 發送廣播,相當於在這裡傳送數據);
      8.  
      9. //發送廣播
      10. sendBroadcast(mIntent);
      11. }
      12. });
      13.  

         

        -----最後附上完整代碼:

        [java] 
        1. package my.yaner;
        2.  
        3. import android.app.Activity;
        4. import android.content.BroadcastReceiver;
        5. import android.content.Context;
        6. import android.content.Intent;
        7. import android.content.IntentFilter;
        8. import android.os.Bundle;
        9. import android.view.View;
        10. import android.view.View.OnClickListener;
        11. import android.widget.Button;
        12. import android.widget.LinearLayout;
        13. import android.widget.Toast;
        14.  
        15. public class Test extends Activity{
        16. private final String ACTION_NAME = 發送廣播;
        17. private Button mBtnMsgEvent = null;
        18.  
        19. protected void onCreate(Bundle savedInstanceState){
        20. super.onCreate(savedInstanceState);
        21.  
        22. //注冊廣播
        23. registerBoradcastReceiver();
        24.  
        25. LinearLayout mLinearLayout = new LinearLayout(this);
        26. mBtnMsgEvent = new Button(this);
        27. mBtnMsgEvent.setText(發送廣播);
        28. mLinearLayout.addView(mBtnMsgEvent);
        29. setContentView(mLinearLayout);
        30.  
        31. mBtnMsgEvent.setOnClickListener(new OnClickListener() {
        32. @Override
        33. public void onClick(View v) {
        34. Intent mIntent = new Intent(ACTION_NAME);
        35. mIntent.putExtra(yaner, 發送廣播,相當於在這裡傳送數據);
        36.  
        37. //發送廣播
        38. sendBroadcast(mIntent);
        39. }
        40. });
        41. }
        42.  
        43. private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){
        44. @Override
        45. public void onReceive(Context context, Intent intent) {
        46. String action = intent.getAction();
        47. if(action.equals(ACTION_NAME)){
        48. Toast.makeText(Test.this, 處理action名字相對應的廣播, 200);
        49. }
        50. }
        51.  
        52. };
        53.  
        54. public void registerBoradcastReceiver(){
        55. IntentFilter myIntentFilter = new IntentFilter();
        56. myIntentFilter.addAction(ACTION_NAME);
        57. //注冊廣播
        58. registerReceiver(mBroadcastReceiver, myIntentFilter);
        59. }
        60. }
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved