廣播
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);
}