【问题标题】:Android Service running in background data lossAndroid服务在后台运行数据丢失
【发布时间】:2015-09-10 10:46:12
【问题描述】:

我有一个使用服务在后台运行的 UDP 服务器/客户端,这些是我的操作:

  • 我启动应用程序
  • 服务启动
  • 我关闭了应用程序,但服务正在后台运行(这是 我想要什么)
  • 我发送 udp 消息,手机正确接收并回复我
  • 我大约 5 分钟不发消息
  • 我发送消息,我的手机不接我
  • 我尝试再次发送消息,我的手机现在可以接听

怎么会这样?当我发送第一条消息时,我的应用程序似乎在睡觉和醒来,但它只能回答第二条消息,或者有时我需要 4-5 条消息才能得到答案,这可能是延迟还是其他?

如果我淹没我的应用程序,它总是会正确回答我,但如果我在一段时间内不发送消息,则会导致问题。

我希望我的应用每次都回复我,即使应用已关闭或手机已锁定。

这是我的代码:

public class UDPListenerService extends Service {

    DatagramSocket socket;
    private Boolean shouldRestartSocketListen = true;
    Thread UDPBroadcastThread;

    private void listenAndWaitAndThrowIntent() throws Exception {
        try {
            byte[] receiveData = new byte[1024];
            byte[] sendData = new byte[1024];
            DatagramSocket serverSocket = new DatagramSocket(9876);

                while (true) {

                    DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                    serverSocket.receive(receivePacket);
                    String sentence = new String( receivePacket.getData());
                    System.out.println("RECEIVED: " + sentence);
                    InetAddress IPAddress = receivePacket.getAddress();
                    int port = receivePacket.getPort();
                    String capitalizedSentence = sentence.toUpperCase();
                    sendData = capitalizedSentence.getBytes();
                    DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
                    serverSocket.send(sendPacket);
                    receiveData = new byte[1024];

            }

        } catch (Exception e) {

        }
    }    

    void startListenForUDPBroadcast() {
        UDPBroadcastThread = new Thread(new Runnable() {
            public void run() {
                try {
                    while (shouldRestartSocketListen) {
                        listenAndWaitAndThrowIntent();
                    }
                    //if (!shouldListenForUDPBroadcast) throw new ThreadDeath();
                } catch (Exception e) {
                    Log.i("UDP", "no longer listening for UDP broadcasts cause of error " + e.getMessage());
                }
            }
        });
        UDPBroadcastThread.start();
    }    

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        shouldRestartSocketListen = true;
        startListenForUDPBroadcast();
        Log.i("UDP", "Service started");
        return START_STICKY;
    }
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {

    }
    @Override
    public void onDestroy() {
        stopListen();
    }
    void stopListen() {
        shouldRestartSocketListen = false;
        socket.close();
    }
}

【问题讨论】:

标签: android sockets service udp server


【解决方案1】:

您的应用停止的原因是服务已被系统置于睡眠状态。也许您需要在调用活动中使用 bindService() 方法。

查看https://developer.android.com/guide/components/services.html 了解有关服务的方法和生命周期的更多信息。

【讨论】:

    猜你喜欢
    • 2014-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    相关资源
    最近更新 更多