当前位置:   article > 正文

【安卓开发】发送广播报错One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED..._a receiver isn't being registered exclusively for

a receiver isn't being registered exclusively for system broadcasts

在备课准备下午去给学生上实验课,本来教材提供了源码,但为了不出意外,还是在自己电脑上完整跑一遍代码,但对着书核对了每行代码,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);			//注销接收器
    }


}
  • 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
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67

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();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

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>
  • 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

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>
  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/630758
推荐阅读
相关标签
  

闽ICP备14008679号