Android Development-Broadcast

Today, let's talk about the use of broadcasting. Broadcasting is also used to disseminate messages within app. There are both senders and receivers of broadcasting.

See the code specifically to illustrate the use of broadcasting.

1. Broadcasting

Write a page to send broadcasts, and see the comments on the code.

(1) Page layout file

<?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"
    android:padding="15dp"
    tools:context=".broadcast.BroadActivity2">

    <Button    Buttons are used to send broadcasts
        android:id="@+id/click"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="CLICK"/>
</LinearLayout>

(2) java files

package com.example.scrollview.broadcast;

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

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

import com.example.scrollview.R;

public class BroadActivity2 extends AppCompatActivity {

    Button btn2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broad2);
        btn2=findViewById(R.id.click);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent("com.example.scrollview"); //Used to carry messages
                LocalBroadcastManager.getInstance((BroadActivity2.this)).sendBroadcast(intent);//Transmit broadcast
            }
        });
    }
}

2. Receiving Broadcasting

Write a page to receive broadcasts

(1) xml file

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="15dp"
    tools:context=".broadcast.BroadActivity">

    <Button  //Button for jumping
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="click"
        android:textSize="15sp"/>

    <TextView   //Used to display broadcasts received
        android:id="@+id/tv_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="abc"
        android:gravity="center"
        android:layout_marginTop="20dp"
        android:textSize="20sp"/>

</LinearLayout>

(2) java files

package com.example.scrollview.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.scrollview.R;

public class BroadActivity extends AppCompatActivity {
    Button btn1;
    TextView  tv1;
    MyBroadcast myBroadcast;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broad);
        btn1=findViewById(R.id.btn1);
        tv1=findViewById(R.id.tv_text);
//Make page jumps
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(BroadActivity.this,BroadActivity2.class);
                startActivity(intent);
            }
        });
        myBroadcast=new MyBroadcast();//Create a new class for broadcasting
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction("com.example.scrollview");//Add Receive Broadcasting
     LocalBroadcastManager.getInstance(this).registerReceiver(myBroadcast,intentFilter);
//Reception of broadcasting
    }

//Create a class to receive broadcasts and inherit the BroadcastReceiver class
    private  class MyBroadcast extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()){//Judgment of received broadcasts
                case "com.example.scrollview":
                    tv1.setText("123"); //After receiving the broadcast, the following text view displays 123
                    break;
            }
        }
    }
//Recycle memory after receiving broadcast
    protected void onDestroy(){
        super.onDestroy();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(myBroadcast);
    }
}

 

Talk about the effect of writing

The text view on page A begins with "abc", and when you go to the broadcast sending page and click on it to send the broadcast, you return to page A and receive the broadcast text view and it becomes "123".

Keywords: Android xml encoding Java

Added by gmwebs on Thu, 10 Oct 2019 10:48:37 +0300