程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-在未捕獲異常的情況下重啟停止了的服務

android-在未捕獲異常的情況下重啟停止了的服務

編輯:編程綜合問答
在未捕獲異常的情況下重啟停止了的服務
 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);

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

                    //開始
                    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.");

    }



    @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 ...");


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

            }

            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有上述服務代碼。系統提醒我獲得未捕獲的異常,且服務也停止了。執行線程進入UnCaughtExceptionHandler狀態。我重新啟動服務,但是啟動不了。問題出在哪裡啊?

最佳回答:


如果服務被終止,你可以使用STARTSTICKY來重啟服務。只是你要返回onStartCommand(Intent, int, int)方法中的STARTSTICKY

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

      return Service.START_STICKY;
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved