Android:使2个服务进行通信

发布于 2024-11-05 00:39:58 字数 2309 浏览 1 评论 0原文

我的应用程序当前有 2 项服务和 1 项活动。我的第一个服务称为传感器,仅返回手机传感器的所有值。然后我有第二个名为 Waypoint 的服务,它必须向我的 Arduino 卡发送命令,以便我的机器人到达某个航点。因此,在某种程度上,传感器将值发送到 Waypoint。此外,我将 Waypoint 绑定到我的 Activity 以“查看”发送的命令。但是,我不知道如何使我的 2 个服务进行通信(如何将传感器的值发送到我的 Waypoint 服务?),有人有相关教程吗?我只看到了有关服务和活动之间通信的教程...

谢谢大家!

PS:抱歉我的英语...

编辑:

谢谢您的回答!

我只是按照你们俩所说的做了,但似乎不起作用(我没有错误,但没有显示任何内容,我想给你我的代码,但它太长了......)。好吧,如果你有勇气:http://paste.tgl0be.org/?id=10458

这就是我想要做的:

  1. 我的传感器服务获取所有传感器的值,并使用 sendBroadcast 方法发送方位角和启动服务布尔值。
  2. Waypoint 有一个广播接收器,它获取传感器服务发送的值并更新其自己的变量(因此这里只有方位角并启动)
  3. 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 :

  1. My sensors service get the values of all sensors and send the azimuth and the started-service boolean using the sendBroadcast method.
  2. Waypoint has a Broadcast receiver which get the value sensors services send and update its own variables (so here only the azimuth and started)
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一抹苦笑 2024-11-12 00:39:58

由于您的 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.

泅渡 2024-11-12 00:39:58

您可以对“活动”使用相同的通信技术<-> 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文