一、 广播发送及广播类型
- Normal Broadcast:普通广播(同步广播)
Intent broadcast = new Intent();
broadcast.setAction("com.example.crackme");
broadcast.putExtra("test", "发送广播");
sendBroadcast(broadcast);
- System Broadcast:系统广播
- Ordered Broadcast:有序广播
- Local Broadcast:app应用内广播
二、广播接收器注册
- 静态注册
<receiver android:name=".WellcomeActivity$MyBroadcastReciver">
<intent-filter>
<action android:name="com.example.crackme"/>
</intent-filter>
</receiver>
- 动态注册
myBroadcastReciver = new MyBroadcastReciver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.example.crackme");
registerReceiver(myBroadcastReciver, intentFilter);
三、BroadcastReceiver接收广播
public class MyBroadcastReciver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.v("消息", "来自GoodByeActivity的广播");
String str = intent.getExtras().getString("test");
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
Toast.makeText(context, "来自GoodByeActivity的广播",Toast.LENGTH_SHORT).show();
finish();
}
}
这样一个完整的发送广播和接收广播的过程就完成了。
这次做的功能是:在一个Activity中销毁另外一个Activity。
方法一:利用广播接收者
第一个Activity
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, WellcomeActivity.class);
startActivity(intent);
finish();
}
});
}
}
第二个Activity:
public class WellcomeActivity extends AppCompatActivity {
private TextView textView;
MyBroadcastReciver myBroadcastReciver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wellcome);
textView = (TextView) findViewById(R.id.textView);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(WellcomeActivity.this, GoodByeActivity.class);
startActivity(intent);
myBroadcastReciver = new MyBroadcastReciver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.example.crackme");
registerReceiver(myBroadcastReciver, intentFilter);
}
});
}
public class MyBroadcastReciver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.v("消息", "来自GoodByeActivity的广播");
String str = intent.getExtras().getString("test");
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
Toast.makeText(context, "来自GoodByeActivity的广播",Toast.LENGTH_SHORT).show();
finish();
}
}
@Override
protected void onDestroy() {
if (myBroadcastReciver != null){
unregisterReceiver(myBroadcastReciver);
}
super.onDestroy();
}
}
第三个Activity:
public class GoodByeActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_good_bye);
button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent broadcast = new Intent();
broadcast.setAction("com.example.crackme");
broadcast.putExtra("test", "发送广播);
sendBroadcast(broadcast);
Intent intent = new Intent(GoodByeActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});
}
}
注意:这里实例化了两个Intent对象,他们的作用不同。第一个Intent用于发送广播,第二个Intent实现页面跳转。这里不能将它们写在一起,写在一起在第二个Activity就无法收到广播,如下写法:
Intent broadcast = new Intent(GoodByeActivity.this, MainActivity.class);
broadcast.setAction("com.example.crackme");
broadcast.putExtra("test", "发送广播);
sendBroadcast(broadcast);
startActivity(broadcast);
第二种方法:利用startActivityForResult()同样可以实现
第一个Activity不变
第二个Activity修改为:
public class WellcomeActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wellcome);
textView = (TextView) findViewById(R.id.textView);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(WellcomeActivity.this, GoodByeActivity.class);
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (resultCode){
case 1:
Log.v("消息", "来自GoodByeActivity的结果码");
finish();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
第三个Activity修改为:
public class GoodByeActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_good_bye);
button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
setResult(1, intent);
finish();
}
});
}
}
就这个功能而言似乎第二种方法更简洁
网友评论