如何从扩展Service的调用中调用asynctask

时间:2014-05-02 12:25:04

标签: java android android-asynctask

如何从runnable调用Asynctask。在我的班级我正在扩展服务,所以我不知道是什么问题。 Log cat输出类似于

    05-02 11:44:40.914: E/AndroidRuntime(1997): FATAL EXCEPTION: main
    05-02 11:44:40.914: E/AndroidRuntime(1997):       
    android.view.WindowManager$BadTokenException: Unable to add window -- token null is 
    not     for an application
    05-02 11:44:40.914: E/AndroidRuntime(1997):     at  
    android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
    05-02 11:44:40.914: E/AndroidRuntime(1997):     at  
    android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
    05-02 11:44:40.914: E/AndroidRuntime(1997):     at 
    android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
    05-02 11:44:40.914: E/AndroidRuntime(1997):     at 
    android.app.Dialog.show(Dialog.java:281)
    05-02 11:44:40.914: E/AndroidRuntime(1997):     at  
  com.example.sebi.BroadcastService$SaveProducts.onPreExecute(BroadcastService.java:55)
   05-02 11:44:40.914: E/AndroidRuntime(1997):  at  
   android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
   05-02 11:44:40.914: E/AndroidRuntime(1997):  at  
   android.os.AsyncTask.execute(AsyncTask.java:534)
   05-02 11:44:40.914: E/AndroidRuntime(1997):  at 
   com.example.sebi.BroadcastService$1.run(BroadcastService.java:39)
   05-02 11:44:40.914: E/AndroidRuntime(1997):  at 
   android.os.Handler.handleCallback(Handler.java:725)
   05-02 11:44:40.914: E/AndroidRuntime(1997):  at 
   android.os.Handler.dispatchMessage(Handler.java:92)
   05-02 11:44:40.914: E/AndroidRuntime(1997):  at  
   android.os.Looper.loop(Looper.java:137)
   05-02 11:44:40.914: E/AndroidRuntime(1997):  at 
   android.app.ActivityThread.main(ActivityThread.java:5039)
   05-02 11:44:40.914: E/AndroidRuntime(1997):  at                   
   java.lang.reflect.Method.invokeNative(Native Method)
   05-02 11:44:40.914: E/AndroidRuntime(1997):  at 
   java.lang.reflect.Method.invoke(Method.java:511)
   05-02 11:44:40.914: E/AndroidRuntime(1997):  at 
   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
   05-02 11:44:40.914: E/AndroidRuntime(1997):  at 
   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
   05-02 11:44:40.914: E/AndroidRuntime(1997):  at 
   dalvik.system.NativeStart.main(Native Method)

当我尝试使用 runOnUiThread 时,我在 runOnUiThread 时收到错误。

      The method runOnUiThread(new Runnable(){}) is undefined for the type new  
      Runnable(){}

代码:

       private Runnable sendUpdatesToUI = new Runnable() {
        public void run() {
            runOnUiThread(new Runnable(){

            public void run(){
            new SaveProducts().execute();  
            handler.postDelayed(this, 30000); // 10 seconds
        }
            });
        }
    };    

    class SaveProducts extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {

            super.onPreExecute();

                pDialog = new  
           ProgressDialog(BroadcastService.this);
                pDialog.setMessage("loading.");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.show();

        }

1 个答案:

答案 0 :(得分:0)

您只能显示带有活动上下文的对话框。使用服务上下文不起作用。

只需从异步任务中删除进度对话框。