2012-02-17 19 views
1
public class UpdateService extends Service { 
    private Timer timer = new Timer(); 

    private static final long UPDATE_INTERVAL = 60000; 
    private final IBinder mBinder = new MyBinder(); 
    public static boolean isServiceRunning; 
    public void onCreate() { 
     super.onCreate(); 
     pollForUpdates(); 
    } 

    private void pollForUpdates() { 

     timer.scheduleAtFixedRate(new TimerTask() { 
      @SuppressWarnings("rawtypes") 
      @Override 
      public void run() { 

       Log.v("service", "called...");  
       Thread.setDefaultUncaughtExceptionHandler(onForceCloseError); 
       try 
       { 
        isServiceRunning=true; 
        DatabaseHelper db = new DatabaseHelper(UpdateService.this); 

        BaseObject bObj = db.GetObjectFromDB("GlobalObject1"); 
        long id=Thread.currentThread().getId(); 
        String serviceThreadID= String.valueOf(id); 


        long restaurantID=((GlobalObject) UpdateService.this.getApplication()).getRestuarantID(); 
        RequestProperty rp1 = new RequestProperty("restaurantID", restaurantID); 
        ArrayList<RequestProperty> properties = new ArrayList<RequestProperty>(); 
        properties.clear(); 
        properties.add(rp1); 
        ArrayList customerArrList = SoapInvoker.GetListFromService("GetAllCustomersForRestaurant", properties, new Customer(),UpdateService.this); 

        ArrayList returnArrayList1 = SoapInvoker.GetListFromService("GetAllAirHostessDetails", null,new Airhostess(),UpdateService.this); 

        /* For Chef Console and Order Detail Update */ 
        long branchID = ((GlobalObject) UpdateService.this.getApplication()).getBranchID(); 
        RequestProperty rp = new RequestProperty("branchID", branchID); 

        properties.clear(); 
        properties.add(rp);  

        ArrayList returnArrayList = SoapInvoker.GetListFromService("GetAllActiveOrderDetailsForBranch", properties ,new OrderDetail(),UpdateService.this); 

        ArrayList retArrayList = SoapInvoker.GetListFromService("GetAllActiveOrdersForBranch", properties ,new Orders(),UpdateService.this); 

        ArrayList empTablMapList=SoapInvoker.GetListFromService("GetAllEmployeeTable", null, new EmployeeTable(), UpdateService.this); 

        ArrayList retArrlist1=SoapInvoker.GetListFromService("GetAllTableBookingsForBranch", properties, new TableBooking(), UpdateService.this); 

        ArrayList billingList=SoapInvoker.GetListFromService("GetAllBillsForBranch", properties, new Billing(), UpdateService.this); 



        long orderID=0;            

        if(Long.parseLong(bObj.getClass().getDeclaredField("OrderID").get(bObj).toString()) != 0){ 
         orderID = ((GlobalObject) UpdateService.this.getApplication()).getOrderID(); 
        } 

        if (orderID > 0) 
        { 
         NotConfirmedOrderDetails1(orderID); 
        } 

        //Start 
        Log.v("Service", "Service call ended."); 
       } 
       catch(Exception e) 
       { 

        Log.e("ServiceError", e.getMessage()); 

       } 

      } 
     }, 0, UPDATE_INTERVAL); 

     Log.i(getClass().getSimpleName(), "Timer started."); 

    } 

    private void NotConfirmedOrderDetails1(long orderID) { 

     try 
     { 
      DatabaseHelper db = new DatabaseHelper(UpdateService.this); 
      db.setOrderDetailsDataToDB(orderID); 

     } 
     catch (Exception e) { 
      CommonMethods.showErrorDialog(e.getMessage(),UpdateService.this); 
     } 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     if (timer != null) { 
      timer.cancel(); 

     } 
     Log.i(getClass().getSimpleName(), "Timer stopped."); 

    } 

    // We return the binder class upon a call of bindService 
    //The onBind() method enables you to bind an activity to a service. This in turn enables an activity 
    //to directly access members and methods inside a service. 


    @Override 
    public IBinder onBind(Intent arg0) { 
     return mBinder; 
    } 

    public class MyBinder extends Binder { 
     UpdateService getService() { 
      return UpdateService.this; 
     } 
    } 


    public Thread.UncaughtExceptionHandler onForceCloseError= new Thread.UncaughtExceptionHandler() { 
     public void uncaughtException(Thread thread, Throwable ex) { 

      Log.v("Service stopped", "Service stopped after force close error ..."); 


      // 
      unbindService(mConnection); 
      boolean ismConnectionNull = mConnection.equals(null); 
      String connection=String.valueOf(ismConnectionNull); 
      Log.v("Connection status", connection); 
      Log.v("Service", "UnbindService Called"); 
      doBindService(); 
      Log.v("Service", "BindService Called"); 
      ex.printStackTrace(); 

      // 
      Log.v("Service started", "Service started after force close error ..."); 

     } }; 

     private UpdateService s; 

     private ServiceConnection mConnection = new ServiceConnection() { 

      public void onServiceConnected(ComponentName className, IBinder binder) { 
       s = ((UpdateService.MyBinder) binder).getService(); 
       /*Toast.makeText(UpdateService.this, "Connected", 
         Toast.LENGTH_SHORT).show();*/ 
      } 

      public void onServiceDisconnected(ComponentName className) { 
       s = null; 
       Log.v("Update Servicve","Service Disconnected..."); 
      } 
     }; 

     void doBindService() { 
      bindService(new Intent(this, UpdateService.class), mConnection, 
        Context.BIND_AUTO_CREATE); 
      Log.v("bindService", "bindService called..."); 

     } 


} 

私は上記のサービスをAndroidで使用しています。いくつかの時間私はキャッチされていない例外と私のサービスstops.My実行スレッドUnCaughtExceptionHandlerに行きます。サービスを再開しようとしましたが、機能していません。誰でもこの問題で私を助けてください。アンドロイドでサービスを再起動する方法、キャッチされなかった例外の後で停止したら?

おかげで、ネハ

答えて

0


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}


それを作るために、あなたのサービス・クラスでService.START_STICKY

+1

としてフラグを使用することができます実際に私はその上で見つけたバックアップできkilledを取得した場合、あなたのonStartCommand(Intent, int, int)

@Override public int onStartCommand(Intent intent, int flags, int startId) { //your stuff return Service.START_STICKY; } 

START_STICKYを返すようにしていますキャッチされない例外、私のサービスは殺されませんでした。私はそれを確認すると、私はそれが生きていることを発見した。私のタイマーは殺されました。だから、キャッチされない例外で私は私のタイマーを再起動しました。とにかく、あなたの貴重な提案のために多くの人に感謝します。それは私の知識を加えた。 :) – user874437

0

使用このメソッドを使用し、実行時に終了される任意のサービスを再起動しますあなたが望むように行動します。

1

Sericekilledの場合、START_STICKYを使用してServiceを再起動できます。ちょうどあなたがあなたのServiceにいくつかのIntentを渡していると、それはあなたがIntentあなたがSTART_REDELIVER_INTENT

関連する問題