android 廣播的簡單使用

在Android中,有一些操作完成以後,會發送廣播,比如說發出一條短信,或打出一個電話,如果某個程序接收了這個廣播,就會做相應的處理。這個廣播跟我們傳統意義中的電臺廣播有些相似之處。之所以叫做廣播,就是因爲它只負責“說”而不管你“聽不聽”,也就是不管你接收方如何處理。另外,廣播可以被不只一個應用程序所接收,當然也可能不被任何應用程序所接收。

廣播機制最大的特點就是發送方並不關心接收方是否接到數據,也不關心接收方是如何處理數據的。

Android中廣播的是操作系統中產生的各種各樣的事件。例如,收到一條短信就會產生一個收到短信息的事件。而Android操作系統一旦內部產生了這些事件,就會向所有的廣播接收器對象來廣播這些事件。
廣播的使用有靜態註冊和動態註冊,這裏我就寫寫動態註冊的

首先 廣播的使用有三步:
1:定義廣播接收類,該類繼承BroadcastReceiver
//定義一個廣播接收類
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(ACTION_NAME)){
Toast.makeText(MainActivity.this, “處理action名字相對應的廣播”, 200).show();
}
}
};

2:註冊廣播(動態註冊)
public void registerBoradcastReceiver(){
IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction(ACTION_NAME);
//註冊廣播
registerReceiver(mBroadcastReceiver, myIntentFilter);
}

3:廣播的觸發反應
Intent mIntent = new Intent(ACTION_NAME);
mIntent.putExtra(“broadcast”, “發送廣播,相當於在這裏傳送數據”);

            //發送廣播 
            sendBroadcast(mIntent);

最後附上完整代碼(佈局文件就一個Button)
public class MainActivity extends Activity {
private Button button;
private final String ACTION_NAME = “發送廣播”;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button=(Button)findViewById(R.id.button);
    //broadcastReceiver=new MyBroadcastReceiver();
    //註冊廣播  
    registerBoradcastReceiver();  
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
             Intent mIntent = new Intent(ACTION_NAME); 
                mIntent.putExtra("yaner", "發送廣播,相當於在這裏傳送數據"); 

                //發送廣播 
                sendBroadcast(mIntent);

        }
    });
}
//定義一個廣播接收類
 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){ 
        @Override 
        public void onReceive(Context context, Intent intent) { 
            String action = intent.getAction(); 
            if(action.equals(ACTION_NAME)){ 
                Toast.makeText(MainActivity.this, "處理action名字相對應的廣播", 200).show(); 
            } 
        } 

    }; 


public void registerBoradcastReceiver(){  
    IntentFilter myIntentFilter = new IntentFilter();  
    myIntentFilter.addAction(ACTION_NAME);  
    //註冊廣播        
    registerReceiver(mBroadcastReceiver, myIntentFilter);  
}  

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

免費源碼下載
http://download.csdn.net/detail/caohuicong/9388492

發佈了26 篇原創文章 · 獲贊 9 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章