我正在尝试将AltBeacon库与远程服务中的测距功能一起使用。但是,我现在无法成功地使信标检测成功运行。在声明与本地服务相同的服务时,一切正常。
当服务在自己的进程中运行时,函数startRangingBeaconsInRegion()似乎抛出以下异常:
09-17 17:09:14.643 10709-10729/com.my.project E/Parcel﹕ Class not found when unmarshalling: org.altbeacon.beacon.service.StartRMData
java.lang.ClassNotFoundException: org.altbeacon.beacon.service.StartRMData
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:309)
at android.os.Parcel.readParcelableCreator(Parcel.java:2281)
at android.os.Parcel.readParcelable(Parcel.java:2245)
at android.os.Message.readFromParcel(Message.java:571)
at android.os.Message.access$000(Message.java:32)
at android.os.Message$1.createFromParcel(Message.java:527)
at android.os.Message$1.createFromParcel(Message.java:524)
at android.os.IMessenger$Stub.onTransact(IMessenger.java:51)
at android.os.Binder.execTransact(Binder.java:446)
Caused by: java.lang.ClassNotFoundException: org.altbeacon.beacon.service.StartRMData
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:309)
at android.os.Parcel.readParcelableCreator(Parcel.java:2281)
at android.os.Parcel.readParcelable(Parcel.java:2245)
at android.os.Message.readFromParcel(Message.java:571)
at android.os.Message.access$000(Message.java:32)
at android.os.Message$1.createFromParcel(Message.java:527)
at android.os.Message$1.createFromParcel(Message.java:524)
at android.os.IMessenger$Stub.onTransact(IMessenger.java:51)
at android.os.Binder.execTransact(Binder.java:446)
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
09-17 17:09:14.643 10709-10729/com.my.project W/Binder﹕ Caught a RuntimeException from the binder stub implementation.
android.os.BadParcelableException: ClassNotFoundException when unmarshalling: org.altbeacon.beacon.service.StartRMData
at android.os.Parcel.readParcelableCreator(Parcel.java:2295)
at android.os.Parcel.readParcelable(Parcel.java:2245)
at android.os.Message.readFromParcel(Message.java:571)
at android.os.Message.access$000(Message.java:32)
at android.os.Message$1.createFromParcel(Message.java:527)
at android.os.Message$1.createFromParcel(Message.java:524)
at android.os.IMessenger$Stub.onTransact(IMessenger.java:51)
at android.os.Binder.execTransact(Binder.java:446)
这个问题有解决办法吗?
答案 0 :(得分:1)
Android Beacon Library并非设计为在自己的进程中运行,因此您所描述的内容是不可能的。您可以在此问题讨论中查看更详细的讨论原因:https://github.com/AltBeacon/android-beacon-library/issues/291
答案 1 :(得分:1)
这是一个古老的讨论,但正如我现在发现的那样,我将发布我的解决方案。如果你总是希望你的Beacon服务在它自己的进程中运行,因此你需要altBeacon服务在同一个进程中运行,这是非常可行的。只需让您的服务创建BeaconManager
并将以下内容添加到您的Manifest:
<!-- This is our own BeaconService. We run it in own process, as we don't want it affecting UI, and better crash control.
This also solves the ACRA issue. -->
<service android:name=".BeaconControllerService"
android:process=":my.BeaconControllerService"
android:exported="false"
android:enabled="true">
</service>
<!-- Override beacon library. Set it to our process -->
<receiver android:name="org.altbeacon.beacon.startup.StartupBroadcastReceiver"
android:process=":my.BeaconControllerService"
tools:replace="process" />
<service
android:name="org.altbeacon.beacon.service.BeaconService"
android:process=":my.BeaconControllerService"
tools:replace="process" />
<service
android:name="org.altbeacon.beacon.BeaconIntentProcessor"
android:process=":my.BeaconControllerService"
tools:replace="process" />
到目前为止它对我有用。这也应解决ACRA问题。请记住,当它在自己的进程中运行时,如果要从应用程序绑定到服务,则必须使用Message
类或AIDL绑定器。有关信息,请参阅Android文档。此外,BeaconManager
必须在您的服务类中,而不是您的应用程序。