csh_34 2012-10-18 03:12 采纳率: 0%
浏览 3097
已采纳

在未捕获异常的情况下重启停止了的服务

 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状态。我重新启动服务,但是启动不了。问题出在哪里啊?

  • 写回答

2条回答

  • zxd_72 2012-10-22 10:02
    关注

    如果服务被终止,你可以使用STARTSTICKY来重启服务。只是你要返回onStartCommand(Intent, int, int)方法中的STARTSTICKY

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    
          return Service.START_STICKY;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题