【问题标题】:IllegalStateException when calling blockingConnect in wear Service在磨损服务中调用blockingConnect时出现IllegalStateException
【发布时间】:2018-08-13 01:41:16
【问题描述】:

我正在尝试开发一个 Android 穿戴应用程序并从移动助手活动中传递一个资产。我遵循了传递资产的官方文档 - Transferring Assets,但在尝试从 onDataChanged 函数中传递的资产加载位图时出现以下错误。请注意,代码在传递 String 值时有效。代码如下:

public Bitmap loadBitmapFromAsset(Bitmap bitmap, Asset asset) {
    if (asset == null) {
        throw new IllegalArgumentException("Asset must be non-null");
    }

    ConnectionResult result = mGoogleApiClient.blockingConnect(5000, TimeUnit.MILLISECONDS);
    if (!result.isSuccess()) {
        return null;
    }

    InputStream assetInputStream = Wearable.DataApi.getFdForAsset(mGoogleApiClient, asset).await().getInputStream();
    mGoogleApiClient.disconnect();

    if (assetInputStream == null) {
        return null;
    }

    if (bitmap != null) {
        bitmap.recycle();
        bitmap = null;
    }

    bitmap = BitmapFactory.decodeStream(assetInputStream);
    return bitmap;
}

我得到的错误如下:

java.lang.IllegalStateException: 不能在 UI 线程上调用blockingConnect 在 com.google.android.gms.common.internal.zzx.zza(未知来源) 在 com.google.android.gms.common.api.internal.zzj.blockingConnect(未知来源) ...

关于造成这种情况的任何想法?

【问题讨论】:

    标签: android assets wear-os illegalstateexception


    【解决方案1】:

    最终解决方案是在可运行文件中执行 loadBitmapFromAsset。不明白为什么我没有早点解决这个问题......我还没有找到类似的帖子,所以希望它会对某人有所帮助。

    new Thread(new Runnable() {
        @Override
        public void run() {
        ...
        }
    }).start();
    

    【讨论】:

      【解决方案2】:

      错误消息的含义正是它所说的:您不能在应用程序的 UI 线程上调用 GoogleApiClient.blockingConnect。连接GoogleApiClient前需要设置回调,然后在onConnected回调中进行处理。

      http://developer.android.com/training/wearables/data-layer/accessing.html 有一个很好的例子。您缺少的部分在 addConnectionCallbacks 调用中。

      【讨论】:

      • 谢谢,但这就是我正在做的。表盘服务实现以下功能,DataApi.DataListener、GoogleApiClient.ConnectionCallbacks 和 GoogleApiClient.OnConnectionFailedListener。当调用 onDataChanged 函数(更改移动伴侣应用程序上的某些设置后)并且我尝试从传递的资产中加载位图时,就会出现问题。我使用的函数与相关文档中的相同函数(loadBitmapFromAsset):developer.android.com/intl/ru/training/wearables/data-layer/…
      【解决方案3】:

      把这个放到Activity中创建异步任务

      class doAsync(val handler: () -> Unit) : AsyncTask<Void, Void, Void>() {
          init {
              execute()
          }
          override fun doInBackground(vararg params: Void?): Void? {
              handler()
              return null
          }
      }
      

      现在在 onResume 或 onCreate 中调用它

       doAsync{
              mGoogleApiClient?.blockingConnect()
              Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(object : ResultCallback<Status> {
                  override fun onResult(status: Status) {
                      mAuth?.signOut()
      
                  }
      
              })
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-01
        • 2021-10-11
        • 1970-01-01
        相关资源
        最近更新 更多