当前位置:   article > 正文

第十三单元:BroadCastReceiver运用-广播接受者_发送全局广播android 13

发送全局广播android 13

第十三单元:BroadCastReceiver运用-广播接受者

BroadCastReceiver运用
全局广播
本地广播
BroadCastReceiver和Notifcation
使用BroadCastReceiver发送消息更新UI
Android EventBus 的使用
全局介绍
发送的广播事件能被其他应用程序获取,也可以响应其他应用程序发送的广播事件(可以通过 exported–是否监听其他应用程序发送的广播 ) 全局广播可以动态注册,也可以静态注册。
  • 1
默认发送的广播是全局广播,而且所有的App都可以请求接受
本地广播

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

首先注册一个广播

        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: ");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
Java代码
  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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
本地广播的优点

可以明确的知道正在发送的广播不会离开我们的程序,不用担心机密数据泄露

其他程序无法将广播发送到我们的程序内部,不需要担心安全问题

发送本地广播比系统全局的广播效率高

BroadCastReceiver和Notifcation
Java代码

    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);

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
使用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_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>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
Activity中的Java代码

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 MainActivity extends AppCompatActivity {
    private Button updataUiId;
    private ImageView imageId;
    private MyReceiver myReceiver;

    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_main);

        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(myReceiver,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(myReceiver);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
广播接收的代码

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);
                }
            });
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/935133
推荐阅读
相关标签
  

闽ICP备14008679号