我遇到的问题是,当我关闭应用程序时,我想停止服务并运行方法
new sendLogoutDetail()。execute(AccountID);
更新我的数据库。我把方法放在onDestroy()中但是当我结束我的应用程序时,Log Cat充满了很多错误,我的方法根本没有运行。
这是我的Android代码:
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
String AccountID = null;
Bundle extras = getIntent().getExtras();
if (extras != null){
AccountID = extras.getString("AccountID");
}
new sendLogoutDetail().execute(AccountID);
stopService(new Intent(this,getLocationService.class));
}
private class sendLogoutDetail extends AsyncTask<String, Void, String>
{
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog= ProgressDialog.show(LoggedIn.this, "Ending Service","Loading..", true);
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
HttpPost request = new HttpPost(SERVICE_URI + "/DeleteLoginPool");
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
JSONStringer getWifiInfo;
try {
getWifiInfo = new JSONStringer()
.object()
.key("staffLocate")
.object()
.key("AccountID").value(params[0])
.endObject()
.endObject();
StringEntity entity = new StringEntity(getWifiInfo.toString());
request.setEntity(entity);
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
progressDialog.dismiss();
}
}
我的日志猫:
>06-19 18:42:09.626: W/asset(22278): deep redirect failure from 0x01030117 => 0x0a0a0004, defStyleAttr=0x01010084, defStyleRes=0x0103008f, style=0x010302eb
>06-19 18:42:09.626: W/asset(22278): deep redirect failure from 0x01030117 => 0x0a0a0004, defStyleAttr=0x01010084, defStyleRes=0x0103008f, style=0x010302eb
>06-19 18:42:09.688: E/WindowManager(22278): Activity com.example.staffallocatorproject.LoggedIn has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{413f5790 V.E..... R.....I. 0,0-0,0} that was originally added here
>06-19 18:42:09.688: E/WindowManager(22278): android.view.WindowLeaked: Activity com.example.staffallocatorproject.LoggedIn has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{413f5790 V.E..... R.....I. 0,0-0,0} that was originally added here
>06-19 18:42:09.688: E/WindowManager(22278): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
>06-19 18:42:09.688: E/WindowManager(22278): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
>06-19 18:42:09.688: E/WindowManager(22278): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
>06-19 18:42:09.688: E/WindowManager(22278): at android.app.Dialog.show(Dialog.java:281)
>06-19 18:42:09.688: E/WindowManager(22278): at android.app.ProgressDialog.show(ProgressDialog.java:116)
>06-19 18:42:09.688: E/WindowManager(22278): at android.app.ProgressDialog.show(ProgressDialog.java:99)
>06-19 18:42:09.688: E/WindowManager(22278): at com.example.staffallocatorproject.LoggedIn$sendLogoutDetail.onPreExecute(LoggedIn.java:57)
>06-19 18:42:09.688: E/WindowManager(22278): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
>06-19 18:42:09.688: E/WindowManager(22278): at android.os.AsyncTask.execute(AsyncTask.java:534)
>06-19 18:42:09.688: E/WindowManager(22278): at com.example.staffallocatorproject.LoggedIn.onDestroy(LoggedIn.java:45)
>06-19 18:42:09.688: E/WindowManager(22278): at android.app.Activity.performDestroy(Activity.java:5391)
>06-19 18:42:09.688: E/WindowManager(22278): at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1110)
>06-19 18:42:09.688: E/WindowManager(22278): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3568)
>06-19 18:42:09.688: E/WindowManager(22278): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3599)
>06-19 18:42:09.688: E/WindowManager(22278): at android.app.ActivityThread.access$1200(ActivityThread.java:156)
>06-19 18:42:09.688: E/WindowManager(22278): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
>06-19 18:42:09.688: E/WindowManager(22278): at android.os.Handler.dispatchMessage(Handler.java:99)
>06-19 18:42:09.688: E/WindowManager(22278): at android.os.Looper.loop(Looper.java:137)
>06-19 18:42:09.688: E/WindowManager(22278): at android.app.ActivityThread.main(ActivityThread.java:5233)
>06-19 18:42:09.688: E/WindowManager(22278): at java.lang.reflect.Method.invokeNative(Native Method)
>06-19 18:42:09.688: E/WindowManager(22278): at java.lang.reflect.Method.invoke(Method.java:511)
>06-19 18:42:09.688: E/WindowManager(22278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
>06-19 18:42:09.688: E/WindowManager(22278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
>06-19 18:42:09.688: E/WindowManager(22278): at dalvik.system.NativeStart.main(Native Method)
>06-19 18:42:09.719: E/<<myServiceRunner-onDestroy>>(22278): I am dead-3
答案 0 :(得分:1)
问题是当ProgressDialog
被销毁时,您显示的是Activity
。因此,如果您想在Activity被销毁时进行一些后台操作,请不要显示ProgressDialog
。因此,在这种情况下,仅AsyncTask
的{{1}}应该适合您。
答案 1 :(得分:1)
当活动被销毁时,您无法显示任何对话框。
你可以做的是覆盖onBackPressed(以及任何你称之为finish()的地方),并在那里显示你需要的东西,当它完成你想做的任何事情时,完成活动。