Reputation: 128
I have a splash screen which check an URL if there's any new content on the server. If show i wish to show a AlertDialog, to the app user so that depending on the action of user,i.e if YES download new contents from the server and get it to database else if NO load the contents for the app from the server.
However i am not being able to use an alert dialog inside AsyncTask My snippet code is as:
protected String doInBackground(String... mode){
/* I AM QUERY MY DATABASE FOR THE PREVIOUS CONTENT(SAY CONTENT 1, CONTENT 2);
* then i start my connection to the server which has an xml file with the content
* info (say content 3), at this stage .
* i Check certain condition here,
* say if result ==0 then i wish to display an alertdialog.
* I used an alert dialog and i get some error. Stating use Looper or Handler.
* Can anyone help me with this?
*/
}
Edited
So in
doInBackGround(String... mode){
if(result==0){
// how do i implement this alert show that if the dialog appears and on clicking Yes i wish to exectute the URL handling part below
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Updates Found");
alert.setMessage( "New Updates for has been found.\n Would you like to download ?\n"
+ "Whats the update: Checking "+pld.result.get(i).get( "issue" ));
alert.setIcon(android.R.drawable.ic_dialog_info);
alert.setPositiveButton(android.R.string.yes,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
try {
URL url = new URL(pld.result.get(i).get("link"));
ManageZipFile.getArchive(url,pld.result.get(i).get("issue"), file);
}
catch (Exception ex) {
Log.d(TAG, "Exception from URL"+ ex.getMessage());
}
progressState += updateProgressBar(20);
pld.saveCoverData(pld.result.get(i).get("issue"));
try {
pldContent = new PullLoadData(getString(R.string.files_path)
+ pld.result.get(i).get("issue")
+ "/contents.xml",context);
pldContent.getNewsItems();
progressState += updateProgressBar(20);
}
catch(XmlPullParserException e) {
Log.e(TAG, "GetNEWSITEM "+ e.getMessage());
}
catch (IOException e) {
Log.e(TAG, "XML FIle not found" + e.getMessage());
}
}
});
alert.setNegativeButton(android.R.string.no,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
dialog.dismiss();
}
});
AlertDialog showAlert = alert.create();
showAlert.show();
}
}
Upvotes: 0
Views: 2399
Reputation: 1584
UI thread means it is associated directly with your UI.you cant do operations like network datafetch,disk access etc which require large processing time in your UI thread. They should be done in seperate thread.
AsyncTask
helps to carry out theese operations in seperate thread which may otherwise results in infamous ANR error.(application not responding).
AsyncTask contain methods like onPreExecute() onPostExecute() onProgressUpdate() ,doInBackground ()
etc you can access UI thread from onPreExecute() onPostExecute() onProgressUpdate()
.The operation requiring longer processing time should be carried out in doinbackground().
I can't understand the functionality demands from your question but if you want to display Alert Dialog
before data fetching operation,Display it from onPreExecute()
or if you want to display after data fetch Do it from onPostExecute().
Upvotes: 3
Reputation: 16570
This is because the method doInBackground
runs on a non-UI thread, and an AlertDialog
needs to be displayed on the UI-thread.
To solve your problem, move the code for your AlertDialog
to the method onProgressUpdate
in AsyncTask
, then when you want to display the Dialog
call publishProgress()
from doInBackground
protected String doInBackground( String... mode ) {
if( something )
//Conditions for showing a Dialog has been met
}
protected void onProgressUpdate( Void... params ) {
//Show your dialog.
}
If you need to pass some kind of variable/data to the dialog, you can pass the kind of data you declared in extends AsyncTask<Params, Progress, Result>
- where Progress
is the type of parameter you can pass via the publishProgress( myVariable )
-method.
Upvotes: 5