Class 在后台启动服务时引用 null

Class references null when starting service in background

我正在尝试在我的 android 应用程序中实现 "never-ending background service"。它应该用于等待服务器向客户端发送某种类型的对象,并在发生这种情况时通知用户。作为模板,我使用 fabcirablog。 当我尝试以意图启动服务时,我必须在 onStartCommand 中使用的引用这些引用似乎为空。

我尝试在服务构造函数中一个一个地设置变量,并在 onStartCommand 本身中获取它们。每次它们都会导致空引用。

主要活动:

protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);

    ...

    if (clientHandler == null)
        clientHandler = new ClientHandler(this, this);

    if (clientService == null) {
        clientService = new ClientService(this, this);
        mServiceIntent = new Intent(this, clientService.getClass());

        if (!isMyServiceRunning(clientService.getClass()))
            startService(mServiceIntent);                            //somehow creates a new ClientService instance with null variables such as mainActivity, clientHandlder...
    }
}

服务:

构造函数:

public ClientService(Context applicationContext, MainActivity mainActivity) {
    super();

    this.mainActivity = mainActivity;
    client = mainActivity.getClient();
}

onStartCommand:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);

    client.addListener(new Listener(){              //FIXME client is null (a mainActivity would be null too)
        @Override
        public void connected(Connection connection) {
            super.connected(connection);
        }

        @Override
        public void disconnected(Connection connection) {
            super.disconnected(connection);
        }

        @Override
        public void received(Connection connection, Object o) {
            Log.i("SERVICE", "Received: " + o.toString());
        }
    });

    client.start();
    Network.register(client);

    return START_STICKY;
}

错误日志:

java.lang.RuntimeException: Unable to start service com.simplecodings.smma.tools.ClientService@b6efbc2 with Intent { cmp=com.simplecodings.smma/.tools.ClientService }: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.esotericsoftware.kryonet.Client.addListener(com.esotericsoftware.kryonet.Listener)' on a null object reference
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3343)
    at android.app.ActivityThread.-wrap21(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1582)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.esotericsoftware.kryonet.Client.addListener(com.esotericsoftware.kryonet.Listener)' on a null object reference
    at com.simplecodings.smma.tools.ClientService.onStartCommand(ClientService.java:50)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3326)
    at android.app.ActivityThread.-wrap21(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1582) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

我可以吗,例如通过 .putExtra?

传递它们来引用客户端/mainActivity

阅读 this 并使用方法 "Extending the Binder class" 实现绑定服务。您尝试在 ActivityService 之间进行通信的方式不正确。

拥有永无休止的后台服务也是not possible。您可能会逃脱使用前台服务。您应该考虑不在 Service 中监视您的服务器,而是使用 Firebase Cloud Messaging 等推送消息在您的服务器发生某些事情时通知您的应用程序。

您的 Activity/Service class 成员如 this.mainActivitythis.client 为空,因为 android 可能已决定杀死并重新创建 Activity/Service(即如果你旋转设备)因此那些成员要么是空的要么指向已经 destoyed Activities/Services