【安卓开发】发送广播报错One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED...

在备课准备下午去给学生上实验课,本来教材提供了源码,但为了不出意外,还是在自己电脑上完整跑一遍代码,但对着书核对了每行代码,android studio中还是报错,能在模拟机上安装app,但点开闪退,查了一些资料,有几种说法:
1.没有开广播权限,没有开通知权限等等权限问题;
2.由于安卓版本升级,安全等级变高,需要设置专门的通道

但是在尝试完上述两种对应解决办法后,无果,遂仔细去看了控制台的两个报错

error1:One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn’t being registered exclusively for system broadcasts

针对这个问题去查资料,详见:
Android14 registerReceiver注册广播时报错

其实就在registerReceiver函数的第三个参数写上RECEIVER_EXPORTED或者RECEIVER_NOT_EXPORTED

registerReceiver(myReceiver, intentFilter,RECEIVER_EXPORTED);

添加完后,第二个错也直接没了,好的,就顺利解决本次问题了!

最后附上我的源代码:

MainActivity.java

package com.example.broadcastdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationManagerCompat;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.provider.Settings;

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

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private EditText paramText;
    private TextView resultView;
    private final String ACTION_INTENT_TEST="com.example.broadcastdemo.intent";
    Button sendbtn;
//    Button cancelbtn;
    MyReceiver myReceiver;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        paramText = (EditText) this.findViewById(R.id.param_input);
        resultView = (TextView) this.findViewById(R.id.view_result);
        sendbtn = (Button)findViewById(R.id.send_button);

        myReceiver = new MyReceiver();                                          //实例化广播接收器
        IntentFilter intentFilter = new IntentFilter();                         //新建IntentFilter
        intentFilter.addAction(ACTION_INTENT_TEST);
        registerReceiver(myReceiver, intentFilter,RECEIVER_EXPORTED);                             //注册广播接收器
        //点击按钮,发送广播
        sendbtn.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View arg0)
            {
                //发送广播
                //指定一个操作
                Intent intent = new Intent("com.example.broadcastdemo.intent");
                //绑定参数
                String param = paramText.getText().toString();
                intent.putExtra("user_input", param);
                sendBroadcast(intent);
                resultView.setText("发送广播成功,参数值为:"+param);
            }
        });


    }
    @Override
    public void onStop()
    {
        super.onStop();
        unregisterReceiver(myReceiver);			//注销接收器
    }


}

MyReceiver.java



package com.example.broadcastdemo;


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

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String user_input = intent.getStringExtra("user_input");
        Toast.makeText(context,"接收到广播,得到参数值为"+user_input,Toast.LENGTH_SHORT).show();
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/param_input"/>
    <Button
        android:id="@+id/send_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/param_input"
        android:text="发送" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/param_input"
        android:text="@null"
        android:id="@+id/view_result" />


</RelativeLayout>

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.BroadcastDemo"
        tools:targetApi="31">
        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true">
        </receiver>

        <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>
    </application>

</manifest>
  • 16
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zheng113

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值