Android - 无法实例化 BroadCastReceiver

标签 android android-intent android-broadcast

正如标题所说,我的广播有问题。我得到了一个向主要 Activity 发送一些 Intent 的服务(通过广播接收这些消息)。

我得到了这个日志:

08-03 18:23:11.581: E/AndroidRuntime(776): FATAL EXCEPTION: main
08-03 18:23:11.581: E/AndroidRuntime(776): java.lang.RuntimeException: Unable to instantiate receiver com.example.proyectjane_smartphone.Main.$IncomingBroadcaster: java.lang.ClassNotFoundException: com.example.proyectjane_smartphone.Main.$IncomingBroadcaster in loader dalvik.system.PathClassLoader[/data/app/com.example.proyectjane_smartphone-1.apk]
08-03 18:23:11.581: E/AndroidRuntime(776):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:1777)
08-03 18:23:11.581: E/AndroidRuntime(776):  at android.app.ActivityThread.access$2400(ActivityThread.java:117)
08-03 18:23:11.581: E/AndroidRuntime(776):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
08-03 18:23:11.581: E/AndroidRuntime(776):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-03 18:23:11.581: E/AndroidRuntime(776):  at android.os.Looper.loop(Looper.java:130)
08-03 18:23:11.581: E/AndroidRuntime(776):  at android.app.ActivityThread.main(ActivityThread.java:3687)
08-03 18:23:11.581: E/AndroidRuntime(776):  at java.lang.reflect.Method.invokeNative(Native Method)
08-03 18:23:11.581: E/AndroidRuntime(776):  at java.lang.reflect.Method.invoke(Method.java:507)
08-03 18:23:11.581: E/AndroidRuntime(776):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-03 18:23:11.581: E/AndroidRuntime(776):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-03 18:23:11.581: E/AndroidRuntime(776):  at dalvik.system.NativeStart.main(Native Method)
08-03 18:23:11.581: E/AndroidRuntime(776): Caused by: java.lang.ClassNotFoundException: com.example.proyectjane_smartphone.Main.$IncomingBroadcaster in loader dalvik.system.PathClassLoader[/data/app/com.example.proyectjane_smartphone-1.apk]
08-03 18:23:11.581: E/AndroidRuntime(776):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
08-03 18:23:11.581: E/AndroidRuntime(776):  at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
08-03 18:23:11.581: E/AndroidRuntime(776):  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
08-03 18:23:11.581: E/AndroidRuntime(776):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:1768)
08-03 18:23:11.581: E/AndroidRuntime(776):  ... 10 more

这里是 broadcastReceiver 的代码(它被定义为一个内部类,所以 list 中的名称是“com.example.proyectjane_smartphone.Main.$IncomingBroadcaster”,使用“$”):

class IncomingBroadcaster extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(ACTION_CHANGE_TEXT_IO)) {
            textIO.setText("OUTUT: " + intent.getStringExtra("text"));
        } else if (action.equals(ACTION_SPEAK)) {
            JANEvoi.speak(intent.getStringExtra("speak"));
        } else if (action.equals(ACTION_CHANGE_LIGHT)) {
            if (intent.getBooleanExtra("light", false)) {
                connLight.setImageResource(R.drawable.lighton);
            } else {
                connLight.setImageResource(R.drawable.lightoff);
            }
        } else if (action.equals(ACTION_ENABLE_BT)) {
            Intent enableBtIntent = new Intent(
                    BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent,
                    BluetoothManager.REQUEST_ENABLE_BT);
        }
    }
}

我需要将广播定义为内部类,因为它会更改一些文本并调用“activityforResult”,而且它只能在主 Activity 中使用。

调用服务,这里是一个异常的例子:

public void enableBluetoothAdapter() {
        if (blueManager.getAdapter() == null) {
            Log.d("BLUETOOTHMANAGER", "There's no bluetooth adapter");
        } else if (!blueManager.getAdapter().isEnabled()) {
            Intent enableBTIntent = new Intent(Main.ACTION_ENABLE_BT);
            sendBroadcast(enableBTIntent);
            Log.d("BLUETOOTH", "BLUETOOTH ENCENDIDO");
        }
    }

我无法理解的是(例如使用“ACTION_ENABLE_BT”) Intent 是发送和接收的,因为当 Activity 崩溃时,激活蓝牙的“弹出菜单”出现......所以之前的广播崩溃被使用了(尽管收到一条错误消息说广播不能被实例化......)。

最后是 list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.proyectjane_smartphone"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="10" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.proyectjane_smartphone.Main"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name="com.example.proyectjane_smartphone.Main.$IncomingBroadcaster"
            android:exported="false" >
            <intent-filter>
                <action android:name="es.jane.SpeakAction" />
                <action android:name="es.jane.ChangeTextIOAction" />
                <action android:name="es.jane.ChangeLight" />
                <action android:name="es.jane.EnableBT" />
            </intent-filter>
        </receiver>

        <service android:name="com.example.proyectjane_smartphone.Bluetooth.BluetoothService" >
        </service>
    </application>

</manifest>

我正在搜索,但没有找到解决方案。 提前致谢

最佳答案

即使您修复了类名,它仍然无法工作,因为 BroadcastReceiver 不是静态内部类。需要一个 Activity 实例来实例化它,而这对于您如何在 list 中定义它是不可能的。

使用代表Activity 工作的BroadcastReceiver 的正确方法是registerunregister它在 Activity#onResumeActivity#onPause 中。这样,当 Activity 处于 Activity 状态时,接收者就会监听事件,并且可以作为内部类与其正确交互。

onCreate() 中,您需要创建接收器的实例,然后在 onResume() 中,您需要类似于以下内容的内容:

final IntentFilter filter = new IntentFilter();
this.populateFilter(filter);
filter.addAction("es.jane.SpeakAction");
...
registerReceiver(this.receiver, filter);

onPause() 中:

unregisterReceiver(this.receiver)

关于Android - 无法实例化 BroadCastReceiver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18034908/

相关文章:

kotlin - 如何将 registerForActivityResult 与 StartIntentSenderForResult 契约(Contract)一起使用?

Android:BroadcastListner 获取错误

android - 当链接也通过深度链接重定向时在浏览器中打开

android - Android 设备重启后广播接收器不工作

安卓.content.res.Resources$NotFoundException : String resource id #0x64

android - 适用于 Android 的蒂芙尼框架

android - JSONObject 无法转换为 JSONArray

android - GcmListenerService.onMessageReceived 未调用

Android:Eclipse 下有哪些分析工具可用于查看可伸缩性?

android - 发送带有 Intent 的电子邮件附件 android。仅适用于 Gmail