My *_*God 5 android android-service android-asynctask android-intentservice
我已经创建了IntentService类和执行asyncTask但onPreExecute()在此代码行调用时获得异常pDialog.show();
AsyncHandlerService类---
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();
}
Run Code Online (Sandbox Code Playgroud)
更新:
我IntentService在广播接收器中调用具有android.intent.action.PACKAGE_REPLACEDandroid清单中定义的intent过滤器.代码 - -
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);
}
}
Run Code Online (Sandbox Code Playgroud)
错误日志:
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)
Run Code Online (Sandbox Code Playgroud)
Com*_*are 13
首先,IntentService已经使用了后台线程.您不需要其他后台线程.做需要在后台完成的工作onHandleIntent().
第二,一个Service不能显示一个Dialog.相反,让你的应用程序的UI层知道工作是通过事件总线上的消息完成的(例如LocalBroadcastManager,greenrobot的EventBus,Square的Otto).如果UI层未处理该事件,则您的服务可以提出Notification或以其他方式让用户知道已完成的工作(如果需要).
| 归档时间: |
|
| 查看次数: |
7728 次 |
| 最近记录: |