Android:在服务操作中插入处理程序和线程

时间:2011-06-17 20:03:08

标签: java android

我有一个在我的应用程序中在后台运行的服务。我想要的是当我的服务调用进度对话框时应该在执行后台操作时出现。我的代码:

 public class WService extends
        Service implements Runnable {
          private  String TAG_SPV=
        "SPV>service>WService";    private
        ProgressDialog pd;    
          NotificationFactory nf;     Parse
        parse;    DAOSymbol dao;  WSClient
        responseClient;   List<Quote> quotes;
          Map<String, Symbol> symbols;
              private boolean on;

          public IBinder onBind(Intent
        intent) {
                      return null;    }

          @Override   public void onCreate() {
              // TODO Auto-generated method stub
              super.onCreate();
              Log.i(TAG_SPV,"responseTojsonArray");
              parse = new Parse();        dao = new
        DAOSymbol();      quotes = new
        ArrayList<Quote>();       symbols = new
        HashMap<String, Symbol>();


                        startService(new Intent(this,
                      br.com.ops.service.NotificationFactory.class));




                        pd = ProgressDialog.show(this,
        "Downloading..", "Please wait",
        true,
                               false);

                      Thread thread = new Thread(this);       thread.start();     }

          @Override   public void
        onStart(Intent intent, int startId)
        {         on = true;
              super.onStart(intent, startId);
              Log.i(TAG_SPV,"onStart");   }   
          private Handler handler = new
        Handler() {
                @Override
                public void handleMessage(Message msg) {
                        pd.dismiss();


                } };


          @SuppressWarnings("unchecked")
          public void run() {
              Log.i(TAG_SPV,"run");       if (on) {
                  dao.open(getBaseContext());
                   //Perform some DB operations as well as hit web service

                  dao.close();
                  handler.postDelayed(this, 30000);
              }   } }

我收到以下错误:

  

06-18 01:12:16.793:   错误/ AndroidRuntime(737):未捕获   handler:thread main由于退出   未被捕的例外06-18 01:12:16.823:   ERROR / AndroidRuntime(737):   java.lang.RuntimeException:无法   创造服务   br.com.ops.service.WarnningService:   android.view.WindowManager $ BadTokenException:   无法添加窗口 - 令牌null为   不适用于申请06-18   01:12:16.823:   ERROR / AndroidRuntime(737):at   android.app.ActivityThread.handleCreateService(ActivityThread.java:2790)   06-18 01:12:16.823:   ERROR / AndroidRuntime(737):at   android.app.ActivityThread.access $ 3200(ActivityThread.java:119)   06-18 01:12:16.823:   ERROR / AndroidRuntime(737):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1917)   06-18 01:12:16.823:   ERROR / AndroidRuntime(737):at   android.os.Handler.dispatchMessage(Handler.java:99)   06-18 01:12:16.823:   ERROR / AndroidRuntime(737):at   android.os.Looper.loop(Looper.java:123)   06-18 01:12:16.823:   ERROR / AndroidRuntime(737):at   android.app.ActivityThread.main(ActivityThread.java:4363)   06-18 01:12:16.823:   ERROR / AndroidRuntime(737):at   java.lang.reflect.Method.invokeNative(母语   方法)06-18 01:12:16.823:   ERROR / AndroidRuntime(737):at   java.lang.reflect.Method.invoke(Method.java:521)   06-18 01:12:16.823:   ERROR / AndroidRuntime(737):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:860)   06-18 01:12:16.823:   ERROR / AndroidRuntime(737):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)   06-18 01:12:16.823:   ERROR / AndroidRuntime(737):at   dalvik.system.NativeStart.main(母语   方法)06-18 01:12:16.823:   错误/ AndroidRuntime(737):引起:   android.view.WindowManager $ BadTokenException:   无法添加窗口 - 令牌null为   不适用于申请06-18   01:12:16.823:   ERROR / AndroidRuntime(737):at   android.view.ViewRoot.setView(ViewRoot.java:472)   06-18 01:12:16.823:   ERROR / AndroidRuntime(737):at   android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)   06-18 01:12:16.823:   ERROR / AndroidRuntime(737):at   android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)   06-18 01:12:16.823:   ERROR / AndroidRuntime(737):at   android.app.Dialog.show(Dialog.java:239)   06-18 01:12:16.823:   ERROR / AndroidRuntime(737):at   android.app.ProgressDialog.show(ProgressDialog.java:107)   06-18 01:12:16.823:   ERROR / AndroidRuntime(737):at   android.app.ProgressDialog.show(ProgressDialog.java:95)   06-18 01:12:16.823:   ERROR / AndroidRuntime(737):at   br.com.ops.service.WarnningService.onCreate(WarnningService.java:56)   06-18 01:12:16.823:   ERROR / AndroidRuntime(737):at   android.app.ActivityThread.handleCreateService(ActivityThread.java:2780)   06-18 01:12:16.823:   ERROR / AndroidRuntime(737):... 10   更

请帮助我..我不确定处理程序在此操作中的作用。

2 个答案:

答案 0 :(得分:0)

服务无法使用UI。为UI创建活动,或者改为建立通知。

答案 1 :(得分:0)

要在执行长时间运行的任务时显示进度框,请使用AsyncTask。 Google对这些here非常了解。