0

AsyncTask を拡張する Download というクラスがあります。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 メソッドでは、多くのネットワーク操作を行い、目的の URL から画像をダウンロードできるたびに進行状況更新メソッドを呼び出します。

 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 メソッドでそれらの更新を検索し、見つかった場合は、進行状況を公開して「古いダイアログ」を閉じ、ProgressDialog.STYLE_HORIZONTAL で新しいダイアログを作成します。この最後のダイアログは、ネットから何かをダウンロードするたびに更新されます。

ここで問題です。Eclipse でアプリケーションを実行し、ダウンロード中にアプリケーションを一時停止すると、すべて正常に動作します。2 回目にアプリケーションを再入力すると、ダウンロードが完全に続行され、2 番目の進行状況バーが期待どおりに更新され続けていることがわかります。

ただし、署名済み apk を作成する場合 --> その apk を使用してアプリケーションをインストールする --> アプリを起動する --> ダウンロード中に一時停止する --> アプリを再入力すると、最初のダイアログが再び表示され、ダウンロードがうまく進みません。logcat から、Eclipse からアプリを実行すると、アプリを終了して再入力しても、onpreexecute メソッドが 1 回だけ呼び出されることがわかりました。ただし、apk を使用してアプリをインストールすると、アプリを終了してから再起動するたびに onpreexecute メソッドが呼び出されます。

なぜそれが起こっているのですか?問題がそのapkの作成にあったかどうかを確認するために、プロジェクトとその他の基本的な操作をクリーンアップしようとしましたが、結果はありませんでした.

4

1 に答える 1

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" />
于 2012-12-17T13:34:40.717 に答える