Broadcast data is written to interface

  1. **

Broadcast data is written to interface

**

The first step, let's add a TextView layout activity_main.xml used to display data.

The following code
is added to a simple layout TextView.


The second step, in the own radio class, define an internal interface

The following code, the interface can define your own name

interface DaiLi{
    
} 

Here Insert Picture Description

The third step in the main program MainActivity.java, the realization of this interface

Here Insert Picture Description

A fourth step in the broadcast Myguanbo own class definition, create a class, for transmitting its own data

You can define your own class name to note here use permissions to use because MainActivity in this category

A fifth step, this MainActivity.java in the incoming data broadcasting to the member defined in class

code show as below:
Here Insert Picture Description

The sixth step, in the main program, MainActivity.java obtain TextView controls

code show as below:

tv1 = findViewById(R.id.textView);

A seventh step, in the broadcast system, the internal interface, a write method for writing data

code show as below:

{DaiLi interface
void xieru (String S);
}
Here Insert Picture Description

An eighth step, in the main, MainActivity.java, the rewriting method of writing data interface

code show as below:

@Override
public void xieru(String s) {

}

Here Insert Picture Description

The ninth step in the main program, MainActivity.java in xieru rewriting process, the data to the display control TextView

Because, at the beginning, when we get TextView control is local, direct call here defined, is being given, so we have to first TextView control is defined as the overall situation
Here Insert Picture Description
at this time, we go inside xieru method, the data pass inside.

@Override
public void xieru(String s) {
    tv1.setText(s);
}

Results are as follows:
Here Insert Picture Description

The tenth step, in his broadcast class, call, xieru this method

Here, we have to call interface methods inside, first create an instance of the interface, and the need to put this instance, defined as global, because the next step, we need to use it.

First, we create a global interface instance,

	private DaiLi daiLi;

Results are as follows:
Here Insert Picture Description

Then, call xieru method,

daiLi.xieru();

Tenth step, modify chuangShongZiJi () method

At this time, chuangShongZiJi () method DaiLi parameter d is equal, the value of the main program, to the interface.
code show as below:

public void chuangShongZiJi(DaiLi d){
    daiLi=d;
}

Results are as follows:

Here Insert Picture Description

A twelfth step, the main program to obtain the string pass

You have to define your own a string get, "xigua" string value in the main program,
code is as follows:

String str = intent.getStringExtra("xigua");

Finally writes the string to the interface tv1, the string "str" method to write just call.
code show as below:

daiLi.xieru(str);

Now the code to finish it! We look at the effect

Here Insert Picture Description

The complete code

Myguanbo.java Code

package com.example.myapplication_10;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author QiuSiQi
 * @package com.example.myapplication_200402
 * @fileName Myguanbo
 * @date on 2020/4/2  9:24
* @describe TODO
 */
public class Myguanbo extends BroadcastReceiver {
 private DaiLi daiLi;

@Override
public void onReceive(Context context, Intent intent) {
    String str = intent.getStringExtra("xigua");
    //Toast.makeText(context,str,Toast.LENGTH_SHORT).show();
   //写入界面TV中
    daiLi.xieru(str);

}

//定义一个内部接口
interface DaiLi{
    void xieru(String s);
}
public void chuangShongZiJi(DaiLi d){
    daiLi=d;
}

}

MainActivity.java Code

package com.example.myapplication_10;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity implements Myguanbo.DaiLi{
 private Myguanbo gb;
 private TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //选台,注册广播
    gb = new Myguanbo();
    IntentFilter intentFilter = new IntentFilter("nanChang");
    registerReceiver(gb,intentFilter);

    gb.chuangShongZiJi(this);

    tv1 = findViewById(R.id.textView);
    Button btn1= findViewById(R.id.button);
    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //发送广播
            Intent intent = new Intent("nanChang");
            intent.putExtra("xigua","6.5");
            intent.setPackage("com.example.myapplication_10");
            sendBroadcast(intent);
        }
    });

}

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(gb);
}


@Override
public void xieru(String s) {
    tv1.setText(s);
}

}

Layout code, activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">


<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:text="发送广播"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="297dp"
    android:text="TextView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Released two original articles · won praise 0 · Views 101

Guess you like

Origin blog.csdn.net/weixin_46748537/article/details/105267235