2016-10-26 62 views
0

我有一个intent服务正在处理长时间运行的任务。但是,如果发生异常,处理时会让SocketTimeOutException服务停止。如何捕获异常并重新启动过程。如何重新启动Android IntentService

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

    @Override 
    protected void onHandleIntent(Intent intent) { 

     String name = intent.getStringExtra("name"); 
     String packageName = intent.getStringExtra("packageName"); 
     String path = intent.getStringExtra("path"); 
     int downloadedSoFar = 0; 

     Intent i = new Intent(getApplicationContext(), DownloadListViewApp.class); 
     PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, i, 0); 
     nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     mBuilder = new NotificationCompat.Builder(this); 
     mBuilder.setContentTitle(name) 
       .setContentText("Download in progress") 
       .setSmallIcon(R.drawable.logo).setContentInfo("0%").setContentIntent(pi); 
     mBuilder.setOngoing(true); 

     try { 
      url = new URL(IPClass.SERVERIP + path + "/" + packageName); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.setDoInput(true); 
      connection.setDoOutput(true); 
      connection.setReadTimeout(7000); 
      int fileLength = connection.getContentLength(); 
      connection.connect(); 

      input = connection.getInputStream(); 
      output = new FileOutputStream("/sdcard/Android/appdata/tmp/downloadtmp/" + packageName, true); 

      byte data[] = new byte[1024]; 
      int count; 

      boolean continueLoop = true; 
      while ((count = input.read(data)) > 0 && continueLoop) { 
        progressChange((int) (downloadedSoFar * 100L)/fileLength, packageName); 
        downloadedSoFar = downloadedSoFar + count; 
        output.write(data, 0, count); 
      } 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } finally { 
      try { 
       if (output != null) output.close(); 
       if (input != null) input.close(); 
      } catch (IOException ignored) { 
      } 
      if (connection != null) { 
       connection.disconnect(); 
      } 
     } 

回答

0

参考: - https://stackoverflow.com/a/12776012/2705391 您可以使用此为您的问题。

@Override 
protected void onHandleIntent(Intent intent) 
{ 
    try 
    { 
     // STOP SERVICE 

     // DO YOUR WORK HERE 
    } 
    finally 
    { 
     // START SERVICE 
    } 

} 
0

你不能阻止意图服务,直到其职责已经完成,如果你想停止它,那么你应该停止报警经理这样的意图服务。

Intent DataSyncing = new Intent(getBaseContext(), DataSyncingScheduledReceiver.class); 
    DataSyncing.setAction(DataSyncingScheduledReceiver.ACTION_DATASYNC_RECEIVER); 
    PendingIntent DataSyncingIntent = PendingIntent.getBroadcast(getBaseContext(),1003, DataSyncing, PendingIntent.FLAG_CANCEL_CURRENT); 
    AlarmManager alarmManagerdatasync = (AlarmManager) getSystemService(ALARM_SERVICE); 
    alarmManagerdatasync.cancel(DataSyncingIntent); 
    DataSyncingIntent.cancel(); 
0

我想重新启动者地位是不是你应该处理它在捕捉解决方案

把你的代码中调用单独的函数 内部API supose的功能就像是callAPI()

} catch (final java.net.SocketTimeoutException e) { 
    // connection timed out...let's try again 
    callAPI() 
} 

这会创建一个递归 通过这个你的api将被称为n时间,直到你的API会给SocketTimeoutException 所以你将不需要重新启动你的IntentService。它将继续运行,直到你的api执行

如果你想防止这种情况,你必须指定和实现nuberofAttempts的逻辑,如凌乱 凌乱的内置重试策略,你应该阅读它。 其他我sugest凌空或翻新使用

+0

但我想要一个按钮先点击,然后再做任何事情。所以它的活动 - >服务事件(巴士事件不工作)有什么想法? – ralphgabb