Question

J'ai une méthode public void writeEntry(Activity ctx, Entry entry) qui récupère des données et doit appeler une méthode native, ce qui prend plus de temps à terminer.
J'ai donc créé une AsyncTask qui gère le ProgressDialog et la méthode native.Cela fonctionne très bien dans une propre activité pour la tester, dans cette activité j'ai utilisé une interface de rappel et ainsi de suite.

Dans mon cas, j'ai la méthode décrite ci-dessus et je dois exécuter la AsyncTask .L'exécution ne peut pas être dans cette méthode car elle n'interrompt pas la suite de l'exécution.
J'ai besoin du résultat de la méthode native avant de pouvoir continuer l'exécution.
Est-il possible d'attendre la AsyncTask jusqu'à ce qu'elle soit terminée?La méthode wait() n'est pas une option car le thread d'interface utilisateur attendra également et ainsi le sens d'un ProgressDialog sera perdu.

Puis-je utiliser la méthode runOnUiThread() à partir des paramètres donnés ou est-ce la seule solution pour démarrer une propre Activité ?

Était-ce utile?

La solution 2

Ma première solution a été d'utiliser des méthodes de rappel avec une implémentation d'interface voir l'exemple https://stackoverflow.com/a/6396376/ 390177 .

Après avoir bavardé pendant un moment dans le chat Android, j'ai entendu dire qu'il existe une solution plus pratique.
Vous pouvez utiliser un IntentService en combinaison avec un PendingIntent .
La communication est réalisée avec Intent .
Si vous souhaitez utiliser un ProgressDialog , vous avez besoin d'une activité propre, qui enregistre par exemple un BroadcastReciever et le IntentService lui envoie le statut réel par diffusion.

Mais commençons maintenant.
Nous créons d'abord l ' Activity , qui contient le ProgressDialog et un BroadcastReceiver enregistré. Le BroadcastReceiver écoute les messages concernant la mise à jour et la fin de la boîte de dialogue.

Pour l ' Activité , nous avons besoin de la mise en page ...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="#80000000">
</LinearLayout>

... et le code associé:

public class ProgressActivity extends Activity {
    /**
     * ProgressDialog which is shown
     */
    private ProgressDialog progessDialog_g;

    /**
     * Instance of the BroadcastReceiver
     */
    private BroadcastReceiver receiver_g;

    /**
     * Identifier for the different settings of the ProgressDialog
     */
    public static final String PROGRESS_DIALOG_BOOL_HORIZONTAL_BAR = "pbar_horizontal_bar";
    public static final String PROGRESS_DIALOG_BOOL_CANCELABLE = "pbar_horizontal_cancelable";
    public static final String PROGRESS_DIALOG_STR_MESSAGE = "pbar_message";
    public static final String PROGRESS_DIALOG_INT_MAX = "pbar_max_bar";
    public static final String PROGRESS_DIALOG_INT_VALUE = "pbar_value";
    protected static final int PROGRESS_DIALOG_INT_MAX_VALUE = 100;

    @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
        Intent i = getIntent();
        progessDialog_g.setCancelable(i.getBooleanExtra(
                PROGRESS_DIALOG_BOOL_CANCELABLE, false));
        if (i.getBooleanExtra(
                PROGRESS_DIALOG_BOOL_HORIZONTAL_BAR, false)) {
            progessDialog_g.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        } else {
            progessDialog_g.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        }
        progessDialog_g
                .setMessage(i
                        .getStringExtra(PROGRESS_DIALOG_STR_MESSAGE));
        progessDialog_g.setMax(i.getIntExtra(
                PROGRESS_DIALOG_INT_MAX, 100));

        // Create the IntentFilter for the different broadcast messages
        IntentFilter iFilter =
                new IntentFilter(
                        ExampleProgressService.PROGRESS_DIALOG_BROADCAST_INIT);
        iFilter.addAction(ExampleProgressService.PROGRESS_DIALOG_BROADCAST_UPDATE);
        iFilter.addAction(ExampleProgressService.PROGRESS_DIALOG_BROADCAST_FINISH);

        // Creates the BroadcastReceiver
        receiver_g = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent){
                Log.d(DefaultPreferences.DEBUG_PREFIX + "ProgressActivity",
                        intent.getAction());

                if (ExampleProgressService.PROGRESS_DIALOG_BROADCAST_INIT
                        .equals(intent.getAction())) {
                    // Sets the ProgressDialog style
                    if (intent
                            .getBooleanExtra(
                                    PROGRESS_DIALOG_BOOL_HORIZONTAL_BAR,
                                    false)) {
                        progessDialog_g
                                .setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    } else {
                        progessDialog_g
                                .setProgressStyle(ProgressDialog.STYLE_SPINNER);
                    }

                    // Shows the ProgressDialog    
                    progessDialog_g.show();
                } else if (ExampleProgressService.PROGRESS_DIALOG_BROADCAST_UPDATE
                        .equals(intent.getAction())) {
                    // Updates the ProgressDialog
                    int value =
                            intent.getIntExtra(
                                    PROGRESS_DIALOG_INT_VALUE,
                                    -1);
                    if (value != -1) {
                        progessDialog_g.setProgress(value);
                    }
                } else if (ExampleProgressService.PROGRESS_DIALOG_BROADCAST_FINISH
                        .equals(intent.getAction())) {
                    // Finishs the ProgressDialog
                    progessDialog_g.cancel();
                    finish();
                }
            }
        };

        // Registers the BroadcastReceiver
        registerReceiver(receiver_g, iFilter);
    }

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

Nous voulons maintenant utiliser l ' activité , commençons par l'appeler:

final Intent i = new Intent(parentActivity, <packages>.ProgressActivity);
i.putExtra(ProgressActivity.PROGRESS_DIALOG_BOOL_CANCELABLE, cancelable_g);
i.putExtra(ProgressActivity.PROGRESS_DIALOG_BOOL_HORIZONTAL_BAR, showProgress_g);
i.putExtra(ProgressActivity.PROGRESS_DIALOG_STR_MESSAGE, message_g);
i.putExtra(ProgressActivity.PROGRESS_DIALOG_INT_MAX, ProgressActivity.PROGRESS_DIALOG_INT_MAX_VALUE);

parentActivity.startActivity(i);

Nous avons donc une ProgressActivity en cours d'exécution, qui attend différentes diffusions. Mais nous avons d'abord besoin du IntentService , qui envoie les diffusions.
Alors allons-y:

public class ExampleProgressService extends IntentService {
    /**
     * PendingIntent for callback.
     */
    protected PendingIntent pi_g = null;

    private static final String DEBUG_TAG = "ExampleProgressService";

    /**
     * Message identifier for ProgressDialog init
     */
    public static final String PROGRESS_DIALOG_BROADCAST_INIT = "Dialog.Progress.Init";
    /**
     * Message identifier for ProgressDialog finish
     */
    public static final String PROGRESS_DIALOG_BROADCAST_FINISH = "Dialog.Progress.Finish";
    /**
     * Message identifier for ProgressDialog update
     */
    public static final String PROGRESS_DIALOG_BROADCAST_UPDATE = "Dialog.Progress.Update";

    /**
     * Identifier of the result for intent content
     */
    public static final String PROGRESS_DATA_RESULT = "Result";
    /**
     * Identifier of the result error for intent content
     */
    public static final String PROGRESS_DATA_RESULT_ERROR_MESSAGE = "Result.Error.Message";
    /**
     * Identifier of the result error exception for intent content
     */
    public static final String PROGRESS_DATA_RESULT_ERROR_EXCEPTION = "Result.Error.Exception";
    /**
     * Identifier of the result status for intent content
     */
    public static final String PROGRESS_DATA_RESULT_STATUS_BOOL = "Result.Status.boolean";

    /**
     * Identifier of the pending intent for intent content
     */
    public static final String PROGRESS_DATA_PENDING_RESULT = "PendingResult";

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

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

        sendBroadcast(intent);
    }

    /**
     * Do some magic with the intent content
     */
    private void extractVariablesFromIntentAndPrepare(Intent intent)
            throws Exception {
        pi_g = (PendingIntent) intent
                .getParcelableExtra(PROGRESS_DATA_PENDING_RESULT);

        if (pi_g == null) {
            throw new Exception("There is no pending intent!");
    }

    /**
     * Sends an error message.
     */
    private void failed(Exception e, String message) {
        Intent i = new Intent();
        i.putExtra(PROGRESS_DATA_RESULT_ERROR_EXCEPTION, e);
        i.putExtra(PROGRESS_DATA_RESULT_ERROR_MESSAGE, message);

        send(i, false);
    }

    /**
     * Sends the init message.
     */
    private void initProgressActivity() {
        Intent intent = new Intent(PROGRESS_DIALOG_BROADCAST_INIT);

        intent.putExtra(PROGRESS_DIALOG_BOOL_HORIZONTAL_BAR,
                multipart_g);

        sendBroadcast(intent);
    }

    /**
     * (non-Javadoc)
     * 
     * @see android.app.IntentService#onHandleIntent(android.content.Intent)
     */
    @Override
    protected void onHandleIntent(Intent intent) {
        extractVariablesFromIntentAndPrepare(intent);

        initProgressActivity();

        // do your calculation here and implements following code
        Intent intent = new Intent(PROGRESS_DIALOG_BROADCAST_UPDATE);

        intent.putExtra(PROGRESS_DIALOG_INT_VALUE, progressValue);

        sendBroadcast(intent);

        // If you finished, use one of the two methods to send the result or an error
        success(result);
        failed(exception, optionalMessage);
    }

    /**
     * Sends the data to the calling Activity
     */
    private void send(Intent resultData, boolean status) {
        resultData.putExtra(PROGRESS_DATA_RESULT_STATUS_BOOL, status);

        closeProgressActivity();

        try {
            pi_g.send(this, Activity.RESULT_OK, resultData);
        } catch (PendingIntent.CanceledException e) {
            Log.e(DEBUG_TAG,
                    "There is something wrong with the pending intent", e);
        }
    }

    /**
     * Sends the result message.
     */
    private void success(String result) {
        Intent i = new Intent();
        i.putExtra(PROGRESS_DATA_RESULT, result);

        send(i, true);
    }
}

Le résultat de la progression du calcul doit être disponible dans parentActivity , nous créons donc PendingIntent dans cette Activity et appelons le IntentService .

// Some identifier for the call
int requestCode = 12345;

final Intent sI = new Intent(ExampleProgressService.PROGRESS_SERVICE_ACTION);

// Callback
sI.putExtra(ExampleProgressService.PROGRESS_DATA_PENDING_RESULT, parentActivity
        .createPendingResult(requestCode, null,
                PendingIntent.FLAG_CANCEL_CURRENT));

// Service start
parentActivity.startService(sI);

Pour recevoir les résultats, nous devons remplacer la méthode onActivityResult(int requestCode, int resultCode, Intent data).

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    // Compares the requestCode with the requestCode from above
    if (requestCode == ...) {
        if (data.getBooleanExtra(ExampleProgressService.PROGRESS_DATA_RESULT_STATUS_BOOL, false)) {
            // Calculation was success
            data.getStringExtra(ExampleProgressService.PROGRESS_DATA_RESULT);
        } else
        {
            // Calculation is failed
            data.getStringExtra(ExampleProgressService.PROGRESS_DATA_RESULT_ERROR_MESSAGE);
            ((Exception) data.getSerializableExtra(ExampleProgressService.PROGRESS_DATA_RESULT_ERROR_EXCEPTION));
        }
    }
}

C'était la magie, j'espère que cela vous aidera.

Autres conseils

donc je vais essayer d'expliquer autant que je peux

Démarrez votre processus lourd dans une AsyncTask, mais quel que soit le code que vous voulez exécuter après l'achèvement d'AsyncTask, placez-le dans une méthode publique distincte.Maintenant, une fois que vous avez terminé avec votre appel de processus lourd, cette méthode créée séparément dans onPostExecute().

Le code psuuedo ressemblera à ceci,

class main extends Activity {

    class Something extends AsyncTask<String, Integer, String> {

        protected void onPreExecute() {
            // Start your progress bar...
        }

        protected String doInBackground(String... params) {
            // Do your heavy stuff...
            return null;
        }

        protected void onPostExecute(String result) {
            // close your progress dialog and than call method which has
            // code you are wishing to execute after AsyncTask.
        }
    }

}

J'espère que cela vous aidera,

Bonne chance!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top