- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我不确定如何解决这个问题,但在一个 Activity 中我调用了 startService,然后立即调用以启动下一个 Activity。
这有效,服务启动,并开始按预期处理数据。
我转到下一个 Activity,并在 onResume
中调用 AsyncTask
来绑定(bind)服务。
所以,基本流程是我调用 AsyncTask,bindService
返回 false,因此永远不会调用 mConnection。
那么,问题是为什么 bindService
返回 false?
我将绑定(bind)放在 AsyncTask 中的原因是我让线程在绑定(bind)前休眠 10 秒,以查看是否需要先启动服务。
我也在这个 Activity 中启动了服务,首先,在 onCreate 方法中,等了 10 秒,但是 bindService
仍然返回 false。
private class BindServiceTask extends AsyncTask<Void, Void, Boolean> {
protected Boolean doInBackground(Void... params) {
return bindService(
new Intent(IMyCallback.class.getName()),
mConnection, Context.BIND_AUTO_CREATE);
}
protected void onPostExecute(Boolean b) {
if (b) {
Log.i(TAG, "onResume - binding succeeded");
Toast.makeText(mContext, "Bound to service succeeded",
Toast.LENGTH_LONG).show();
} else {
Log.i(TAG, "onResume - binding failed");
Toast.makeText(mContext, "Bound to service failed",
Toast.LENGTH_LONG).show();
}
}
}
private IMyCallback mCallback = new IMyCallback.Stub() {
@Override
public void dataChanged(double[] info) throws RemoteException {
mHandler.sendMessage(mHandler.obtainMessage(LOCATION_MSG, info));
}
};
IMyService mIRemoteService;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
Log.i(TAG, "onServiceConnected");
mIRemoteService = IMyService.Stub.asInterface(service);
try {
Log.i(TAG, "registering callback");
mIRemoteService.registerCallback(mCallback);
} catch (RemoteException e) {
Log.e(TAG, e.toString());
}
}
public void onServiceDisconnected(ComponentName className) {
Log.e(TAG, "Service has unexpectedly disconnected");
mIRemoteService = null;
}
};
最佳答案
问题似乎是 list 的问题。
我缺少 intent-filter 来告诉系统可以绑定(bind)哪些服务:
<service
android:name=".MyService"
android:process=":remote" >
<intent-filter>
<!--
These are the interfaces supported by the service, which
you can bind to.
-->
<action android:name="my.com.services.IMyCallback" />
<action android:name="my.com.services.IMySecondaryService" />
<!--
This is an action code you can use to select the service
without explicitly supplying the implementation class.
-->
<action android:name="my.com.activity.MY_SERVICE" />
</intent-filter>
</service>
关于android - 在上一个 Activity 中调用 startService 时 bindService 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9155484/
我需要一种方法来限制对特定应用程序的服务使用。也就是说,我有一个包含用户可以安装的服务的应用程序和一个配套应用程序。我只希望配套应用能够使用该服务。我使用 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
我是一名优秀的程序员,十分优秀!