开发者

Android : Inserting Handler and thread in service operations

I have a service which runs in background in my application. NOw what i want is when my service invokes a progress dialog should appear when background operations are performed.My Code:

 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);
              }   } }

I'm getting the following error:

06-18 01:12:16.793: ERROR/AndroidRuntime(737): Uncaught handler: thread main exiting due to uncaught exception 06-18 01:12:16.823: ERROR/AndroidRuntime(737): java.lang.RuntimeException: Unable to create service br.com.ops.service.WarnningService: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 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(Native Method) 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(Native Method) 06-18 01:12:16.823: ERROR/AndroidRuntime(737): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 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.开发者_C百科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 more

Please help me .. I'm not sure about the role of handler in this operation.


To show a progress box while you perform a long-running task, use an AsyncTask. Google has a great into to these here.


Services do not get to use the UI. Create an activity for the UI, or establish a Notification instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜