3

沮丧,因为我在等待网络调用时一直在尝试实现一个超级简单的加载轮。我已经搜索并阅读了数十个 SO 问题,我只是觉得我一定错过了一些东西,除非没有人真正做我想做的事情。我试过沿着这AsyncTask条路线走,但这不是我想要的。

我还要说,现在我的应用程序运行良好,只是从屏幕到屏幕的转换似乎挂起,因为它在网络上等待。我只想要一个加载轮,以便在 1-2 秒内用户知道应用程序正在运行并且没有冻结。

这是我当前的网络调用的样子:

 private static String sendDataToServer(String arg1, String arg2)
 {        
    Thread dbThread = new Thread()
    {
        public void run()
        {
             // do the call that takes a long time    
        }
    };
    dbThread.start();
    try {
        // I do this so that my program doesn't continue until
        // the network call is done and I have received the information
        // I need to render my next screen
        dbThread.join();
    }
    catch (InterruptedException e) {
        e.printStackTrace();
    }
 }

现在,为什么我不能ProgressDialog像这样添加?如果我这样做,progressDialog 永远不会出现。

 private static String sendDataToServer(String arg1, String arg2)
 {
    final ProgressDialog progress = new ProgressDialog(BaseActivity.getInstance());
    progress.setIndeterminate(true);
    progress.setMessage("Loading...");
    progress.show();

    Thread dbThread = new Thread()
    {
        public void run()
        {
             // do the call that takes a long time    
        }
    };
    dbThread.start();
    try {
        dbThread.join();
        progress.dismiss();
    }
    catch (InterruptedException e) {
        e.printStackTrace();
    }
 }

我想我被卡住了,因为网络调用需要在与 UI 线程不同的线程上,但我不想在我的应用程序中继续,因为我需要该调用的结果才能继续。但如果我这样做,thread.join()我会举起一切。我以为我需要,AsyncTask但很快就走下坡路了。如果你好奇的话,这是我的问题。 Android的AsyncTask:多参数,返回值,等待

当这个调用发生而不继续我的应用程序的其余部分时,我怎么只显示一个加载对话框?

编辑

这是我的AsyncTask尝试。

private class PostToFile extends AsyncTask<PostToFile, Void, Void>{
    private String functionName;
    private ArrayList<NameValuePair> postKeyValuePairs;
    private String result = "";

    public PostToFile(String function, ArrayList<NameValuePair> keyValuePairs){
        functionName= function;
        postKeyValuePairs = keyValuePairs;
    }

    @Override
    protected void onPreExecute() {
        progressDialog = ProgressDialog.show(BaseActivity.getInstance(), "Loading", "Please wait...", true, false);
    }

    @Override
    protected Void doInBackground(PostToFile... params) {                       
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair(FUNCTION_KEYWORD, functionName));
        for (int i = 0; i < postKeyValuePairs.size(); i++)            {
            nameValuePairs.add(postKeyValuePairs.get(i));
        }

        try{
            // ***do the POST magic.***
            result = response.toString();
        }
        catch (Exception e){
             // clean up my mess
        }

        return null;
    }

    private String getResult(){
        return result; // can I use this somehow???
    }

    @Override
    protected void onPostExecute(Void result) {
        progressDialog.dismiss();
    }
 }

当我使用它时:

        new PostToPHP(FUNCTION_NAME, postPairings){
            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                try
                {
                    if (result != null && !result.startsWith("null"))
                    {
                        JSONArray jArray = new JSONArray(result);
                        parseData(jArray);
                    }
                }
                catch (JSONException e)
                {
                    Log.e(Constants.LOG_TAG, e.toString());
                }
            };
        }.execute()

问题是,我有几个这样的电话是背靠背的,它们都是相互依赖的。所以第一个开始,第二个在第一个开始之后立即开始,但第一个完成之前开始。所以我得到错误的行为。只有在第一个电话完全完成后,我才能开始第二个电话?

4

2 回答 2

2

也许这会起作用,我还没有测试过,但你可以试试:

public class MyTask extends AsyncTask<String, Void, String> {
    private int flag;


    public MyTask(int flag) {
        this.flag = flag;
    }


    @Override
    protected String doInBackground(String... params) {
        switch (flag) {
            case 1:
                return doNetworking1();
            break;
            case 2:
                return doNetworking2();
            break;
            case 3:
                return doNetworking3();
            break;
            default:
                return doNetworking1();

        }
    }

    @Override
    protected void onPreExecute() {
        //show progress dialog
    }

    @Override
    protected void onPostExecute(String s) {
        //hide progress dialog
        switch (flag) {
            case 1: //do something with result
                new MyTask(2).execute();
                break;
            case 2: //do other stuff
                new MyTask(3).execute();
                break;
            case 3: //do event more stuff
                break;
            default:
                //do something
        }
    }
}

和用法:

new MyTask(1).execute();
于 2013-03-02T21:28:58.453 回答
1

在网络连接的情况下,我会使用IntentService而不是AsyncTask.

例如为网络连接创建 IntentServices:

 public class NetworkCallIntentService extends IntentService {
    public static final String BROADCAST_ACTION = "com.yourpackage:NETWORK_CALL_BROADCAST";
    public static final String RESULT = "com.yourpackage:NETWORK_CALL_RESULT";

    public NetworkCallIntentService() {
        super(NetworkCallIntentService.class.getSimpleName());
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // get data from intent if needed
        // do the call that takes long time  
        // send broadcast when done
        Intent intent = new Intent(BROADCAST_ACTION);
        intent.putExtra(RESULT, "some_result");//and more results
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }
}

接下来,从活动启动该服务,显示进度对话框并将负责显示下一个屏幕的代码移动到BroadcastReceiver#onReceive()方法:

 public class SomeActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //start service
        Intent serviceIntent = new Intent(this, NetworkCallIntentService.class);
        //put extras into intent if needed
        //serviceIntent.putExtra("some_key", "some_string_value");
        startService(serviceIntent);
        //here just show progress bar/progress dialog
    }

    @Override
    protected void onResume() {
        super.onResume();
        LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(mNetworkCallReceiver,
                new IntentFilter(NetworkCallIntentService.BROADCAST_ACTION));
    }

    @Override
    protected void onPause() {
        super.onPause();
        LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(mNetworkCallReceiver);
    }

    private BroadcastReceiver mNetworkCallReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            //hide progress bar/progress dialog
            //here get results from intent extras
            String result = intent.getStringExtra(NetworkCallIntentService.RESULT);
            //process results and continue program(go to next screen, show error message etc.)
        }

    }

}

在清单文件中声明服务:

<service
    android:name="com.yourpackage.DownloadSvtValuesIntentService"
    android:exported="false" >
</service>
于 2013-03-02T22:20:52.607 回答