奇怪的行为asynctask

时间:2012-12-17 13:23:43

标签: android android-asynctask apk progressdialog

我有一个名为Download的类,它扩展了AsyncTask。 OnPreExecute方法执行以下操作:

    @Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    super.onPreExecute();   
    this.dialog.setTitle("Check updates...");

    this.dialog.show();

}

列出的对话框在类的构造函数中即时显示,并具有以下特征:

 dialog = new ProgressDialog(activity);
 dialog.setCancelable(false);

在doInBackground方法中,我会做很多网络操作,每当我能够从所需的网址下载图像时,我都会调用进度更新方法:

 protected void onProgressUpdate(String... values) 
 // TODO Auto-generated method stub
  super.onProgressUpdate(values);



    //call the onprogress update
    publishProgress("1000");

    //do a lot of stuff with the network


 }

在onprogressupdate中,我将关闭创建的第一个对话框,我将展示另一个:

        protected void onProgressUpdate(String... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);

        if(values[0].equals("1000")){

            dialog.dismiss();

                  progress_brand.show();

            progress_brand.setProgress(progress_brand.getProgress()+1);

            if(progress_brand.getProgress() == progress_brand.getMax()){

                progress_brand.dismiss();

            }

        } 
 }

所以基本上:在asynctask的开头我正在显示一个标题为“检查更新”的对话框...然后我将在doinbackground方法中搜索这些更新,如果我找到一些,我ll使用发布进度关闭“旧对话框”并使用ProgressDialog.STYLE_HORIZONTAL创建一个新对话框。每次我从网上下载内容时,都会更新最后一个对话框。

所以这就是问题所在。如果我将使用eclipse运行应用程序,然后在下载期间我将暂停应用程序一切正常。如果我第二次重新进入应用程序,我可以看到下载继续完美,我可以看到第二个进度条继续按预期自动更新。

然而,如果我制作一个签名的apk - >通过apk安装应用程序 - >启动应用 - >在下载过程中暂停 - >重新进入应用程序,然后再次显示第一个对话框,下载无法正常进行。 我从logcat中看到,如果我将从eclipse运行应用程序,onpreexecute方法只调用一次,即使我将退出并重新进入应用程序。 但是,如果我通过apk安装应用程序,每次我退出然后重新启动应用程序时都会调用onpreexecute方法。

为什么会这样?我试图清理项目和其他基本操作,看看问题是否是该apk的创建,但没有结果。

1 个答案:

答案 0 :(得分:0)

不,你不在AnyTask中使用ProgressDialog

试试这个(例如)

public class Updated extends Activity {
    /**
     * ProgressDialog which is shown
     */
    private ProgressDialog progessDialog_g;
    private boolean downloadUses = false;


    /**
     * Instance of the BroadcastReceiver
     */
    private BroadcastReceiver receiver_g;
    private IntentFilter iFilter;
    protected ServiceConnection mServerConn = new ServiceConnection() {
        public void onServiceConnected(ComponentName name, IBinder binder) {

        }

        public void onServiceDisconnected(ComponentName name) {

        }

    };
    private Intent sI;

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

        setContentView(R.layout.progress);

        progessDialog_g = new ProgressDialog(this);

        // Reads and sets the settings for the ProgressDialog

        // Create the IntentFilter for the different broadcast messages
        iFilter = new IntentFilter(
                ProgressService.PROGRESS_DIALOG_BROADCAST_FINISH);
        // Creates the BroadcastReceiver
        receiver_g = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                if (ProgressService.PROGRESS_DIALOG_BROADCAST_FINISH
                        .equals(intent.getAction())) {
                    // Finishs the ProgressDialog
                    progessDialog_g.cancel();
                    Finish();

                }
            }
        };




    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onResume() {

        sI = new Intent(this, ProgressService.class);
        this.bindService(sI, mServerConn, Context.BIND_AUTO_CREATE);
        this.startService(sI);
        // Registers the BroadcastReceiver
        registerReceiver(receiver_g, iFilter);
        if (downloadUses) {
            downloadUses = false;
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        } else {

            progessDialog_g.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progessDialog_g.setMessage("you messege");
            progessDialog_g.show();

            new DownloadJSONTask(this, sI)
                    .execute(Initialize.server_url+"/changes/update/1");

        }

        super.onResume();
    }

    @Override
    protected void onPause() {
        this.stopService(new Intent(this, ProgressService.class));
        this.unbindService(mServerConn);
        unregisterReceiver(receiver_g);
        super.onPause();
    }

    private void Finish() {

        Intent in = new Intent(this, RegionsActivity.class);
        startActivity(in);
        downloadUses = true;
    }

}

}

public class ProgressService extends IntentService {

    public static final String PROGRESS_DIALOG_BROADCAST_FINISH = "Dialog.Progress.MyKey.Finish";

    public ProgressService() {
        super("ExampleProgressService");
    }

    /**
     * Send the finish message.
     */
    private void closeProgressActivity() {
        Intent intent = new Intent(PROGRESS_DIALOG_BROADCAST_FINISH);

        sendBroadcast(intent);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // extractVariablesFromIntentAndPrepare(intent);

        String action = intent.getStringExtra("Action");

        if ("0".equals(action)) {

            closeProgressActivity();

        }

    }

}

并在你的AnyTask

sI.putExtra("Action", "0");
        context.startService(sI);

并在你的清单

  <service android:name=".Intent.ProgressService" />