Android--BrodcastReceiver广播的简单使用

一. 简介

BrodcastReceiver是Android的四大组件之一,很多时候我们都需要用到,特别是App后台运行时,通知栏的提示,除了推送,就是BrodcastReceiver的应用了。

二. 简单使用

广播的使用,需要先注册后使用,只能回传数据,不能直传数据。

两个Activity之间的广播更改数据:

MainActivity.class

package com.jmg.brodcastreceivertext;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity{

    Button send1;
    TextView text;
    String username;
    //处理
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    text.setText(username);     //更新数据列
                    break;
                default:
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //动态注册广播
        IntentFilter filter = new IntentFilter();
        filter.addAction("AAA");    //广播标识,每个广播需要不同,不然会获取乱的数据
        registerReceiver(mReceiver, filter);

        send1 = (Button) findViewById(R.id.send1);
        text = (TextView) findViewById(R.id.text);

        send1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, SecondActivity.class));
            }
        });

    }

    /**
     * 广播接收
     */
    public BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals("AAA")) {
                username = intent.getStringExtra("username");
                mHandler.sendEmptyMessage(1);
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mReceiver);      //取消注册
    }

}

SecondActivity.class

package com.jmg.brodcastreceivertext;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;

public class SecondActivity extends Activity {

    Button button;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_layout);

        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //发送广播,通知最新列表更新
                Intent intent = new Intent();
                intent.putExtra("username", "123456");
                //Action:自己定义
                intent.setAction("AAA");
                sendBroadcast(intent, null);
                finish();
            }
        });
    }

}

三. 通过广播类接收广播

(1)静态注册广播

静态注册不会随着activity的生命周期结束而结束,动态注册会随着activity的生命周期结束而结束。

在清单文件中静态注册:

<receiver android:name=".MyBrodcast"></receiver>

MyBrodcast.class

package com.jmg.brodcastreceivertext;

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

public class MyBrodcast extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String msg = intent.getStringExtra("data");
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }
}
每天进步一点点!



猜你喜欢

转载自blog.csdn.net/qq_26446715/article/details/80265292