2012-09-13 2 views
-2

저는 Android 개발에 처음으로 익숙해 졌으므로 기본 응용 프로그램으로 시작할 것이라고 생각했습니다. 버튼을 누르면 코드에 기록 된 위치로 파일을 복사합니다 (아래 코드 참조). 설치 버튼을 누르면 파일이 그 위치에 복사 될 때 "파일을 복사하는 동안 성공적으로 설치되었거나 오류가 발생했습니다"라는 토스트 메시지가 나타납니다. 어떻게 구현할 수 있습니까?파일 복사 후 성공 메시지 표시

public class TrialActivity extends Activity { 

private ProgressDialog progressDialog; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


    runDialog(5); 
} 

private void runDialog(final int seconds) 
{ 
    progressDialog = ProgressDialog.show(this, "Please Wait...", "unpacking patch in progress"); 

    new Thread(new Runnable(){ 
     public void run(){ 
      try { 
       Thread.sleep(seconds * 1000); 
       progressDialog.dismiss(); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
    }).start(); 

    ((Button)findViewById(R.id.button1)).setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View paramView) 
     { 

     } 

    { 

       InputStream in = null; 
       OutputStream out = null; 
    String filename="savegame.bin";   
    try {  

       in = getResources().openRawResource(R.raw.savegame); 
       out = new FileOutputStream("/sdcard/Android/data/files/" + filename); 
       copyFile(in, out); 
       in.close(); 
       in = null; 
       out.flush(); 
       out.close(); 
       out = null; 
       } catch(Exception e) { 
        Log.e("tag", e.getMessage()); 
       }  

     } 
     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); 


      } 
     } 
     } 
    ); 
    } 

} 
+0

이 링크 ... HTTP를 참조하십시오 //stackoverflow.com/questions/12406106/display-android-toast-success-unsuccessful-message/12406455#12406455 –

+0

You 당신의 괄호'{}'를 완전히 잃어 버렸습니다 ... 당신은'OnClickListener' 안에있는 두 개의 copy 메소드를 가지고 있지만'onClick()'메소드의 일부는 아닙니다 ... 어쨌든, 여러분이 복사 한 파일을 복사하면 원래 크기와 복사 된 파일의 크기를 비교할 수 있습니다. – Sam

답변

1

사용 다음 코드 :

private class AsyncTaskDialog extends AsyncTask<Void, Void, Void> { 
    Toast toastSuccess; 


    boolean flagSuccessCopy =false; 
      protected void onPreExecute() { 
       super.onPreExecute(); 


       } 
      } 

      protected Boolean doInBackground(Void... params) { 
       copyFiles(); 
       return null; 
      } 

      @Override 
      protected void onPostExecute(Boolean result) { 
       super.onPostExecute(result); 

       if (getActivity() != null) { 
       if(flagSuccessCopy){ 
        toastSuccess = Toast.makeText(context, "Success", duration); 
       }else{ 
         toastSuccess = Toast.makeText(context, "Error", duration); 
       } 
       toast.show();    
      } 
     } 
+0

코드에 대한 설명과 질문 작성자가 사용할 코드를 입력하십시오. – Sam

+0

작성자가 파일 복사를 원하고 성공한 복사 파일 또는 성공하지 못했다는 메시지를 얻으려고합니다. 백그라운드에서 복사 할 때 AsyncTask를 사용하는 것이 좋습니다. 나는 장소 작성자가 사용자의 시간 복사 파일에 진행률 대화 상자를 추가했습니다. –

+0

또는 파일 복사를 위해 서비스를 생성하고 필요할 때 전화하십시오. 필요하다면 Comand가 될 수 있습니다. 많은 파일을 다른 장소에 복사 할 수 있습니다. 나만의 시야 –

-1

은 SD 카드에 응용 프로그램에서 모든 파일을 복사 할 수 있습니다

import ru.gelin.android.sendtosd.R; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.widget.ProgressBar; 
import android.widget.TextView; 

public class ProgressDialog extends AlertDialog implements Progress { 

/** Activity for which the dialog is created */ 

Activity activity; 

/** Progress manager for the dialog */ 
ProgressManager manager = new ProgressManager(); 

/** 
* Creates the customized progress dialog for 
* activity. 
*/ 
protected ProgressDialog(Activity activity) { 
    super(activity); 
} 

@Override 
public void setFiles(int files) { 
    synchronized (manager) { 
     manager.setFiles(files); 
    } 
    activity.runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      synchronized (manager) { 
       updateTotalProgress(); 
      } 
     } 
    }); 
} 

@Override 
public void nextFile(final File file) { 
    synchronized (manager) { 
     manager.nextFile(file); 
    } 
    activity.runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      synchronized (manager) { 
       updateFileName(file); 
       updateFileProgress(); 
       updateTotalProgress(); 
      } 
     } 
    }); 
} 

@Override 
public void processBytes(long bytes) { 
    synchronized (manager) { 
     manager.processBytes(bytes); 
    } 
    activity.runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      synchronized (manager) { 
       updateFileProgress(); 
      } 
     } 
    }); 
} 

void updateTotalProgress() { 
    ProgressBar progress = (ProgressBar)findViewById(R.id.total_progress); 
    progress.setMax(manager.getFiles()); 
    progress.setProgress(manager.getFile()); 
    TextView text = (TextView)findViewById(R.id.total_files); 
    text.setText(getContext().getString(R.string.files_progress, 
      manager.getFile(), manager.getFiles())); 
} 

void updateFileName(File file) { 
    if (file == null) { 
     return; 
    } 
    TextView view = (TextView)findViewById(R.id.file_name); 
    view.setText(file.getName()); 
} 

void updateFileProgress() { 
    ProgressBar progress = (ProgressBar)findViewById(R.id.file_progress); 
    if (manager.getProgressInUnits() < 0) { 
     progress.setIndeterminate(true); 
    } else { 
     progress.setIndeterminate(false); 
     progress.setMax((int)(manager.getSizeInUnits() * 10)); 
     progress.setProgress((int)(manager.getProgressInUnits() * 10)); 
    } 
    TextView text = (TextView)findViewById(R.id.file_size); 
    text.setText(getContext().getString(manager.getSizeUnit().progressString, 
      manager.getProgressInUnits(), manager.getSizeInUnits())); 
    } 

} 

소스 : http://code.google.com/p/sendtosd-android/source/browse/src/ru/gelin/android/sendtosd/progress/ProgressDialog.java?spec=svn0933973391a12bee4759a32f5776864c64022d71&r=0933973391a12bee4759a32f5776864c64022d71

관련 문제