如何在不阻止UI线程的情况下传输Android资产?

时间:2015-12-23 21:35:23

标签: android google-api wear-os android-assets android-wear-data-api

我试图通过遵循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()。有谁知道如何解决这个问题?我该如何倾听并检索资产?提前谢谢。

3 个答案:

答案 0 :(得分:2)

好吧,我让它工作(有点),仍然遇到onDataChanged未被调用的问题,但是通过重新执行像这篇文章{{3}这样的代码来解决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未被调用的问题,我已经问过here

答案 1 :(得分:1)

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

请参阅此内容 - https://developers.google.com/android/reference/com/google/android/gms/common/api/GoogleApiClient

您可以看到connectBlocking旁边还有connect。文档说

  

将客户端连接到Google Play服务。此方法返回   立即,并在后台连接到服务。如果   连接成功,onConnected(Bundle)被调用并入队   项目被执行。失败时,onConnectionFailed(ConnectionResult)   被称为。

所以你需要做的就是致电registerConnectionCallbacks并传递ConnectionCallbacks来实现onConnected。这些回调将在UI线程中运行(就像当前的回调在那里运行一样)。此外,您还可以对isConnectionFailedListenerRegistered执行相同操作,这将在连接失败时调用。 这实际上就是你在代码的第一段中所做的事情,只是你在构建器中设置了监听器。

这需要对代码进行一些更改,但我认为没有太严肃的事情。

答案 2 :(得分:0)

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

除非您确定不想在应用中执行任何其他需要连接;最好只调用活动的onStop()中的断开连接(并在onStart()中建立连接)。

那就是说,如果你需要在那些回调中进行的任何进程(在主线程上)是一个很长的进程,那么你需要生成一个单独的线程(比如,使用AsyncTask,IntentService或者某个(那种)并处理那里的漫长过程。