Rob*_*bin 0 android exception looper android-asynctask
我有一个启动画面,如果服务器上有任何新内容,则会检查URL.如果show我希望向app用户显示一个AlertDialog,以便取决于用户的操作,即如果YES从服务器下载新内容并将其送到数据库,否则如果NO从服务器加载应用程序的内容.
但是,我无法在AsyncTask中使用警报对话框我的代码段如下:
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?
*/
}
Run Code Online (Sandbox Code Playgroud)
编辑
所以
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();
}
}
Run Code Online (Sandbox Code Playgroud)
这是因为该方法doInBackground在非UI线程上运行,并且AlertDialog需要在UI线程上显示.
为了解决你的问题,移动的代码为您AlertDialog的方法onProgressUpdate中AsyncTask,那么当你想要显示的Dialog呼叫publishProgress()从doInBackground
protected String doInBackground( String... mode ) {
if( something )
//Conditions for showing a Dialog has been met
}
protected void onProgressUpdate( Void... params ) {
//Show your dialog.
}
Run Code Online (Sandbox Code Playgroud)
如果您需要通过某种变量/数据的对话框,你可以通过你声明的类型的数据extends AsyncTask<Params, Progress, Result>-在那里Progress是可以通过传递参数的类型publishProgress( myVariable )-方法.
| 归档时间: |
|
| 查看次数: |
2357 次 |
| 最近记录: |