【问题标题】:bindservice wont start the service and onserviceconnect wont callbindservice 不会启动服务并且 onserviceconnect 不会调用
【发布时间】:2018-10-07 18:24:00
【问题描述】:

我正在尝试为 react native 应用程序构建和运行服务,在服务运行后我需要调用一些服务方法,这意味着我需要获取正在运行的服务的实例,所以我正在尝试使用 bindservice()。

问题是,当我调用 bindservice() 时,onServiceConnected() 和 onStartCommand() 不会启动,另一方面,服务中的 onBind() 正在启动。

我尝试过的所有答案都没有帮助,这是其中的一部分:

ServiceConnection.onServiceConnected() never called after binding to started service onServiceConnected never called after bindService methodServiceConnection::onServiceConnected not called even though Context::bindService returns true? OnServiceConnected not getting called

提前致谢。


这里是服务:

public class TestService extends Service {
    private LocalBinder mBinder = new LocalBinder();
    private Intent serviceIntent;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    Log.i("service", "service starting!!@!@!@ ");

    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.i("EXIT", "ondestroy!");
    Intent broadcastIntent = new Intent("com.testapp.service.RestartTest");
    sendBroadcast(broadcastIntent);
}

@Override
public void onTaskRemoved(Intent rootIntent) {
    Intent broadcastIntent = new Intent("com.testapp.service.RestartTest");
    sendBroadcast(broadcastIntent);
    super.onTaskRemoved(rootIntent);
}

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

public class LocalBinder extends Binder {
    TesService getService() {
        return TestService.this;
    }
}

}

react native 的包:

public class TestPackage implements ReactPackage {

private ReactApplicationContext applicationContext;
TestService runningService;
boolean mBound = false;

@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
    return Collections.emptyList();
}

@Override
public List<NativeModule> createNativeModules(
        ReactApplicationContext reactContext) {
    List<NativeModule> modules = new ArrayList<>();
    runningService = new TestService(reactContext);
    Intent serviceIntent = new Intent(reactContext, runningService.getClass());
    reactContext.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE);
    modules.add(runningService.someMoudle());
    return modules;
}

/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className,
            IBinder service) {
runningService = ((LocalBinder) service).getService();
        mBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        mBound = false;
    }
};
}

AndroidManifest.xml 包含

<service
    android:name=".TestService">
</service>

【问题讨论】:

  • 如果您不拨打startService,您将不会收到onStartCommand
  • 感谢您的快速响应,但据我了解 bindservice 应该启动服务.. 但无论如何我也尝试使用 startservice 并且它没有启动 onserviceconnected

标签: java android react-native service bindservice


【解决方案1】:

API.java

public class Api extends ReactContextBaseJavaModule {
     public KeyStoreApi(ReactApplicationContext reactContext){
        super(reactContext);
    }

    ....

    @ReactMethod
    public void connect(Promise promise){

        ServiceConnection mServiceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                ...
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                ...
            }
        };

        Intent intent = new Intent();
        intent.setAction(...);
        intent.setClassName(...);
        getReactApplicationContext().bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);

    }
}

ApiPackage.java

public class KeyStoreApiPackage implements ReactPackage {

    @Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext){
        return Collections.emptyList();
    }

    @Override
    public List<NativeModule> createNativeModules(ReactApplicationContext reactContext){
        List<NativeModule> modules = new ArrayList<>();
        modules.add(new Api(reactContext));
        return modules;
    }
}

我有同样的问题,然后我找到了你的问题。 所以,我想分享我的解决方案。 这对我有用。

尽管如此,对你来说已经很晚了, 我想帮助某人。

【讨论】:

  • 您确实做得很好,先生:D
猜你喜欢
  • 1970-01-01
  • 2019-03-05
  • 1970-01-01
  • 2015-12-17
  • 1970-01-01
  • 2014-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多