Android React Native-如何创建后台服务

Android React Native-如何创建后台服务,android,react-native,Android,React Native,我期待着将我的应用程序从本机切换到rect本机。我有一个应用程序,即使在应用程序关闭时也可以跟踪用户的GPS位置。如何在react native中实现这一点 我查看了Headless JS,并尝试了以下内容,但它不起作用 明显地 <application> ... <service android:name="com.BackgroundService" /> ... </application> SomeTaskName.js import SocketI

我期待着将我的应用程序从本机切换到rect本机。我有一个应用程序,即使在应用程序关闭时也可以跟踪用户的GPS位置。如何在react native中实现这一点

我查看了Headless JS,并尝试了以下内容,但它不起作用

明显地

<application>
...
<service android:name="com.BackgroundService" />
...
</application>
SomeTaskName.js

import SocketIOClient from 'socket.io-client';
module.exports = async (taskData) => {
    // do stuff
    var socket = SocketIOClient('http://xxx.xx.xx.xx:4000');
    socket.emit("test");
}
BackgroundService.java

package com;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.facebook.react.HeadlessJsTaskService;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.jstasks.HeadlessJsTaskConfig;
public class BackgroundService extends HeadlessJsTaskService {

  @Override
  protected @Nullable
  HeadlessJsTaskConfig getTaskConfig(Intent intent) {
    Bundle extras = intent.getExtras();
    if (extras != null) {
      return new HeadlessJsTaskConfig(
          "SomeTaskName",
          Arguments.fromBundle(extras),
          5000);
    }
    return null;
  }
}
MainActivity.java

package com.geolocation;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;

import com.BackgroundService;
import com.facebook.react.ReactActivity;

public class MainActivity extends ReactActivity {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        Intent service = new Intent(getApplicationContext(), BackgroundService.class);
        this.startService(service);
    }

    /**
     * Returns the name of the main component registered from JavaScript.
     * This is used to schedule rendering of the component.
     */

    @Override
    protected String getMainComponentName() {
        return "geolocation";
    }
}

你有解决这个问题的办法吗?
package com.geolocation;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;

import com.BackgroundService;
import com.facebook.react.ReactActivity;

public class MainActivity extends ReactActivity {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        Intent service = new Intent(getApplicationContext(), BackgroundService.class);
        this.startService(service);
    }

    /**
     * Returns the name of the main component registered from JavaScript.
     * This is used to schedule rendering of the component.
     */

    @Override
    protected String getMainComponentName() {
        return "geolocation";
    }
}