如何在Android中将套接字事件作为后台服务处理? [英] How to handle socket events as background service in Android?

查看:79
本文介绍了如何在Android中将套接字事件作为后台服务处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android开发的新手,我希望我的应用即使在后台服务未激活的情况下也能够检测套接字事件(因此我可以进行推送通知,例如,是否有套接字事件触发了新消息,例如Whatsapp和其他人如何做到).

I'm new to Android development and I wanted my app to be able to detect socket events even when app is not active via Background service (so I can do push notification e.g if there's a new message triggered by a socket event like how Whatsapp and others do it).

我实现了后台服务和一个启动该服务的应用程序类,但是将套接字事件作为可运行任务放置在后台服务中的位置和方式.

I implemented Background service and an application class that starts the service but stuck where and how to put the socket events as Runnable task in my Background service.

我修改了下面的socket.io android chat项目示例,并添加了服务和应用程序类.

I modified the socket.io android chat project example below and added service and application class.

ChatApplication.java

package com.github.nkzawa.socketio.androidchat;

import android.app.Application;
import android.content.Intent;
import android.content.res.Configuration;

import io.socket.client.IO;
import io.socket.client.Socket;

import java.net.URISyntaxException;

public class ChatApplication extends Application {

    @Override
    // called when the application is starting, before any other application objects have been created
    public void onCreate() {
        super.onCreate();

        // represents our background service
        Intent background = new Intent(this, SocketBackgroundService.class);
        startService(background);
    }

    private Socket mSocket;
    {
        try {
            mSocket = IO.socket(Constants.CHAT_SERVER_URL);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    public Socket getSocket() {
        return mSocket;
    }
}

SocketBackgroundService.java

package com.github.nkzawa.socketio.androidchat;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class SocketBackgroundService extends Service {
    private boolean isRunning;
    private Thread backgroundThread;

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

    @Override
    public void onCreate() {
        this.isRunning = false;
        this.backgroundThread = new Thread(myTask);
    }

    private Runnable myTask = new Runnable() {
        @Override
        public void run() {
            // do something in here
            Log.i("INFO", "SOCKET BACKGROUND SERVICE IS RUNNING");

            //TODO - how to handle socket events here?
            //How do I do something like mSocket.on(Socket.EVENT_CONNECT,onConnect); here?
        }
    };

    @Override
    public void onDestroy() {
        this.isRunning = false;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if( !this.isRunning) {
            this.isRunning = true;
            this.backgroundThread.start();
        }

        return START_STICKY;
    }
}

推荐答案

即使在应用程序停止后仍需要使套接字保持活动状态.将套接字移至后台服务,然后可以在服务中添加套接字事件.

If you need the socket to be alive even after your application is stopped. Move your socket to the Background service and then you can add the socket events in the service.

这篇关于如何在Android中将套接字事件作为后台服务处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆