【问题标题】:Android service - intent doesn't update on rebindAndroid 服务 - 重新绑定时意图不更新
【发布时间】:2014-07-07 13:35:54
【问题描述】:

我有一个Activity B,我时不时地启动和停止它。每当我启动B 时,它都会绑定 到从MainActivity 开始的Service,并且在离开B 时将未绑定

每次我再次显示B 时,它都会像这样绑定Service

    Intent musicIntent = new Intent(this, MusicService.class);
    Bundle musicBundle = new Bundle();
    musicBundle.putStringArrayList("playlist", pathList);
    musicBundle.putInt("position", position);

    musicIntent.putExtras(musicBundle);
    getApplicationContext().bindService(musicIntent, mConnection,
            BIND_AUTO_CREATE);

所以onBind()onRebind() 将被调用。

服务:

@Override
public void onRebind(Intent intent) {
    super.onRebind(intent);

    Bundle extras = intent.getExtras();

    this.songPathList = extras.getStringArrayList("playlist"); //Is always the same
    this.position = extras.getInt("position"); //Is always the same
    this.isInitialzed = false; 
}

但是,当我第一次绑定服务并将一些数据传递给它时,它是最终的。意图的数据永远不会改变。所以当onRebind() 被调用时,意图不会改变positionsongPathList

为了更清楚,我第一次调用 B 并通过意图传递一些数据。 position 将设置为 5,songPathList 将设置为一些随机的 5 个字符串。

现在我离开B 并重新开始。这次将调用onRebind()。但是这次positionsongPathList 的值与以前不同。但是当我调试并检查onRebind() 中的值时,它们并没有改变。 position 仍然是 5

我做错了什么?有谁知道为什么意图永远不会改变?

是的,当我再次启动B 时,position 具有不同的值。

【问题讨论】:

    标签: java android android-intent service


    【解决方案1】:

    我没有证据,但我相信如果两个Intents 之间的唯一区别是额外的,那么旧的将被重用,而不是创建一个新的Intent 对象。

    我知道PendingIntents 就是这种情况,不确定普通旧的Intents 是否也是这种情况。

    我通过更新PendingIntent 中的requestCode 解决了PendingIntents 的这一限制,因此有一些明显不同的东西。

    【讨论】:

      【解决方案2】:

      好的,经过一番研究,我有了答案。

      来自“Android 开发者”文档:

      public void onRebind (Intent intent)
      

      在新客户端连接到服务时调用,之前它已在其 onUnbind(Intent) 中通知所有已断开连接。仅当 onUnbind(Intent) 的实现被覆盖以返回 true 时才会调用。

      参数

      intent 用于绑定到此服务的 Intent,与 Context.bindService 相同。 请注意,此时 Intent 中包含的任何额外内容都不会在此处显示。

      我们可以看到 - 我们不应该将任何数据与绑定意图一起传递。

      我真的不知道为什么会这样。我的解决方案是在服务中创建公共方法以在服务绑定到活动后访问它,并在您的 mConnection 的 onServiceConnected 方法中调用这些方法:

      为您服务:

      ...
      private int position;
      private List pathLish;
      
      public void setData(int position, List pathList){
          this.position = position;
          this.pathList = pathList;
          Log.d(LOG_TAG, "Linked Chat Id now "+chatId);
      }
      

      在您的活动中:

       private Boolean bound = false;
      
       private ServiceConnection mConnection = new ServiceConnection() {
          @Override
          public void onServiceConnected(ComponentName componentName, IBinder service) {
              MusicServce.mBinder binder = (MusicServce.mBinder) service;
              musicService= binder.getService();
              musicService.setData(position, pathList);
              bound = true;
          }
      
          @Override
          public void onServiceDisconnected(ComponentName componentName) {
              bound = false;
          }
      };
      
      @Override
      protected void onStart() {
          super.onStart();
          Intent musicIntent = new Intent(this, MusicService.class);
          bindService(musicIntent, mConnection, BIND_AUTO_CREATE);
      }
      

      【讨论】:

        猜你喜欢
        • 2022-07-30
        • 1970-01-01
        • 1970-01-01
        • 2017-06-16
        • 1970-01-01
        • 2021-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多