如何在不阻塞 UI 线程的情况下转移 Android 资产



我正在尝试按照 android 开发人员培训来转移资产,该培训说要使用此代码:

@Override
public void onDataChanged(DataEventBuffer dataEvents) {
  for (DataEvent event : dataEvents) {
    if (event.getType() == DataEvent.TYPE_CHANGED &&
        event.getDataItem().getUri().getPath().equals("/image")) {
      DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
      Asset profileAsset = dataMapItem.getDataMap().getAsset("profileImage");
      Bitmap bitmap = loadBitmapFromAsset(profileAsset);
      // Do something with the bitmap
    }
  }
}
public Bitmap loadBitmapFromAsset(Asset asset) {
    if (asset == null) {
        throw new IllegalArgumentException("Asset must be non-null");
    }
    ConnectionResult result =
           mGoogleApiClient.blockingConnect(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    if (!result.isSuccess()) {
        return null;
    }
    // convert asset into a file descriptor and block until it's ready
    InputStream assetInputStream = Wearable.DataApi.getFdForAsset(
            mGoogleApiClient, asset).await().getInputStream();
            mGoogleApiClient.disconnect();
    if (assetInputStream == null) {
        Log.w(TAG, "Requested an unknown Asset.");
        return null;
    }
    // decode the stream into a bitmap
    return BitmapFactory.decodeStream(assetInputStream);
}

所以我以大致相同的方式做了同样的事情:

    // Build a new GoogleApiClient for the Wearable API
    googleClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle bundle) {
                    Wearable.DataApi.addListener(googleClient, onDataChangedListener);
                }
                @Override
                public void onConnectionSuspended(int i) {
                }
            })
            .addApi(Wearable.API)
            .build();
    googleClient.connect();

在我的 onDatachanged 方法中,我有:

public DataApi.DataListener onDataChangedListener = new DataApi.DataListener() {
    @Override
    public void onDataChanged(DataEventBuffer dataEvents) {
        Log.d(TAG, "Data changed: " + dataEvents);
        for (DataEvent event : dataEvents) {
            Log.d(TAG, "Data received: " + event.getDataItem().getUri());
            if (event.getType() == DataEvent.TYPE_CHANGED &&
                event.getDataItem().getUri().getPath().equals("/audio")) {
                DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
                Asset audioAsset = dataMapItem.getDataMap().getAsset("audioAsset");
                audioBytes = loadBytesFromAsset(audioAsset);
            }
            // Set play button enabled
            handler.post(onNewAudio());
        }
    }
}

使用我的 loadBytesFromAsset(( 方法:

public byte[] loadBytesFromAsset(Asset asset) {
    if (asset == null) {
        throw new IllegalArgumentException("Asset must be non-null");
    }
    result = googleClient.blockingConnect(3000, TimeUnit.MILLISECONDS);
    if(!result.isSuccess()){
        return null;
    }
    // Convert asset into a file descriptor and block until it's ready
    InputStream assetInputStream = Wearable.DataApi.getFdForAsset(googleClient, asset).await().getInputStream();
    googleClient.disconnect();
    if (assetInputStream == null) {
        Log.w(TAG, "Requested an unknown Asset.");
        return null;
    }
    // Decode the stream into a byte[]
    return getBytesFromInputStream(assetInputStream);
}

这似乎完全符合 Android 开发人员培训的建议,但是当我运行它时,"loadBytesFromAsset(("方法崩溃,并出现异常,指出我无法在 UI 线程上调用 blockingConnect((。有谁知道如何解决这个问题?我应该如何监听然后检索资产?提前谢谢。

好的,我让它工作了(有点(,仍然遇到 onDataChanged 未被调用的问题,但是 UI 线程和 blocking.connect 调用的问题通过重新执行像这篇文章这样的代码来解决。他们这样做的方式是让类实现DataApi.DataListener,GoogleApiClient.ConnectionCallbacks和GoogleApiClient.OnConnectionFailedListener接口,如下所示:

public class MainActivity extends Activity implements
        DataApi.DataListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
    private TextView mTextView;
    private static final long CONNECTION_TIME_OUT_MS = 100;
    private static final String ON_MESSAGE = "On!";
    private static final String OFF_MESSAGE = "Off!";
    private static final String TAG = "Moto360DisplayControl";
    private GoogleApiClient client;
    private String nodeId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initApi();
    }
    private void initApi() {
        client = getGoogleApiClient(this);
        retrieveDeviceNode();
    }
    private GoogleApiClient getGoogleApiClient(Context context) {
        return new GoogleApiClient.Builder(context)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    }
    private void retrieveDeviceNode() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                client.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
                NodeApi.GetConnectedNodesResult result =
                    Wearable.NodeApi.getConnectedNodes(client).await();
                List<Node> nodes = result.getNodes();
                if (nodes.size() > 0) {
                    nodeId = nodes.get(0).getId();
                }
                client.disconnect();
            }
        }).start();
    }
    @Override
    protected void onStart() {
        super.onStart();
        client.connect();
    }
    @Override
    public void onConnected(Bundle connectionHint) {
        Wearable.DataApi.addListener(client, this);
        Toast.makeText(this, "AddedListener!", Toast.LENGTH_LONG).show();
    }
    @Override
    public void onConnectionSuspended(int num) {
        Toast.makeText(this, "ConnectionSuspended", Toast.LENGTH_LONG).show();
    }
    @Override
    public void onConnectionFailed(ConnectionResult res) {
        Toast.makeText(this, "ConnectionFailed", Toast.LENGTH_LONG).show();
    }
    @Override
    protected void onStop() {
        Wearable.DataApi.removeListener(client, this);
        client.disconnect();
        super.onStop();
    }
    @Override
    public void onDataChanged(DataEventBuffer dataEvents) {
        Toast.makeText(this, "DataChanged!", Toast.LENGTH_LONG).show();
        for (DataEvent event : dataEvents) {
            if (event.getType() == DataEvent.TYPE_CHANGED && event.getDataItem().getUri().getPath().equals("/image")) {
                DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
                Asset profileAsset = dataMapItem.getDataMap().getAsset("profileImage");
                Bitmap bitmap = loadBitmapFromAsset(profileAsset);
                // Do something with bitmap
                Toast.makeText(this, "DataChanged!", Toast.LENGTH_LONG).show();
            }
        }
    }
    public Bitmap loadBitmapFromAsset(Asset asset) {
        if (asset == null) {
            throw new IllegalArgumentException("Asset must be non-null");
        }
        ConnectionResult result = client.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
        if (!result.isSuccess()) {
            return null;
        }
        // Convert asset into a file descriptor and block until it's ready
        InputStream assetInputStream = Wearable.DataApi.getFdForAsset(client, asset).await().getInputStream();
        client.disconnect();
        if (assetInputStream == null) {
            Log.w(TAG, "Requested an unknown Asset.");
            return null;
        }
        // Decode the stream into a bitmap
        return BitmapFactory.decodeStream(assetInputStream);
    }
}

使用这种方法,问题得到了解决,我仍在努力解决 onDataChanged 未被调用的问题,我在这里问过这个问题。

(免责声明:我从未真正测试过这个答案,但这只是我阅读API(

请看这个 - https://developers.google.com/android/reference/com/google/android/gms/common/api/GoogleApiClient

你可以看到,除了connectBlocking,你也有connect。文件说

将客户端连接到 Google Play 服务。此方法返回 立即,并在后台连接到服务。如果 连接成功,调用 onConnected(捆绑包(并排队 项被执行。失败时,连接失败(连接结果( 被称为。

因此,您需要做的是调用registerConnectionCallbacks并向其传递实现onConnectedConnectionCallbacks。这些回调将在 UI 线程中运行(就像当前回调在那里运行一样(。此外,您还可以对连接失败时将调用的isConnectionFailedListenerRegistered执行相同的操作。这实际上是您在代码的第一段中已经执行的操作,只是您在构建器中设置了侦听器。

这将需要对您的代码进行一些更改,但我认为不会太严重。

鉴于您拥有的代码结构,在我看来,您正在onConnected((回调中注册onDataChangedListener(这是正确的位置(。在 onDataChangedListener#onDataChanged(( 回调(在主线程上调用(中,您正在调用 loadBytesFromAsset((。在这种方法中,您无需再次连接您的谷歌 api 客户端;此时它应该已经连接,因此无需调用阻塞连接方法。最好检查以确保您已连接(apiClient.isConnected(((,然后继续执行您想做的事情。

在离开应用程序之前也无需断开 API 客户端的连接(事实上,最好不要这样做(,除非您真的确定不想在应用程序中执行任何其他需要连接的操作;最好只在活动的 onStop(( 中调用断开连接(并在 onStart(( 中建立连接(。

也就是说,如果你需要在这些回调(在主线程上(中执行的任何进程是一个漫长的过程,那么你需要生成一个单独的线程(例如,使用 AsyncTask、IntentService 或类似的东西(并在那里处理长进程。

相关内容

  • 没有找到相关文章

最新更新