android 使用BroadcastReceiver发送自定义广播踩坑版

不知道踩了多少的坑,才弄好这个

下面的是没有用的代码,sdk为33

NormalOneReceiver.java接受广播的地方
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;


import androidx.appcompat.app.AlertDialog;

public class NormalOneReceiver extends BroadcastReceiver {

    private static final String tag = "NormalOneReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {

        new AlertDialog.Builder(context)
                .setIcon(R.drawable.ic_launcher_background)
                .setTitle("提示")
                .setMessage("出来了")
                .setCancelable(false )
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                })
                .show();
    }
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".NormalOneReceiver" android:exported="true" android:enabled="true">
            <intent-filter>
                <action android:name="com.my.myapplication.wdaw" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

android:enabled="true" 是否启动

android:exported="true"  android 高版本30后不添加这个会出错

<action android:name="com.my.myapplication.wdaw" />里面的这个可以自定义
AppCompatActivity.java发送广播的地方
package com.my.myapplication;

import androidx.appcompat.app.AppCompatActivity;

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


public class MainActivity extends AppCompatActivity {





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

    }



    public void ma() {
       Button button= findViewById(R.id.sendBroad);

       button.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent = new Intent("com.my.myapplication.wdaw");
               sendBroadcast(intent);
           }
       });

    }
}

在AndroidManifest.xml里面的<action android:name="com.my.myapplication.wdaw" />和你使用Intent里面添加的一样也可以使用

intent.setAction("com.my.myapplication.wdaw");

他们两个都区别

  1. intent.setAction("com.my.myapplication.wdaw");

这行代码是在一个已经创建的Intent对象上设置动作。首先,你创建了一个新的Intent对象,然后使用setAction方法为其设置了一个特定的动作。
2. Intent intent = new Intent("com.my.myapplication.wdaw");

这行代码是创建一个新的Intent对象并立即为其设置动作。这种方式实际上是创建了一个新的Intent实例,其动作被设置为"com.my.myapplication.wdaw"

主要的区别在于:

  • 第一个方式允许你在之后的代码中更改或添加更多的信息到这个Intent对象。
  • 第二个方式创建的Intent对象在初始化时就具有了完整的信息,之后不能更改

然后我看网上的方法就是那样说可以直接发送,结果不行然后又百度到说检查一下自己使用的测试机系统是否是Android 8以上。如果是的话,则需要设置packageName来把消息变成显式的。

在Android 8之后,发送广播务必显示设置接收的包名

  Intent intent = new Intent("com.my.myapplication.wdaw");
               intent.setPackage(getPackageName());
               sendBroadcast(intent);

结果我还是不行

然后

 Intent intent = new Intent("com.my.myapplication.wdaw");
intent.setComponent(new ComponentName(getPackageName(),
    "com.my.myapplication.NormalOneReceiver"));
sendBroadcast(intent);

 "com.my.myapplication.NormalOneReceiver"是你接受这个广播的文件也就是上面的NormalOneReceiver.java

我就怀疑人生了,他为什么不行

然后百度,查质料,搞了不知道多久,然后知道了 说是sdk26开始对静态广播进行了一些调整

因为本地广播不能静态注册,所以只能采取的方式是动态注册的方式,然后我就改代码了,接下来就是成功的代码可以,变成了了动态广播了,下面的代码可以实现,就效果

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {



    @Override
    protected void onResume() {
        super.onResume();
        NormalOneReceiver myReceiver  = new NormalOneReceiver();
        IntentFilter filter = new IntentFilter("com.my.myapplication.wdaw");
        registerReceiver(myReceiver, filter);

    }

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

    }



    public void ma() {
       Button button= findViewById(R.id.sendBroad);

       button.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent = new Intent("com.my.myapplication.wdaw");
               sendBroadcast(intent);
           }
       });

    }
}

NormalOneReceiver.java这个文件这样copy上面的里面的东西不需要修改,AndroidManifest.xml里面的,可以删除了

 <receiver android:name=".NormalOneReceiver" android:exported="true" android:enabled="true">
            <intent-filter>
                <action android:name="com.my.myapplication.wdaw" />
            </intent-filter>
        </receiver>

最后还是使用了动态的注册,我只是单纯记录我的代码,如果有错误的地方可以指出来

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值