【问题标题】:Local Service Binding Code from Activity to Service从活动到服务的本地服务绑定代码
【发布时间】:2012-01-15 10:05:58
【问题描述】:

用于绑定服务的客户端代码,通常在活动类中;我正在尝试将其移至服务类,以便活动类尽可能干净和小。

即基本上是在尝试合并the code in the second box here into the first box = as much of it into the service class as possible

Activity 中用于绑定到服务的单行

public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // Bind to service with this line only:
    AService.bindService(this);
}
}

静态 bindService 和 ServiceConnection 移到服务中

public class AService extends Service {

public String test = "I want to see this";
public static AService aService;
private static boolean isBound;
private static Context context;

// ... IBinder, onBind etc also here on service side

public static void bindService(Context context) {
    try {
        Log.i(TAG, "bindService Start");
        if (!isBound && context != null) {
            Log.i(TAG, "Binding");
            context.bindService(new Intent(context, AService.class),
                    serviceConnection, Context.BIND_AUTO_CREATE);
            isBound = true;
            Log.i(TAG, "Bound");
        }
    } catch (Exception e) {
        Log.e(TAG, "bindService", e);
    }
}

private static ServiceConnection serviceConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        try {
            Log.i(TAG, "onServiceConnected Start");
            aService = ((AService.LocalBinder) service).getService();
            if (aService != null)
                Log.i(TAG, aService.test);
            Log.i(TAG, "onServiceConnected Finish");
        } catch (Exception e) {
            Log.e(TAG, "onServiceConnected", e);
        }
    }

    public void onServiceDisconnected(ComponentName className) {
        try {
            Log.i(TAG, "onServiceDisconnected");
            aService = null;
        } catch (Exception e) {
            Log.e(TAG, "onServiceDisconnected", e);
        }
    }
};


public static void unbind() {
    try {
        Log.i(TAG, "unbind start");
        if (isBound && context != null) {
            Log.i(TAG, "Unbinding");
            context.unbindService(serviceConnection);
            isBound = false;
            context = null;
            Log.i(TAG, "Unbound");
        }
    } catch (Exception e) {
        Log.e(TAG, "unbind", e);
    }
}

}

但从未调用过 onServiceConnected?

日志显示以下所有内容:

...
Bound
  • 不是 onServiceConnected Start 或以后
  • 没有例外。
  • 请注意,当相同的代码在 Activity 中时,它可以工作(使用 MyActivity.this 调用时)

我做错了什么?

【问题讨论】:

    标签: java android service android-context oncreate


    【解决方案1】:

    这是

    AService.bindService(this);
    

    比这好多了?

    bindService(new Intent(context, AService.class),
                    serviceConnection, Context.BIND_AUTO_CREATE);
    

    ServiceConnection 的实现真的在 Activity 中让你很烦恼吗?我不信。

    我没有看到任何点将所有内容集中到 Service 中,然后在实际 Service 中调用静态方法从 Activity 启动此 Service。最佳做法是遵循 Google 推荐的标准做事方式,以您的方式执行此操作,会使您的代码在阅读您的代码时变得晦涩难懂并让其他人感到困惑(如果您在团队中工作)。 IMO 没有任何意义。

    与其把所有的精力都放在将服务与活动隔离开来,我宁愿更多地考虑如何将业务逻辑与活动隔离并将它们集中到服务中,并让活动主要关注 UI 的东西。

    真的希望对你有帮助。

    【讨论】:

    • 我让我的移动/代码工作,结果发现有一些奇怪的刷新问题或者我真的不知道是什么,当我手动卸载应用程序并杀死 adb 然后重新安装时,绑定开始起作用了。
    • 在混淆人们方面,我认为遵循 Google 推荐的方式实际上会造成更多的混淆,特别是因为我在 Android 库中提供我的服务,而该库的用户可能对服务知之甚少等等。我应该让他们尽可能简单地访问他们感兴趣的库功能,而不是让他们负担使用库所需的额外样板代码。尽管我本质上不同意你的观点,但我会接受你的回答,因为你是唯一一个试图解决问题的人。谢谢!
    • @Cel,很高兴看到你自己弄清楚了,尽管我保持我的观点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多