Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓保持活力IntentService_Android_Multithreading_Sockets_Service_Background - Fatal编程技术网

Android 安卓保持活力IntentService

Android 安卓保持活力IntentService,android,multithreading,sockets,service,background,Android,Multithreading,Sockets,Service,Background,我需要保持一个线程的活动状态,该线程允许我发送数据,即使应用程序处于后台或已被杀死(用户将应用程序滑开或关闭) 我知道我可以使用Service或IntentService。有人告诉我,有了IntentService,我可以使用通知,我正在做的是像Telegram或Whatsapp这样的应用程序,它可以持续监听新消息 我使用了服务,这是我的结果: package test.com.connectionservices.Services; import android.app.Service;

我需要保持一个线程的活动状态,该线程允许我发送数据,即使应用程序处于后台或已被杀死(用户将应用程序滑开或关闭)

我知道我可以使用Service或IntentService。有人告诉我,有了IntentService,我可以使用通知,我正在做的是像Telegram或Whatsapp这样的应用程序,它可以持续监听新消息

我使用了服务,这是我的结果:

package test.com.connectionservices.Services;


import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPListenerService extends Service {
    static String UDP_BROADCAST = "UDPBroadcast";

    DatagramSocket socket;

    private void listenAndWaitAndThrowIntent() throws Exception {
        try {
            DatagramSocket clientSocket = null;
            clientSocket = new DatagramSocket();
            String data ;
            byte[] sendData ;
            InetAddress IPAddress = InetAddress.getByName("AN IP");

            int i = 0;
                while (true) {
                data = "TEST THE SERVICE"+i;
                sendData = data.getBytes();
                DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 21);
                clientSocket.send(sendPacket);
                i++;
            }

        } catch (Exception e) {

        }
    }

    private void broadcastIntent(String senderIP, String message) {
        Intent intent = new Intent(UDPListenerService.UDP_BROADCAST);
        intent.putExtra("sender", senderIP);
        intent.putExtra("message", message);
        sendBroadcast(intent);
    }

    Thread UDPBroadcastThread;

    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();
    }

    private Boolean shouldRestartSocketListen = true;

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

    @Override
    public void onDestroy() {
        stopListen();
    }

    @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;
    }
}
这很好,如果我进入另一个应用程序,或者我用Android(用棒棒糖把它滑开)杀死了这个应用程序,那么在后台会有一个进程淹没接收器


我如何使用IntentService更改它?我为什么要更改,有没有一种方法可以将通知与服务一起使用

您没有理由在此处使用
IntentService
IntentService
将它接收到的启动命令(从调用
startService()
)排队,并在后台线程上串行执行这些命令,然后在队列耗尽时自行停止。这不是你想要的行为,所以不要使用它

要将服务与通知一起使用,请使用方法并提供要显示的通知对象。我不建议这样做,除非用户可以看到一些东西,比如音乐播放服务(通知中有播放控件)