从IntentService调用AsyncTask的问题 [英] Problems in Calling AsyncTask from IntentService
问题描述
IntentService
类,并执行 的AsyncTask
但得到异常时,上preExecute()
被称为在这个code线 pDialog.show();
AsyncHandlerService类---
公共类AsyncHandlerService扩展IntentService {
ProgressDialog pDialog;
HttpPost岗位;
HTT presponse响应;
上下文CTX;公共AsyncHandlerService(){
超级(AsyncHandlerService);
CTX =这一点;
}@覆盖
保护无效onHandleIntent(意向意图){
新LoadDeviceInfo()执行();
}
类LoadDeviceInfo扩展的AsyncTask<字符串,字符串,字符串> {@覆盖
在preExecute保护无效(){
super.on preExecute();
pDialog =新ProgressDialog(CTX);
pDialog.setMessage(更新设备信息...);
pDialog.setIndeterminate(假);
pDialog.setCancelable(假);
pDialog.show(); //异常这里..
}保护字符串doInBackground(字符串参数... args){
}保护无效onPostExecute(字符串FILE_URL){
pDialog.dismiss();
}
更新:
我打电话了 IntentService
在具有 android.intent.action.PACKAGE_REPLACED
在Android清单定义。在code ---
公共类OnUpgradeBroadcastReceiver扩展广播接收器{
关联活动;
@覆盖
公共无效的onReceive(最终上下文的背景下,最终的意图意图){
活性=背景;
意图msgIntent =新意图(活动,AsyncHandlerService.class);
activity.startService(msgIntent);
}
}
错误日志:
com.testapp.main致命错误:无法添加窗口 -
令牌null不是一个应用
android.view.WindowManager $ BadTokenException:无法添加窗口 -
令牌null不是一个应用
在android.view.ViewRootImpl.setView(ViewRootImpl.java:588)
在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
在android.view.WindowManagerImpl $ CompatModeWrapper。
addView(WindowManagerImpl.java:149)
在android.app.Dialog.show(Dialog.java:277)
在com.testapp.main.AsyncHandlerService $ LoadDeviceInfo。
在preExecute(AsyncHandlerService.java:62)
在android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
在android.os.AsyncTask.execute(AsyncTask.java:534)
首先, IntentService
已经使用了后台线程。你不需要的其他的后台线程。做到这一点需要在后台完成的 onHandleIntent工作()
。
二,一个服务
无法显示对话框
。相反,让您的应用程序的UI层知道,工作是通过消息进行事件总线上(例如, LocalBroadcastManager
,greenrobot的EventBus,广场的奥托)。如果UI层不处理该事件,您的服务可以提高一个通知
或以其他方式让用户知道这是做的工作,如果是需要的。
I have created IntentService
class and performing asyncTask
but getting exception when onPreExecute()
is called at this code line pDialog.show();
AsyncHandlerService Class ---
public class AsyncHandlerService extends IntentService{
ProgressDialog pDialog;
HttpPost post;
HttpResponse response;
Context ctx;
public AsyncHandlerService() {
super("AsyncHandlerService");
ctx = this;
}
@Override
protected void onHandleIntent(Intent intent) {
new LoadDeviceInfo().execute();
}
class LoadDeviceInfo extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ctx);
pDialog.setMessage("Updating device info...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show(); //Exception here..
}
protected String doInBackground(String... args) {
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
}
UPDATE:
I am calling the IntentService
in the broadcast receiver that has the intent filter of android.intent.action.PACKAGE_REPLACED
defined in android manifest. The code ---
public class OnUpgradeBroadcastReceiver extends BroadcastReceiver {
Context activity;
@Override
public void onReceive(final Context context, final Intent intent) {
activity = context;
Intent msgIntent = new Intent(activity, AsyncHandlerService.class);
activity.startService(msgIntent);
}
}
Error Log:
com.testapp.main fatal error : Unable to add window --
token null is not for an application
android.view.WindowManager$BadTokenException: Unable to add window --
token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:588)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
at android.view.WindowManagerImpl$CompatModeWrapper.
addView(WindowManagerImpl.java:149)
at android.app.Dialog.show(Dialog.java:277)
at com.testapp.main.AsyncHandlerService$LoadDeviceInfo.
onPreExecute(AsyncHandlerService.java:62)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
at android.os.AsyncTask.execute(AsyncTask.java:534)
First, IntentService
already uses a background thread. You do not need another background thread. Do the work that needs to be done in the background in onHandleIntent()
.
Second, a Service
cannot display a Dialog
. Instead, let the UI layer of your app know that the work was done via a message on an event bus (e.g., LocalBroadcastManager
, greenrobot's EventBus, Square's Otto). If the UI layer does not handle the event, your service can raise a Notification
or otherwise let the user know about the work that was done, if that is needed.
这篇关于从IntentService调用AsyncTask的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!