import static com.example.administrator.myandroid.BroadcastReceive.AnotherActivity.ACTION;
public class MyReceiveActivity extends Activity implements View.OnClickListener { private Button mBtnStartReceive; private Button mBtnStopReceive; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_receive); mBtnStartReceive = (Button) findViewById(R.id.start_receive); mBtnStopReceive = (Button) findViewById(R.id.stop_receive); mBtnStartReceive.setOnClickListener(this); mBtnStopReceive.setOnClickListener(this); } private boolean flagReceiveRegis; @Override public void onClick(View v) { switch (v.getId()) { case R.id.start_receive: IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(ACTION); //这个ACTION和后面activity的ACTION一样就行,要不然收不到的 registerReceiver(myBroadcastReceive, intentFilter); Log.i("广播", "----注册广播"); flagReceiveRegis = true; startActivity(new Intent(this, AnotherActivity.class)); break; case R.id.stop_receive: if (flagReceiveRegis) { unregisterReceiver(myBroadcastReceive); flagReceiveRegis = false; Log.i("广播", "----注销了"); } else { Log.i("广播","----还没有开启广播"); } break; } } BroadcastReceiver myBroadcastReceive = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.i("广播", "----接收到的是----" + intent.getStringExtra("msg")); } }; @Override protected void onDestroy() { if (flagReceiveRegis) { unregisterReceiver(myBroadcastReceive); flagReceiveRegis = false; Log.i("广播", "----注销了"); } else { Log.i("广播","----还没有开启广播"); } super.onDestroy();
}
//第一个布局
<?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:orientation="vertical"> <Button android:id="@+id/start_receive" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开启广播"/> <Button android:id="@+id/stop_receive" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="关闭注销广播"/> </LinearLayout>
//第二个activity,,,将值通过广播传给第一个activity
public class AnotherActivity extends Activity implements View.OnClickListener { private Button mBtnSend; public static final String ACTION ="com.example.action"; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_another); mBtnSend = (Button) findViewById(R.id.btn_send); mBtnSend.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_send: Intent intent = new Intent(ACTION); intent.putExtra("msg", "--我是发送给第一个界面的信息"); sendBroadcast(intent); Log.i("广播","--发送了消息"); finish(); break; } }
//第二个布局
<?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:orientation="vertical"> <Button android:id="@+id/btn_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送"/> </LinearLayout>//千万不要忘了在manifest中配置activity哦!!!