android - 绑定(bind)/取消绑定(bind)服务示例 (android)

标签 android binding service

你能给我一个带有后台服务的应用程序的简单示例,它使用绑定(bind)/取消绑定(bind)方法来启动和停止它吗?我在谷歌上搜索了半个小时,但这些示例使用 startService/stopService 方法或者对我来说非常困难。谢谢。

最佳答案

您可以尝试使用此代码:

protected ServiceConnection mServerConn = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder binder) {
        Log.d(LOG_TAG, "onServiceConnected");
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        Log.d(LOG_TAG, "onServiceDisconnected");
    }
}

public void start() {
    // mContext is defined upper in code, I think it is not necessary to explain what is it 
    mContext.bindService(intent, mServerConn, Context.BIND_AUTO_CREATE);
    mContext.startService(intent);
}

public void stop() {
    mContext.stopService(new Intent(mContext, ServiceRemote.class));
    mContext.unbindService(mServerConn);
}

关于android - 绑定(bind)/取消绑定(bind)服务示例 (android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8341667/

相关文章:

java - 添加谷歌 jar 后出现 IllegalArgumentException 错误

安卓系统.ErrnoException : ioctl failed: EBADF (Bad file descriptor) with file from data/data folder

c# - 调用绑定(bind)方法时无法识别的选择器

c# - 如何打开服务属性对话框

javascript - 如何通过ID从两个服务获取数据并显示内容

WCF 服务名称和绑定(bind)名称

android - Gradle 不会按要求排除模块

java - 导出导入 Android Studio AVD

ios - 如何在 Xamarin iOS 中使用 MvvmCross 将 UITableView 绑定(bind)到 List<string>

c# - WPF绑定(bind)改变椭圆的填充颜色