连接我的活动,服务和BroadcastListener我的、BroadcastListener

2023-09-07 09:58:50 作者:快乐的仙人掌

目前开发我的第一个Android应用程序。我是新来的Andr​​oid,但没有在大学位的Java。 PHP,JavaScript中,C#和我的雇主专有语言(不同于BASIC)是我的背景,使我有一个很好的编程理解。

Developing my first Android app at the moment. I'm new to Android, but did a bit of Java in University. PHP, JavaScript, C# and my employers proprietary language (not unlike BASIC) are my background so I do have a good programming understanding.

该计划是:

Acticity 有一个切换按钮 - 当它启动服务(下)。 A 服务将坐在背景和侦听特定TCP端口上的数据。 A 广播接收器将监视的WiFi状态。 An Acticity with a toggle button - when on it starts the service (below). A Service that will sit in the background and listen for data on a specific TCP port. A BroadcastReceiver that will monitor the WiFi status.

我在的地方全部三个 - 但似乎无法弄清楚如何将它们连接

I have all three in place - but can't seem to figure out how to connect them!

服务应该能够将数据发送回活动。在服务也应该停止,如果活动终止(但如果它只是关闭 - 只有当应用程序是终止。

The Service should be able to send data back to the Activity. The Service should also stop if the Activity is terminated (but not if it is just closed - only if the app is terminated.

广播接收器应能够停止服务并更新活动如果WiFi变为断开。

The BroadcastReceiver should be able to stop the service and update the Activity if the WiFi changes to disconnected.

目前的瞬间彼此独立的所有三个工作半。在活动可以停止和启动服务没有理会。在广播接收器将呈吐司消息,如果无线网络连接断开。它只是互联互通我不知道的。

At the moment all three work semi independently of each other. The Activity can stop and start the Service no bother. The BroadcastReceiver will show a Toast message if the WiFi is disconnected. Its just the inter-communication I'm not sure of.

该应用程序是一个个人项目,我会打开的时候完成采购它。因此我很乐意与任何人谁可以帮助分担目前的code以上。这是一个在 http://www.tip2tail.co.uk/files/android_ code.zip

The app is a personal project and I will be open sourcing it when complete. As such I'm more than happy to share the current code with anyone who can assist. It's at http://www.tip2tail.co.uk/files/android_code.zip

任何帮助是AP preciated :)谢谢!

Any help is appreciated :) Thanks!

推荐答案

一个服务交代。您可以绑定到一个服务,并从中调用方法。

A service explaination. You can bind to a service and call methods from it.

Intent it = new Intent(MainActivity.this, MyService.class);
bindService(it, mConnection, Context.BIND_AUTO_CREATE);

绑定到每个活动的服务,让你可以做任何你想用它。不要忘记您的活动,以解除其的onDestroy。

Bind to the service in each activity so you can do whatever you want with it. Don't forget to unbind in your activity its' onDestroy.

if(mBound)unbindService(mConnection);

和通过调用停止服务。

stopService(new Intent(this, MyService.class));

这是我使用serviceConnection。

This is the serviceConnection I use.

private ServiceConnection mConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className,IBinder service) {
        // We've bound to LocalService, cast the IBinder and get LocalService instance
        LocalBinder binder = (LocalBinder) service;
        mService = binder.getService();
        mBound = true;            
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        mBound = false;
    }
}; 

将这个在服务本身返回活动:

Place this in your service to return itself to the activity:

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

public class LocalBinder extends Binder {
    MyService getService() {
        // Return this instance of LocalService so clients can call public methods
        return MyService.this;
    }
}

最后,这将是开始为您服务 startService(意向)之后调用; 请注意,它返回 Service.START_STICKY 。这将prevent你的服务被杀死,除非操作系统具有非常小的内存。

Finally, this will be called after starting your service startService(intent); Note that it returns Service.START_STICKY. This will prevent your service from being killed, unless the OS has very little memory.

public int onStartCommand(Intent intent, int flags, int startid) {      
    return Service.START_STICKY;
}

我希望我的answere会帮助你,祝你好运吧。

I hope my answere will help you and good luck with it.