BroadCastReceiver运用-广播接受者

BroadCastReceiver运用

全局广播:
本地广播
案例 1 BroadCastReceiver和Notifcation
案例 2 使用BroadCastReceiver发送消息更新UI
Android EventBus 的使用

全局广播:

发送的广播事件可被其他应用程序获取,也能响应其他应用程序发送的广播事件(可以通过 exported–是否监听其他应用程序发送的广播 在清单文件中控制) 全局广播既可以动态注册,也可以静态注册。

默认发送的广播,就是全局广播,所有的App都可以请求接受

本地广播
发送的广播事件不被其他应用程序获取,也不能响应其他应用程序发送的广播事件。
本地广播只能被动态注册,不能静态注册。动态注册或方法时需要用到LocalBroadcastManager。

本地广播案例
1,注册一个广播

package com.example.day013;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "MyReceiver";
@Override
public void onReceive(Context context, Intent intent) {
    Log.i(TAG, "onReceive: ");
}
}

java代码

package com.example.day013;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements  View.OnClickListener{
private Button send;
private LocalBroadcastManager localBroadcastManager;
private MyReceiver myReceiver;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    send = findViewById(R.id.send);
    send.setOnClickListener(this);
    //创建一个本地广播
    localBroadcastManager = LocalBroadcastManager.getInstance(this);

    //动态注册一个广播
    myReceiver = new MyReceiver();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("com.feng");
    localBroadcastManager.registerReceiver(myReceiver,intentFilter);

}

@Override
public void onClick(View v) {
    int id = v.getId();
    switch (id) {
        case R.id.send:
            Intent intent = new Intent();
            intent.setAction("com.feng");
            //注意广播的发送者
            localBroadcastManager.sendBroadcast(intent);
            break;

        default:
            break;
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    //有注册就有销毁
    localBroadcastManager.unregisterReceiver(myReceiver);
}
}

本地广播的优点:

可以明确的知道正在发送的广播不会离开我们的程序,不用担心机密数据泄露
其他程序无法将广播发送到我们的程序内部,不需要担心安全问题
发送本地广播比系统全局的广播效率高

案例 1 BroadCastReceiver和Notifcation
BroadCastReceiver和Notifcation
1,注册一个广播.(静态或者动态都可以.代码省略)
2,广播的内容.

package com.example.day013;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "MyReceiver";
@Override
public void onReceive(Context context, Intent intent) {
    Log.i(TAG, "onReceive: ");
    Notification.Builder builder = new Notification.Builder(context);
    builder.setAutoCancel(true);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setContentText("标题");
    builder.setContentText("内容");

    //可弹出
    builder.setPriority(Notification.PRIORITY_MAX);
    builder.setDefaults(Notification.DEFAULT_ALL);

    //可跳转
    Intent intent1 = new Intent(context,Main2Activity.class);

    PendingIntent activity = PendingIntent.getActivity(context, 0, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
    builder.setContentIntent(activity);

    Notification build = builder.build();
    NotificationManager  manager =(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(1,build);

}
}

案例 2 使用BroadCastReceiver发送消息更新UI
使用BroadCastReceiver发送消息更新UI
注意 需要动态注册广播
注意 需要动态注册广播
注意 需要动态注册广播
需求, 点击一个按钮,换一个图片
布局xml

<?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"
tools:context=".Main2Activity">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/updata_ui_id"
    android:text="更新一个图片"
    />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/image_id"
    android:src="@mipmap/ic_launcher"
    />
</LinearLayout>

Activity代码

package com.example.day013;

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.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class Main2Activity extends AppCompatActivity {
private Button updataUiId;
private ImageView imageId;
private MyReceiver2 myReceiver2;

private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);

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

    updataUiId = findViewById(R.id.updata_ui_id);
    imageId = findViewById(R.id.image_id);

    myReceiver2  = new MyReceiver2(handler,imageId);

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("com.feng.ui");
    registerReceiver(myReceiver2,intentFilter);

    updataUiId.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setAction("com.feng.ui");
            sendBroadcast(intent);
        }
    });
}

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

广播接受这的代码

package com.example.day013;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.widget.ImageView;

public class MyReceiver2 extends BroadcastReceiver {

private Handler handler;
private ImageView imageView;

public MyReceiver2(Handler handler, ImageView imageView) {
    super();
    this.handler = handler;
    this.imageView = imageView;
}

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if ("com.feng.ui".equals(action)){
        handler.post(new Runnable() {
            @Override
            public void run() {
                imageView.setImageResource(R.mipmap.ic_launcher_round);
            }
        });
    }
}
}

Android EventBus 的使用
官方文档

https://github.com/greenrobot/EventBus

翻译文档

https://www.jianshu.com/p/e7d5c7bda783
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值