2016-11-23 9 views
0

Iき インテントサービスがタスクを終了する前に停止していますか?

onHandleIntent(Intent intent)

で、私は別のスレッドでいくつかの作業を行っています IntentServiceなぜオペレーション(タスク)を実行する前に IntentService停止?ドキュメントを1として

public class SampleIntentService extends IntentService { 

    public static final int DOWNLOAD_ERROR = 10; 
    public static final int DOWNLOAD_SUCCESS = 11; 

    public SampleIntentService() { 
     super(SampleIntentService.class.getName()); 

    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     final String url = intent.getStringExtra("url"); 
     final ResultReceiver receiver = intent.getParcelableExtra("receiver"); 
     final Bundle bundle = new Bundle(); 
     new AsyncTask<Void, Void, String>() { 
      @Override 
      protected String doInBackground(Void... params) { 
       return downloadFile(url, receiver); 
      } 

      @Override 
      protected void onPostExecute(String filePath) { 
       super.onPostExecute(filePath); 
       bundle.putString("filePath", filePath); 
       receiver.send(DOWNLOAD_SUCCESS, bundle); 
      } 
     }; 

    } 

    private String downloadFile(String url, ResultReceiver receiver) { 
     File downloadFile = new File(Environment.getExternalStorageDirectory() + File.pathSeparator + "test.png"); 
     if (downloadFile.exists()) 
      downloadFile.delete(); 
     try { 
      downloadFile.createNewFile(); 
      URL downloadURL = new URL(url); 
      HttpURLConnection conn = (HttpURLConnection) downloadURL 
        .openConnection(); 
      int responseCode = conn.getResponseCode(); 
      if (responseCode != 200) 
       throw new Exception("Error in connection"); 
      InputStream is = conn.getInputStream(); 
      FileOutputStream os = new FileOutputStream(downloadFile); 
      byte buffer[] = new byte[1024]; 
      int byteCount; 
      while ((byteCount = is.read(buffer)) != -1) { 
       os.write(buffer, 0, byteCount); 
      } 
      os.close(); 
      is.close(); 

      String filePath = downloadFile.getPath(); 
      return filePath; 
     } catch (Exception e) { 
      receiver.send(DOWNLOAD_ERROR, Bundle.EMPTY); 
      e.printStackTrace(); 
     } 
     return null; 
    } 

} 

答えて

2

:このメソッドが処理する要求をワーカースレッドで呼び出され

abstract void onHandleIntent(Intent intent) 

は、ここに私のコードです。

このメソッドはすでにワーカースレッドで呼び出されているため、別のスレッドを開始する必要はありません。

これを実行すると、onHandleIntent(Intent intent)が返され、IntentServiceはタスクが完了したと判断し、それ自体は停止します。

出典:ねえ、これはこの質問へのunreleatedある

public class SampleIntentService extends IntentService { 

    public static final int DOWNLOAD_ERROR = 10; 
    public static final int DOWNLOAD_SUCCESS = 11; 

    public SampleIntentService() { 
     super(SampleIntentService.class.getName()); 

    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     final String url = intent.getStringExtra("url"); 
     final ResultReceiver receiver = intent.getParcelableExtra("receiver"); 
     final Bundle bundle = new Bundle(); 
     String filePath = downloadFile(url, receiver); 
     bundle.putString("filePath", filePath); 
     receiver.send(DOWNLOAD_SUCCESS, bundle); 

    } 

    private String downloadFile(String url, ResultReceiver receiver) { 
     File downloadFile = new File(Environment.getExternalStorageDirectory() + File.pathSeparator + "test.png"); 
     if (downloadFile.exists()) 
      downloadFile.delete(); 
     try { 
      downloadFile.createNewFile(); 
      URL downloadURL = new URL(url); 
      HttpURLConnection conn = (HttpURLConnection) downloadURL 
        .openConnection(); 
      int responseCode = conn.getResponseCode(); 
      if (responseCode != 200) 
       throw new Exception("Error in connection"); 
      InputStream is = conn.getInputStream(); 
      FileOutputStream os = new FileOutputStream(downloadFile); 
      byte buffer[] = new byte[1024]; 
      int byteCount; 
      while ((byteCount = is.read(buffer)) != -1) { 
       os.write(buffer, 0, byteCount); 
      } 
      os.close(); 
      is.close(); 

      String filePath = downloadFile.getPath(); 
      return filePath; 
     } catch (Exception e) { 
      receiver.send(DOWNLOAD_ERROR, Bundle.EMPTY); 
      e.printStackTrace(); 
     } 
     return null; 
    } 
+0

onHandleIntent

ここでは、更新されたコードです。しかし、GC rootとしてのhj.oの主な活動性の問題をどのように解決しましたか?私は同じ問題を抱えています。ありがとう:) 私は私の問題を検索中にGoogleキャッシュからあなたの質問に出くわしました。 – q126y

関連する問題