2016-10-26 18 views
0

長時間実行しているタスクを処理しているインテントサービスがあります。しかし、例外が発生した場合には処理中に、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

私はserviseを再起動すると、あなたがキャッチ

でそれを処理すべき解決策はないと思うが があなたの機能をsupose別々の関数内のAPIを呼び出すためにあなたのコードを入れcallAPI(のようなものです)

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

これは、 の再帰を作成します。これは、apiがSocketTimeoutException を与えるまで、あなたのIntiServiceを再起動する必要がなくなるまで、あなたのAPIはn時間呼び出されます。あなたのapiが実行されるまでonlunのままになります。 volley inbuiltのようなnuberofAttemptsのロジックを指定し実装する必要がある場合は、あなたのAPIを再試行する必要があります。 他にもボレーを使用したり、使用するために改造したりする

+0

しかし、他の作業をする前にボタンをクリックしてください。だから、その活動 - >サービスイベント(バスイベントは動作しません)任意のアイデア? – ralphgabb

関連する問題