AndroidStudio-广播

一、广播的本质

广播是一种数据传输方式

二、Android 中的广播

发送一条广播,可以被不同的广播接收者所接收,广播接收者收到广播之后,再进行逻辑处理。

三、收发标准广播

广播的收发过程分为三个步骤:

1.发送标准广播

2.定义广播接收器

3.开关广播接收器

例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp">
    
    <Button
        android:id="@+id/btn_send_standard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="发送标准广播"
        android:textColor="@color/black"
        android:textSize="17sp"/>

</LinearLayout>

创建一个标准广播接收者

package com.example.chapter08.receiver;

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

//定义一个标准广播的接收器
public class StandardReceiver extends BroadcastReceiver {

    public static final String STANDARD_ACTION = "com.example.chapter08.standard";

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent != null && intent.getAction().equals((STANDARD_ACTION))){
            Log.d("ning","收到一个标准广播");
        }
    }
}

在BroadStandardActivity文件中:

package com.example.chapter08;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;

import com.example.chapter08.receiver.StandardReceiver;

public class BroadStandardActivity extends AppCompatActivity implements View.OnClickListener{

    private StandardReceiver standardReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broad_standard);
        findViewById(R.id.btn_send_standard).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        //发送标准广播
        Intent intent = new Intent(StandardReceiver.STANDARD_ACTION);
        sendBroadcast(intent);
    }

    @Override
    protected void onStart() {
        super.onStart();
        standardReceiver = new StandardReceiver();
        //创建一个意图过滤器,只处理STANDARD_ACTION的广播
        IntentFilter filter = new IntentFilter(StandardReceiver.STANDARD_ACTION);
        registerReceiver(standardReceiver,filter);
    }

    @Override
    protected void onStop() {
        super.onStop();
        //注销接收器,注销之后就不再接收广播
        unregisterReceiver(standardReceiver);
    }
}

四、收发有序广播

一个广播存在多个接收器,这些接收器需要排队收听广播,这意味着广播是条有序广播

先收到广播的接收器A,既可以让其他接收器继续收听广播,也可中断广播不让其他接收器收听

例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp">

    <Button
        android:id="@+id/btn_send_order"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="发送标准广播"
        android:textColor="@color/black"
        android:textSize="17sp"/>

</LinearLayout>

创建两个接收器A和B

package com.example.chapter08.receiver;

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

import com.example.chapter08.BroadOrderActivity;

public class OrderAReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent != null && intent.getAction().equals(BroadOrderActivity.ORDER_ACTION)){
            Log.d("ning","接收器A收到一个有序广播");
        }
    }
}
package com.example.chapter08.receiver;

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

import com.example.chapter08.BroadOrderActivity;

public class OrderBReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent != null && intent.getAction().equals(BroadOrderActivity.ORDER_ACTION)){
            Log.d("ning","接收器B收到一个有序广播");
        }
    }
}

在BroadOrderActivity文件中:

package com.example.chapter08;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;

import com.example.chapter08.receiver.OrderAReceiver;
import com.example.chapter08.receiver.OrderBReceiver;

public class BroadOrderActivity extends AppCompatActivity implements View.OnClickListener{

    public static final String ORDER_ACTION = "com.example.chapter08.order";
    private OrderAReceiver orderAReceiver;
    private OrderBReceiver orderBReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broad_order);
        findViewById(R.id.btn_send_order).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        //创建一个指定动作的意图
        Intent intent = new Intent();
        //发送有序广播
        sendOrderedBroadcast(intent,null);
    }

    @Override
    protected void onStart() {
        super.onStart();
        //多个接收器处理有序广播的顺序规则为:
        //1.优先级越大的接收器,越早收到有序广播
        //2.优先级相同的时候,越早注册的接收器越早接收到有序广播
        orderAReceiver = new OrderAReceiver();
        IntentFilter filterA = new IntentFilter(ORDER_ACTION);
        filterA.setPriority(8);
        registerReceiver(orderAReceiver,filterA);

        orderBReceiver = new OrderBReceiver();
        IntentFilter filterB = new IntentFilter(ORDER_ACTION);
        filterB.setPriority(10);
        registerReceiver(orderBReceiver,filterB);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(orderAReceiver);
        unregisterReceiver(orderBReceiver);
    }
}

中断广播

五、注册静态广播

在代码中注册接收器,该方式被称作动态注册

在AndroidManifest.xml中注册接收器,该方式称作静态注册

示例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp">
    
    <Button
        android:id="@+id/btn_send_shock"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="发送震动广播"
        android:textColor="@color/black"
        android:textSize="17sp"/>

</LinearLayout>

在ShockReceiver文件中:

package com.example.chapter08.receiver;

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

public class ShockReceiver extends BroadcastReceiver {

    public static final String SHOCK_ACTION = "com.example.chapter08.shock";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null && intent.getAction().equals(SHOCK_ACTION)){
            Log.d("ning","震动");
            //从系统服务器中获取震动管理器
            Vibrator vb = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
            //命令震动器震动若干秒
            vb.vibrate(500);
        }
    }
}

在BroadStaticActivity文件中:

package com.example.chapter08;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class BroadStaticActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broad_static);
        findViewById(R.id.btn_send_standard).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        //Android8.0之后删除了大部分静态注册,防止退出App后仍在接收广播
        //为了让应用能够继续接收静态广播,需要给静态注册的广播指定包名。
        String fullName = "com.example.chapter08.receiver";
        Intent intent = new Intent("com.example.chapter08.shock");
        //发送静态广播之时,需要通过setComponent方法指定接收器的完整路径
        ComponentName componentName = new ComponentName(this,fullName);
        //设置意图的组件信息
        intent.setComponent(componentName);
        sendBroadcast(intent);
    }
}

发布运行:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值