Android:使2个服务进行通信
我的应用程序当前有 2 项服务和 1 项活动。我的第一个服务称为传感器,仅返回手机传感器的所有值。然后我有第二个名为 Waypoint 的服务,它必须向我的 Arduino 卡发送命令,以便我的机器人到达某个航点。因此,在某种程度上,传感器将值发送到 Waypoint。此外,我将 Waypoint 绑定到我的 Activity 以“查看”发送的命令。但是,我不知道如何使我的 2 个服务进行通信(如何将传感器的值发送到我的 Waypoint 服务?),有人有相关教程吗?我只看到了有关服务和活动之间通信的教程...
谢谢大家!
PS:抱歉我的英语...
编辑:
谢谢您的回答!
我只是按照你们俩所说的做了,但似乎不起作用(我没有错误,但没有显示任何内容,我想给你我的代码,但它太长了......)。好吧,如果你有勇气:http://paste.tgl0be.org/?id=10458。
这就是我想要做的:
- 我的传感器服务获取所有传感器的值,并使用 sendBroadcast 方法发送方位角和启动服务布尔值。
- Waypoint 有一个广播接收器,它获取传感器服务发送的值并更新其自己的变量(因此这里只有方位角并启动)
- Connexion 显示方位角的值
但没有显示任何内容...有人知道这个问题吗? (显然,有人读过我的代码)
我不知道我是否尊重论坛规则,事实上,我认为我没有回答我的问题...如果是这样,对不起...
再次为我的英语感到抱歉
新编辑:
我终于可以得到一些结果,实际上总是 0...我想知道我的接收器是否很好地声明到我的 AndroidManifest 中,因为我的接收器是 Waypoint 的内部类,这是我的代码:
<?xml version="1.0" encoding="utf-8"?>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Connexion"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver class=".Waypoint$Receiver" android:name=".Waypoint$Receiver">
<intent-filter>
<action android:name="android.intent.action.EVENT_ACTION" />
</intent-filter>
</receiver>
<service android:name=".Sensors" android:enabled="true"/>
<service android:name=".Waypoint" android:enabled="true"/>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.LOCATION" />
我认为不是,它可以解释问题。 “$”符号用于内部静态类,而我的接收器不是静态的。有谁知道如何在 AndroidManifest 中声明非静态内部类?
My application has currently 2 services and 1 activity. My first service, called sensor, just return all the values of the sensors of the phone. Then I have a second service called Waypoint which is bound to send commands to my Arduino card for my robot to reach a waypoint. So, in a way, Sensor send the values to Waypoint. Moreover, I bound Waypoint to my Activity to "see" the commands which are sent. However, I don't know how to make my 2 services communicate (how can I send the values of the sensors to my Waypoint service ? ), would anyone have a tutorial about that ? I only saw tutorial about communication between services and activities...
Thank you all !
PS : Sorry for my english...
EDIT :
Thank you for your answers !
I just did what you both said but it seems not to work (i've no error, but nothing get displayed, i'd like to give you my code, but it's too long...). Well, if you have courage : http://paste.tgl0be.org/?id=10458.
Here is what i'm trying to do :
- My sensors service get the values of all sensors and send the azimuth and the started-service boolean using the sendBroadcast method.
- Waypoint has a Broadcast receiver which get the value sensors services send and update its own variables (so here only the azimuth and started)
- Connexion display the value of azimuth
But nothing get displayed... Would anyone have an idea about the problem ? (someone who read my code, obviously)
I don't know if I respected the forum rules, indeed I don't think I ANSWERED my question... If so, sorry...
Again sorry for my english
New edit :
I can finally get some results, always 0 in fact... I wondered if my receiver is well declared into my AndroidManifest, as my receiver is an inner class of Waypoint, here is my code:
<?xml version="1.0" encoding="utf-8"?>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Connexion"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver class=".Waypoint$Receiver" android:name=".Waypoint$Receiver">
<intent-filter>
<action android:name="android.intent.action.EVENT_ACTION" />
</intent-filter>
</receiver>
<service android:name=".Sensors" android:enabled="true"/>
<service android:name=".Waypoint" android:enabled="true"/>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.LOCATION" />
I think it's not and it could explain the problem. The "$" symbol is used for an inner static class, and my Receiver is not static. Does anyone know how to declare a non-static inner class in the AndroidManifest ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您的 Sensor 服务与 Waypoint 服务异步通信,因此您可以让 Sensor 服务发送带有额外数据(传感器数据)的广播,并且 Waypoint 服务将注册 BroadcastReceiver 以获取这些广播。
Since your Sensor service communicates asynchronously with Waypoint service, you could have Sensor service send broadcast with extra data (sensor data) and Waypoint service would register BroadcastReceiver to get those broadcasts.
您可以对“活动”使用相同的通信技术<-> 2 项服务之间的服务。我建议 http://developer.android.com/reference/android /app/Service.html#LocalServiceSample 作为一种方法:传感器服务将绑定到 Waypoint 服务,然后可以向其发送值。
You can use the same communication techniques for Activity <-> Service between 2 services. I'd suggest http://developer.android.com/reference/android/app/Service.html#LocalServiceSample as one approach: the Sensor service will bind to the Waypoint service and can then send the values to it.