2013-03-22 96 views
0

奇效在内部的AsyncTask显示AlertDialog:AsyncTask.onPostExecute()显示AlertDialog当应用程序被最小化的原因unresponing UI

private class CheckDeviceConfiguration extends AsyncTask<Void, Void, Boolean> { 
private ProgressDialog progressDialog; 

@Override 
protected void onPreExecute() { 
    super.onPreExecute();  
    progressDialog = ProgressDialog.show(ActivitySIPCountrySelection.this, "Title", "working...", true);       
} 

@Override 
protected void onPostExecute(Boolean result) { 
    super.onPostExecute(result); 

    progressDialog.dismiss(); //hide progress dialog previously shown 

    if (!result) { 
     AlertDialog.Builder dialog = new AlertDialog.Builder(ActivitySIPCountrySelection.this); 
     dialog.setCancelable(false); 
     dialog.setMessage("Message"); 
     dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int arg1) { 
      //do something 
      } 
     }); 

     dialog.show(); 
    } 

    } 
@Override 
protected Boolean doInBackground(Void... params) 
    Thread.sleep(5000); 
    return false; 
} 

}

:如果应用程序 的的AsyncTask执行期间被最小化

如果我点击我的应用程序图标来恢复,用户界面没有响应,活动看起来有点变暗(无效?)。后退按钮不起作用。

编辑:

有人问起我打电话的AsyncTask。那么,从Activity onCreate()。

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_sip_country_selection); 

     new CheckDeviceConfiguration().execute(); 
    } 

异步任务正确显示进度对话框并将其隐藏在onPostExecute中。

+0

fom你在哪里调用asynctask?((Activity)mContext).finish(); mContext是你的活动上下文。将上下文传递给asynctask构造函数,然后使用该上下文调用finish() – Raghunandan 2013-03-22 17:47:20

+0

编辑,谢谢。添加了onPreExcute()部分。 – 2013-03-22 17:50:24

+0

@Raghunandan完成()没有被调用,忘记它。 – 2013-03-22 17:56:16

回答

1

注意:AsyncTask管理用ThreadPoolExecutor创建的线程池。它将有5到128个线程。如果超过5个线程,这些额外的线程将在被移除之前最多停留10秒。 (注意:这些数字是针对当前可见的开放源代码的,因Android版本而异)。

请单独离开AsyncTask线程。

按下主页将您从应用程序切换到主屏幕,同时让您的应用程序在后台运行。

当手机的内存资源不足时,它将开始关闭在后台运行的应用程序,以便您的手机拥有足够的资源用于您现在正在尝试执行的操作。游戏常常是手机为了节省资源而“杀死”的第一批应用程序,因为它们通常比其他应用程序使用更多的内存和CPU。这就是为什么有时你的游戏仍然处于暂停状态,有时Android会为你关闭它。

http://developer.android.com/guide/components/tasks-and-back-stack.html。有关更多详细信息,请查看任务和后退任务

你可以通过调用cancel(true)来取消asynctask,一个中断将被发送到后台线程,这可能有助于可中断的任务。否则,您应该确保在doInBackground()方法中定期检查isCancelled()。

@Override 
protected void onPause() { 
// TODO Auto-generated method stub 
super.onPause(); 
d.cancel(true); 
if(d.isCancelled()) 
{ 
    System.out.println("Destroyed...."); 
} 
} 
@Override 
protected void onDestroy() { 
// TODO Auto-generated method stub 
super.onDestroy(); 
d.cancel(true); 
if(d.isCancelled()) 
{ 
    System.out.println("Destroyed...."); 
} 
} 

所以当您的活动恢复时,创建一个新的asynctask实例并重新执行。

http://code.google.com/p/shelves/。检查Romain Guy的货架项目。

http://developer.android.com/reference/android/content/AsyncTaskLoader.html。还检查asynctask加载器。

Asynctask的替代品是RoboSpice。 https://github.com/octo-online/robospice

FAQ's https://github.com/octo-online/robospice/wiki/Advanced-RoboSpice-Usages-and-FAQ

+0

因此,如果活动已暂停,那么您正在取消任务?但是...我能做什么如果我不能取消任务? – 2013-03-24 12:50:01

+0

你可以通过调用canel来取消asyntask(true) – Raghunandan 2013-03-24 15:24:18

+0

对不起,我解释得更好:在这一点上,我不能取消AsyncTask而没有管理它。也许我必须重新思考我的方法并管理“取消”情况。 – 2013-03-24 15:27:36

相关问题