optionsix
optionsix

Reputation: 956

Android - How to pass edittext value to background process, then start new activity once finished

I have an alert dialog being popped, which allows a person to enter a password, then choose to either submit it or cancel out (positive and negative buttons, as usual). Currently when they click Login (the pos choice), it just sends them to the next page no matter what they enter. (Code pasted below).

What I need help in changing, is I want the password captured from the edittext to actually be fed into an activity that decrypts an xml file which is sitting in the assets folder. The password captured is the key to the encryption. Can someone offer me some assistance as to how to modify my code to:

a) capture the password and send it to the decryption mechanism (I guess would this be done with onActivityResult?) b) do the decryption in the background (I guess with a progress bar or loader? I am REALLY novice with threading in Android) c) once the xml is decrypted, THEN pass off to the new activity where my code can read the decrypted xml now in memory.

Any help is greatly appreciated!!

.setPositiveButton("Login", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) {

                        Intent openBrowsePage = new Intent();
                        openBrowsePage .setClassName("com.myproject.android", "com.myproject.android.BrowsePage");
                        startActivity(openBrowsePage );

                    }
                })

Upvotes: 1

Views: 744

Answers (2)

rony l
rony l

Reputation: 6012

This is a classic job for an AsyncTask. from the documentation:

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

You won't need another activity or service for this.

The Asynctask documentation has some sample code but in short, you would inherit from AsyncTask and override the doInBackground method to do the background stuff (guaranteed to run in a background thread). you might also want to call publishProgress from within this method to indicate you have progressed with your background operation. Then you'll return the computation result from this method (an XML buffer in your case).

You might want to override the OnProgressUpdate and update a ProgressBar accordingly. This method will run in the UI thread in response to your publishProgress background method calls.

Also you'd want to override the onPostExecuteMethod that is guaranteed to run after the background operation was finished on the main thread. You get the result of doInBackground as a parameter to this method.

Note that this API is available for level 3 (android 1.5) and above.

Upvotes: 1

kwogger
kwogger

Reputation: 1422

You should be using a Service instead of an activity to do background tasks. You can pass the password you obtain from the EditText to the Service as an extra in the Intent you use to start the Service.

The easiest way for you to do this would be to extend IntentService and have your decryption there. Call the service in your button listener using startService. At the end of you encryption, call the new activity using startActivity.

EDIT: I should clarify my answer. Rory is correct - AsyncTask should be used for background tasks within an Activity. The method to differentiate on whether a background task should use AsyncTask or Service should depend on whether or not the task should be executing if you exit the activity. For example, if I start the download of a file in the background of my Activity, and I want the download to continue even when I leave the Activity, you should use a Service. On the other hand, if it's a background task to publish the current wifi signal strength to be displayed in your Activity, use AsyncTask.

Upvotes: 0

Related Questions