Android之BroadcastReceiver广播

LocalBroadcastReceiverManager

BroadActivity(注册广播,接受广播)和xml

package com.example.test0508.broadcast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.test0508.R;

public class BroadActivity extends AppCompatActivity {

    private Button mBtn1;
    private TextView mTvTest;
    private MyBroadcast myBroadcast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broad1);
        mBtn1 = findViewById(R.id.btn_broad1_btn1);
        mTvTest = findViewById(R.id.tv_broad1_test);
        mBtn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(BroadActivity.this,BroadActivity2.class);
                startActivity(intent);
            }
        });

        /**
         * 注册广播
         */
        myBroadcast = new MyBroadcast();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.skypan.update");
        LocalBroadcastManager.getInstance(this).registerReceiver(myBroadcast,intentFilter);
    }
    private class MyBroadcast extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            //广播接受
            switch (intent.getAction()){
                case "ss":
                    break;
                case "com.skypan.update":
                    mTvTest.setText("123");
                    break;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在destroy需要把广播取消掉
        LocalBroadcastManager.getInstance(this).unregisterReceiver(myBroadcast);

    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".broadcast.BroadActivity"
    android:orientation="vertical"
    >

    <Button
        android:id="@+id/btn_broad1_btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="click"
        />

    <TextView
        android:id="@+id/tv_broad1_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="abc"
        android:gravity="center"
        android:textSize="20sp"
        android:layout_marginTop="30dp"
        />

</LinearLayout>

BroadActivity2(发送广播)和xml

package com.example.test0508.broadcast;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.example.test0508.R;

public class BroadActivity2 extends AppCompatActivity {

    private Button mBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broad2);
        mBtn = findViewById(R.id.btn_broad2_click);
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent("com.skypan.update");
                LocalBroadcastManager.getInstance(BroadActivity2.this).sendBroadcast(intent);
            }
        });
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".broadcast.BroadActivity">

    <Button
        android:id="@+id/btn_broad2_click"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="click me"
        android:textAllCaps="false"
        />

</LinearLayout>

你可能感兴趣的:(Android)