正如标题所说,我的广播遇到了麻烦。我得到了一个向主要活动发送一些意图的服务(通过广播接收这些消息)。
我得到了这个日志:
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”)意图是发送和接收的,因为当活动崩溃时,会出现激活蓝牙的“弹出菜单”......所以使用了之前的广播崩溃(尽管收到一个错误说广播不能被实例化......)。
最后是清单:
<?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>
我正在寻找,但我没有找到解决方案。提前致谢