2012-10-26 2 views
0

100MB의 이미지를 다운로드해야하므로 서비스를 다운로드하는 것이 가장 좋은 방법이며, 활동중인 각 파일에 대한 결과가 표시됩니다. 그러나이 서비스는 모든 서비스를 다운로드 한 후에 만 ​​작동하며, 서비스는 해제됩니다. 서비스에서 Activity TextView 업데이트

는 Heres는 활동의 코드 : 서비스의

public class DownloadActivity extends Activity 
{ 
String hist; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.download_activity); 

    startService(new Intent(this, DownloadService.class)); 
    registerReceiver(broadcastReceiver, 
      new IntentFilter(DownloadService.BROADCAST_ACTION)); 
} 

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() 
{ 
    @Override 
    public void onReceive(Context context, Intent _intent) 
    { 
     updateUI(_intent);  
    } 
}; 

private void updateUI(Intent intent) 
{ 
    if (intent.getBooleanExtra("exists", false)) 
     hist = hist + "Item " + 
       intent.getIntExtra("item", -1) + ", image " + 
       intent.getIntExtra("obraz", -1) + " - DOWNLOADED\n"; 
    else 
     hist = hist + "Item " + 
       intent.getIntExtra("item", -1) + ", image " + 
       intent.getIntExtra("obraz", -1) + " - ALREADY EXISTS\n"; 

    ((TextView) findViewById(R.id.dtitle)).setText("Item " + 
      intent.getIntExtra("item", -1) + ", image " + 
        intent.getIntExtra("image", -1) + "."); 

    ((TextView) findViewById(R.id.ddetails)).setText(hist); 
} 
} 

코드 : 내가 잘못

public class DownloadService extends Service 
{ 
public static final String BROADCAST_ACTION = "emis.katalog.grzybow.publishprogress"; 
Intent intent; 
int counter = 0; 
String postString; 

@Override 
public IBinder onBind(Intent arg0) 
{ 
    // TODO Auto-generated method stub 
    return null; 
} 
@Override 
public void onCreate() 
{ 
    super.onCreate(); 
    intent = new Intent(BROADCAST_ACTION); 
} 

@Override 
public void onStart(Intent intent, int startId) 
{ 
    SQLiteDatabase db = new BazaGrzybowHelper(DownloadService.this).getReadableDatabase(); 
    Cursor kursor = db.rawQuery("SELECT * FROM table", null); 

    InputStream in = null; 
    OutputStream out = null; 
    URL ulrn; 

    int nn = 1; 
    int pos = 1; 

    //out: 
     while(kursor.moveToNext()) 
     { 
      while(kursor.getString(kursor.getColumnIndex("i_url_" + nn)) != "" || 
        kursor.getString(kursor.getColumnIndex("i_url_" + nn)) != null) 
      { 
       String filename = "thg_" + pos + "_" + (nn+2) + ".jpg"; 
       if (new File(Environment.getExternalStorageDirectory(), 
         "emis/katalog.grzybow/zapis_na_stale/"+filename).exists()) 
        publishProgress(pos, nn, true); 
       else 
       { 
        publishProgress(pos, nn, false); 


        File destDir = new File(Environment.getExternalStorageDirectory(), 
          "emis/katalog.grzybow/zapis_na_stale"); 
        if (!destDir.exists()) 
         destDir.mkdirs(); 
        destDir = null; 

        try 
        { 
         ulrn = new URL(kursor.getString(kursor.getColumnIndex("i_url_" + nn))); 
         HttpURLConnection con = (HttpURLConnection)ulrn.openConnection(); 
         in = con.getInputStream(); 
         out = new FileOutputStream(Environment.getExternalStorageDirectory(). 
           getPath() + "/emis/katalog.grzybow/zapis_na_stale/" + filename); 
         copyFile(in, out); 
         in.close(); 
         in = null; 
         out.flush(); 
         out.close(); 
         out = null; 
        } 
        catch(Exception e) 
        { 
         e.printStackTrace(); 
        } 
       } 
       nn++; 

       if (nn > 10 || kursor.getString(kursor.getColumnIndex("i_url_" + nn)) == "" || 
         kursor.getString(kursor.getColumnIndex("i_url_" + nn)) == null) 
       { 
        nn = 1; 
        break; 
       } 
       /*if (anuluj) 
        break out;*/ 
      } 
      pos++; 
     } 

    db.close(); 
} 

private void publishProgress(int item, int image, boolean exists) 
{ 
    intent.putExtra("item", item); 
    intent.putExtra("image", image); 
    intent.putExtra("exists", exists); 
    sendBroadcast(intent); 
} 

private void copyFile(InputStream in, OutputStream out) throws IOException 
{ 
    byte[] buffer = new byte[1024]; 
    int read; 
    while((read = in.read(buffer)) != -1) 
    { 
     out.write(buffer, 0, read); 
    } 
} 

} 

을 뭐하는 거지?

답변

3

아마도 this?

주의 : 서비스가 자신의 스레드를 생성하지 않습니다 자사의 호스팅 프로세스 - 더 서비스의 메인 스레드에서 실행 (달리 지정하지 않는 한) 별도의 프로세스에서 실행되지 않습니다. 즉, 서비스가 CPU 집약적 작업 또는 차단 작업 (예 : MP3 재생 또는 네트워킹)을 수행하는 경우 해당 작업을 수행하려면 서비스 내에 새 스레드 을 만들어야합니다. 별도의 스레드를 사용하면 은 응용 프로그램 응답 없음 (ANR) 오류의 위험을 줄이고 응용 프로그램의 주 스레드는 사용자 작업에 전용으로 남아 있습니다.

+0

고마워요! 그 모든 것을 설명합니다 :) – user1555754

관련 문제