Android Wear ProgressDialog和MessageAPI问题

时间:2015-08-11 17:29:02

标签: android wear-os

我目前有一个Android Wear应用程序,向用户显示10个项目的列表。当应用程序打开时,会出现一个带有单个图像的闪屏,同时会向手机发送一条消息以检索必要的列表数据。

我想在我的图片下面添加一个ProgressDialog。我的第一个问题是,这可能吗?如果是这样,怎么样?

我将发布下面的代码,但我遵循了在此处制作ProgressDialog的建议:Android loading animation。我可以成功创建ProgressDialog,但我遇到了一些问题。

  1. ProgressDialog视图取代了我最初的初始屏幕图像。
  2. doInBackground()调用函数向配对的手机发送消息,以获取列表数据。由于必须等待onMessageReceived()中的回复,因此会立即拨打onPostExecute()并关闭对话。
  3. 如果我尝试手动调用onPostExecute(),在获得列表数据后,ProgressDialog永远不会关闭。
  4. 任何帮助将不胜感激。谢谢。

    public class InitialActivity extends Activity implements
            GoogleApiClient.ConnectionCallbacks,
            GoogleApiClient.OnConnectionFailedListener,
            MessageApi.MessageListener{
        private WearableListItem[] items = new WearableListItem[10];
        private String nodeID;
        private GoogleApiClient mGoogleApiClient;
        private ProgressDialog mDialog;
        private ProgressTask progressTask;
        private boolean done = false;
    
        private class ProgressTask extends AsyncTask<Void, Void, Void>{
            ProgressDialog asyncDialog = new ProgressDialog(InitialActivity.this);
    
            @Override
            protected void onPreExecute(){
                //set message of the dialog
                asyncDialog.setMessage("Loading...");
                asyncDialog.setCancelable(false);
                //show dialog
                asyncDialog.show();
                super.onPreExecute();
            }
    
            @Override
            protected Void doInBackground(Void... arg0) {
                //don't touch dialog here it'll break the application
                //do some lengthy stuff like calling login webservice
                retrieveDeviceNode();
                return null;
            }
    
            @Override
            protected void onPostExecute(Void result){
                //hide the dialog
    //            if(done)
    //            {
                    asyncDialog.dismiss();
    //            }
                super.onPostExecute(result);
            }
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.load_screen);
    
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(Wearable.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            mGoogleApiClient.connect();
        }
    
        @Override
        public void onConnected(Bundle bundle) {
            Wearable.MessageApi.addListener(mGoogleApiClient, this);
    //        retrieveDeviceNode();
            progressTask = new ProgressTask();
            progressTask.execute();
        }
    
        @Override
        protected void onPause(){
            super.onPause();
            Wearable.MessageApi.removeListener(mGoogleApiClient, this);
            mGoogleApiClient.disconnect();
        }
    
        private void sendMessage(String message) {
            Wearable.MessageApi.sendMessage(mGoogleApiClient, nodeID, message, null);
        }
    
        @Override
        public void onMessageReceived(MessageEvent messageEvent) {
            nodeID = messageEvent.getSourceNodeId();
            if (messageEvent.getPath().contains("/joblist")) {
                List<Drawable> drawables = getDrawableList(messageEvent.getData());
                String jobList = messageEvent.getPath().substring(8);
                initJobList(jobList, drawables);
                done = true;
                progressTask.onPostExecute(null);
                Intent intent = new Intent(this,MainActivity.class);
                startActivity(intent);
            }
        }
    
        private void retrieveDeviceNode(){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    NodeApi.GetConnectedNodesResult result = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
                    if(result.getNodes().size() > 0){
                        nodeID = result.getNodes().get(0).getId();
                        sendMessage("/getWearData");
                    }
                    else{
                        nodeID = "";
                    }
                }
            }).start();
        }
    }
    

1 个答案:

答案 0 :(得分:0)

为什么不简单地将ProgressBar组件添加到显示图像的视图中,并在启动过程时显示该组件并在完成后隐藏它?