赞
踩
在备课准备下午去给学生上实验课,本来教材提供了源码,但为了不出意外,还是在自己电脑上完整跑一遍代码,但对着书核对了每行代码,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);
添加完后,第二个错也直接没了,好的,就顺利解决本次问题了!
最后附上我的源代码:
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); //注销接收器
}
}
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();
}
}
<?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>
<?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>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。