本帖最后由 liu 于 2018-12-29 15:46 编辑
用法:
要传值的类:
[Java] 纯文本查看 复制代码 Intent intent = new Intent();
intent.setAction("自己定义");
intent.putExtra("content", "要传的值");
sendBroadcast(intent);
要接收值的类:
[Java] 纯文本查看 复制代码 private void getBroadCast() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("要和传值时设置的action保持一致");
registerReceiver(myBroadcastReceive, intentFilter);
}
BroadcastReceiver myBroadcastReceive = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String content = intent.getStringExtra("content");
Log.i("TAG",intent.getStringExtra("content"));
}
};
注意:退出时注销广播
[Java] 纯文本查看 复制代码 @Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myBroadcastReceive);
}
举个两个Activity之间使用广播传值的例子:activity_main.xml:
[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转" />
</LinearLayout>
activity_second.xml:
[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:hint="输入内容" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="传值" />
</LinearLayout>
SecondActivity:
[Java] 纯文本查看 复制代码 public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button mButton = (Button) findViewById(R.id.btn);
final EditText mEditText = (EditText) findViewById(R.id.et);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("自己定义");
intent.putExtra("content", "要传的值");
sendBroadcast(intent);
finish();
}
});
}
}
MainActivity:
[Java] 纯文本查看 复制代码 public class MainActivity extends AppCompatActivity {
private TextView mTextView;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.tv);
mButton = (Button) findViewById(R.id.btn);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
getBroadCast();
}
private void getBroadCast() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("要和传值时设置的action保持一致");
registerReceiver(myBroadcastReceive, intentFilter);
}
BroadcastReceiver myBroadcastReceive = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String content = intent.getStringExtra("content");
mTextView.setText("获取到的值-----" + content);
}
};
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myBroadcastReceive);
}
}
运行效果:
|