Android AIDL 活动和服务问题
我正在编写一个 Android 2.1 应用程序,当外部事件触发其中一个 GPIO 时,它应该向用户显示一个对话框。我已经编写了Linux设备驱动程序,并为其编写了JNI用户空间接口。 当我的应用程序启动时,它会启动一个远程服务来轮询设备驱动程序。通过aidl,我设法在活动和服务之间进行通信,但是,当我关闭活动时,我希望服务重新启动活动。 我使用以下代码实现了这一点:(在服务中)
Intent dialogIntent = new Intent(getBaseContext(),WiegandDemoActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(dialogIntent);
在那之后,我想使用aidl进行通信,所以我使用了:
synchronized (listeners) {
for (WiegandListener listener : listeners) {
try {
listener.handleWiegandUpdated();
...
该活动被带到前面,但是aidl消息没有到达它。 为什么会这样? (当活动在前面时,一切正常)
我不想在这里发布所有代码,因为它很大,但如果您需要更多信息,请随时询问。
谢谢。
I'm writing an Android 2.1 app which supposed to display a dialog to the user when an external event triggers one of the GPIO's. I have written the Linux device driver, and written the JNI user-space interface to it.
When my app start it launches a remote service which polls the device driver. With aidl, I managed to communicate between the activity and the service, however, when I'm closing the activity, I want the service to relaunch the activity.
I achieved that using the following code: (in the service)
Intent dialogIntent = new Intent(getBaseContext(),WiegandDemoActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(dialogIntent);
Right after that, I want to communicate using the aidl, so I used:
synchronized (listeners) {
for (WiegandListener listener : listeners) {
try {
listener.handleWiegandUpdated();
...
The activity is brought to front, however the aidl message doesn't reach it.
why is that happening ? (when the activity is in front, everything works fine)
I didn't want to post all the code here because it's big, but if you need more information don't hesitate to ask.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将需要重新绑定到远程活动。让 Activity 在启动时注册到服务是否可行?这样,当您知道新 Activity 已准备好接收传入消息时,就会触发更新代码?
另外,请务必在活动关闭时取消注册事件侦听器 - 否则它会挂起。如果它包含对实际活动的引用,则整个活动将保留在内存中并且不会收集垃圾(即使它已关闭并且您已经创建了一个新活动)。
You will need to re-bind to the remote Activity. Is it practical to have the Activity register to the service when it starts - so the update code is triggered when you know that the new Activity is ready to receive incoming messages?
Also be sure to un-register your event listener when your activity closes - otherwise it hangs around. If it contains a reference to the actual activity, then the whole activity remains in memory and doesn't get Garbage collected (even though it's closed and you've created a new one).