android廣播代碼彙總一__無序廣播

廣播

1 註冊廣播

		LocalReceiver  localReceiver = new LocalReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Constant.CONTACTBACKUPMESSAGEICTION);
        filter.addAction(Constant.CONTACTBACKUPCHOOSEID);
        filter.addAction(Constant.CONTACTRESTOREMESSAGE);
        registerReceiver(localReceiver, filter);

2 接收廣播


    public class LocalReceiver extends BroadcastReceiver {

        public LocalReceiver() {
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e(TAG, "接收到了本地廣播");
            if (intent != null) {
                String actionStr = intent.getAction();
                if (actionStr.equals(Constant.CONTACTBACKUPMESSAGEICTION)) {
                    setLoadingIndicator(false, null);
                    String contactsNum = intent.getStringExtra("contactsNum");      
                } else if (actionStr.equals(Constant.CONTACTBACKUPCHOOSEID)) {
               
                } else if (actionStr.equals(Constant.CONTACTRESTOREMESSAGE)) {
                  
                }
            }

        }

    }

3 發送廣播

 public void sendLocalBroadcast(String msg) {
        Intent intent = new Intent(Constant.CONTACTRESTOREMESSAGE);
        intent.putExtra("msg", msg);
        sendBroadcast(intent);

    }

4 註銷廣播

  @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(localReceiver);
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章