- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试构建并运行一个用于 react native 应用程序的服务,在该服务运行后,我需要调用一些服务方法,这意味着我需要获取正在运行的服务的实例,所以我正在尝试使用bindservice()。
问题是当我调用bindservice()时,onServiceConnected()和onStartCommand()不会启动,另一方面服务中的onBind()正在启动。
我尝试过的所有答案都没有帮助,这是其中的一部分:
ServiceConnection.onServiceConnected() never called after binding to started service onServiceConnected never called after bindService method ServiceConnection::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>
最佳答案
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;
}
}
我也遇到了同样的问题,然后我发现了你的问题。所以,我想分享我的解决方案。这对我来说很有用。
尽管如此,对你来说已经很晚了,我想帮助别人。
关于java - bindservice 不会启动服务并且 onserviceconnect 不会调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52691549/
我需要一种方法来限制对特定应用程序的服务使用。也就是说,我有一个包含用户可以安装的服务的应用程序和一个配套应用程序。我只希望配套应用能够使用该服务。我使用 bindIntent 调用该服务。 我一直在
我有一个使用远程服务的 Android 应用程序,我使用异步的 bindService() 绑定(bind)到它。 在绑定(bind)服务之前应用程序是无用的,所以我想简单地等到绑定(bind)完成后
在onCreate()我调用主要 Activity 的bindService()与 BIND_AUTO_CREATE标记,然后调用 unbindService()在 onDestroy() .然后我启
这是关于基于内存的 IPC(如 LocalService 示例)但对于在同一进程中运行的两个应用程序: 我有两个应用程序(App1、App2)和一个共享项目(Shared),它为两个应用程序定义了一些
我有一个在 API 14 中工作的项目。现在我正在迁移到 API 21,所以我正在做我需要的更改。 这是一个使用位置来跟踪路线的应用程序。我有一项服务可以处理位置信息。但是当我尝试绑定(bind)到该
以下代码不断抛出空指针异常... public class MainActivity extends Activity implements OnClickListener{ Button but;
我在将服务绑定(bind)到 Activity 时遇到问题。我得到 playing_service==null。我找不到我做错了什么。为什么 playing_service 为空?? MyActivi
我想知道 Context.bindService() 什么时候返回 false? 我试图让 onBind() 返回 null,但当 bindService 时它仍然返回 true > 被调用并且 on
作为 Android 开发的新手,我正在尝试做基础知识并构建一个媒体播放器。它在一项 Activity 中运行良好,但我现在将 MediaPlayer 内容移动到远程服务,以便它可以在 Activit
我正在尝试构建并运行一个用于 react native 应用程序的服务,在该服务运行后,我需要调用一些服务方法,这意味着我需要获取正在运行的服务的实例,所以我正在尝试使用bindservice()。
我正在尝试为 Android 应用程序创建远程服务。我或多或少地从 Android Developers 网站复制了代码,并根据我的需要对其进行了重组。但是,当我进入 bindService 步骤时,
我发现的许多类似问题都没有帮助解决我的问题。 以下是我看过的一些问题: ServiceConnection.onServiceConnected() never called after bindin
我有一个服务单例类,其中包含从同一进程调用的静态方法。我在应用程序启动时调用 startService。我也调用了 bindService,但现在我想知道这是否真的有必要。 最佳答案 这取决于您是否需
我正在实现基于 Dungeons 示例的计费服务(Google 建议您采用这种方式)。以下事实使它稍微复杂一些: 主要服务类位于库项目中(因为我想多次重新设计代码)。作为this stackoverf
我正在尝试开发一个连接 android 和 RedBear BLE shield 的应用程序。 第一次一切正常。但是当我第二次尝试连接到设备时,它不起作用。 OnServiceConnected()
我在 onCreate 中有一个 bindservice 需要很长时间,我试图通过将它移动到工作线程来解决这个问题,但它仍然导致 UI 等待。我能做什么 public void onCreate(Bu
我有一个与此相关的问题 question @mnish 大约一年前问过这个问题。 请看一下他的问题和代码。他实现了一个 ServiceConnection() 并将其传递给 bindService()
从所有代码示例来看,如果我们调用了#bindService,我们只想调用#unbindService(在同一上下文中),这是通过 boolean 检查完成的。 但是在#bindService 调用中没
据我了解,如果我希望服务在不受任何限制的情况下运行,则必须首先使用 startService(Intent i) 启动它。 我的问题是,如果我想在启动后立即绑定(bind)到服务,下面的代码是否可以保
我正在尝试使用 AIDL 开发 2 个应用程序(服务应用程序 + 客户端应用程序)的设置。我目前有 3 个模块的设置: android-agent-framework(仅包含 AIDL 文件的 and
我是一名优秀的程序员,十分优秀!