2

练习的重点是:保持服务活着,将它从一个活动传递到另一个活动。

  1. Activity A 在服务 S 上调用 bindService();
  2. S.onBound() 调用;
  3. A.serviceConnection.onServiceConnected() 被调用;
  4. 活动 A 开始活动 B;
  5. Activity B 在服务 S 上调用 bindService();
  6. B.serviceConnection.onServiceConnected() 被调用;5a:从 onServiceConnected() 活动 B 调用 A.finish();
  7. 活动 A 正在停止,从其 onDestroy() 方法调用 unbindService(S)。

预期行为:服务 S 继续愉快地存在,直到活动 B 调用 unbindService()

实际行为:

  1. S.onUnbind() 被调用;
  2. S.onDestroy() 被调用;
  3. B.serviceConnection.onServiceDisconnected() 被调用;

从而破坏了链接并与文档相矛盾。

为什么?我错过了什么?

更新:已解决。来自http://developer.android.com/reference/android/app/Service.html

服务既可以启动,也可以绑定连接。在这种情况下,只要服务已启动或与 Context.BIND_AUTO_CREATE 标志有一个或多个连接,系统就会保持服务运行。

这是代码:

public class A extends Activity {

    private final Logger logger = LoggerFactory.getLogger(getClass().getSimpleName());

    private String serviceClassName;
    private ServiceConnection feedConnection;
    private Messenger feedMessenger;

    private void bind(String argument) {

        serviceClassName = TheService.class.getName();
        Intent intent = new Intent(serviceClassName);

        intent.putExtra(Keys.ACCOUNT, argument);

        feedConnection = new FeedConnection();

        if (!bindService(intent, feedConnection, Context.BIND_AUTO_CREATE)) {
            throw new IllegalStateException("Failed to bind to " + argument);
        }

        logger.debug("bindService(" + serviceClassName + ") successful");
    }

    private void forward() {

        Intent intentB = new Intent();

        intentB.setClassName(B.class.getPackage().getName(), B.class.getName());
        intentB.putExtra(Keys.SERVICE_CLASS_NAME, serviceClassName);

        startActivity(intentB);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        unbindService(feedConnection);
    }


    private class FeedConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {

            A.this.feedMessenger = new Messenger(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName className) {

            A.this.feedMessenger = null;
            logger.error("Crashed " + Integer.toHexString(hashCode()));
        }
    }
}

public class B extends Activity {

    private final Logger logger = LoggerFactory.getLogger(getClass().getSimpleName());
    private ServiceConnection feedConnection;
    private Messenger feedMessenger;
    private A activityA;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        bindFeed();
    }

    private void bindFeed() {

        Intent startingIntent = getIntent();

        String serviceClassName = startingIntent.getStringExtra(Keys.SERVICE_CLASS_NAME);

        Intent intent = new Intent(serviceClassName);

        feedConnection = new FeedConnection();
        // FIXME: BIND_AUTO_CREATE flag is missing
        if (!bindService(intent, feedConnection, 0)) {
            throw new IllegalStateException("Failed to bind to " + serviceClassName);
        }

        logger.debug("bindService(" + serviceClassName + ") successful");
    }

    private class FeedConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {

            B.this.feedMessenger = new Messenger(service);

            logger.debug("bound " + className);

            // Finish the previous activity only after the service is bound            
            activityA.fileList();
        }

        @Override
        public void onServiceDisconnected(ComponentName className) {

            B.this.feedMessenger = null;
            logger.error("Crashed " + className);
        }
    }
}
4

1 回答 1

2

更新:已解决。来自http://developer.android.com/reference/android/app/Service.html

服务既可以启动,也可以绑定连接。在这种情况下,只要服务已启动或有一个或多个使用 Context.BIND_AUTO_CREATE 标志的连接,系统就会保持服务运行。

在不同的连接处于不对称角色的情况下不是很直观,但唉,就是这样。

于 2012-05-21T05:20:55.397 回答