【发布时间】:2014-08-06 11:58:44
【问题描述】:
我目前正在使用一个启动服务的片段。该服务使用 asynctask 与服务器建立连接。我想在应用程序连接到服务器时显示进度对话框。问题是尝试传递对话框的应用程序上下文,我得到一个令牌错误。
我认为我需要我的活动上下文,但我不知道如何将它传递给服务,以便它可以使用它将它传递给异步任务。连接是在服务的onCreate()方法上实现的。
日志猫:
08-06 12:58:01.696: E/AndroidRuntime(16733): java.lang.RuntimeException: Unable to create service com.homedcs.pushservice.PushAlarm: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
08-06 12:58:01.696: E/AndroidRuntime(16733): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2564)
08-06 12:58:01.696: E/AndroidRuntime(16733): at android.app.ActivityThread.access$1600(ActivityThread.java:181)
08-06 12:58:01.696: E/AndroidRuntime(16733): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1414)
08-06 12:58:01.696: E/AndroidRuntime(16733): at android.os.Handler.dispatchMessage(Handler.java:99)
服务:
@Override
public void onCreate() {
if(connection == null){
try {
connection = new PushConnection(getApplicationContext()).execute().get();
} catch (InterruptedException | ExecutionException e2) {
e2.printStackTrace();
}
}
异步任务:
public PushConnection(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(context);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.show();
}
【问题讨论】:
-
我希望你不要在 DoINBacground 中使用 UI
-
贴一些代码和logcat
-
asynctask 有自己的进度对话框.. 显示你的 logcat
-
服务是一个上下文,好吧,我试过从服务中传递“this”,还是不行。
标签: android android-activity service android-asynctask android-context