正如标题所说,我的广播遇到了麻烦。我有一个服务,它将一些意图发送到主要活动(通过广播接收这些消息)。
我得到了这个日志:
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的代码(它被定义为内部类,因此清单中的名称是“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”,而且只能在主要活动中使用。
对服务进行了调用,这是一个射出异常的例子:
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”)意图是发送和接收,因为当活动崩溃时,激活蓝牙的“弹出菜单”出现...所以广播崩溃之前被使用(尽管收到一个错误,说广播无法实现......)。
最后这是Manifest:
<?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>
我正在搜索,但我找不到解决方案。 提前致谢
答案 0 :(得分:1)
您不能将内部类注册为接收器(因为此类只能通过外部类的实例进行实例化,即此类没有默认构造函数)。 但是,您可以在活动中动态注册该类的实例:link
答案 1 :(得分:1)
即使您修改了类名,它仍然无效,因为BroadcastReceiver
不是静态内部类。需要一个活动实例来实例化它,而这是不可能的,你可以在Manifest中定义它。
使用代表BroadcastReceiver
的{{1}}的正确方法是Activity
和Activity#onResume
Activity#onPause
和onCreate()
}。这样,当Activity活动时接收器正在侦听事件,并且可以作为内部类正确地与它交互。
在onResume()
中,您需要创建接收器的实例,然后在final IntentFilter filter = new IntentFilter();
this.populateFilter(filter);
filter.addAction("es.jane.SpeakAction");
...
registerReceiver(this.receiver, filter);
中您需要类似的内容:
onPause()
在unregisterReceiver(this.receiver)
:
{{1}}
答案 2 :(得分:0)
如果我使用BroadcastReceiver,那就麻烦了,它不需要在Manifest中声明。 然后我在活动中使用以下代码声明广播:
private BroadcastReceiver inBroadcast = new 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);
}
}
};
最后,它有效!