2012-09-13 220 views
-1

我的應用程序在下面將原始文件夾中的文件複製到代碼中寫入的分配位置,我想要做的是顯示Toast消息,指出文件是否已成功寫入或者是否失敗了。顯示Android Toast成功/失敗消息

需要什麼代碼以及我將它放在現有代碼歡呼聲中的位置。

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/com.glu.android.brawler/files/" + filename); 
       copyFile(in, out); 
       in.close(); 
       in = null; 
       out.flush(); 
       out.close(); 
       out = null; 
       } catch(Exception e) { 
        Log.e("tag", e.getMessage()); 
        Message message = Message.obtain();  
        message.what = 1;    
        mHandler.sendMessage(message); 
       }  

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

      Handler mHandler = new Handler() { 
       public void handleMessage(Message msg) 
       { 
        Toast toast; 
         switch(msg.what) 
         {   
          case 1: // for success 
           toast = Toast.makeText(getBaseContext(), "File has been successfully written.", Toast.LENGTH_LONG); 
           toast.show(); 
          break; 
          case 0: // for Error 
           toast = Toast.makeText(getBaseContext(), "Error occur during writting file.", Toast.LENGTH_LONG); 
           toast.show(); 
          break; 

         } 
       }; 
      }; 

      } 
     } 
     } 
    ); 


} 

}

回答

0

您可以在代碼中使用處理程序來完成此操作。

你應該傳遞給處理程序中需要顯示在OS ....像下面什麼消息?

Message message = Message.obtain();  
message.what = 1; // success message    
mHandler.sendMessage(message); 

&顯示吐司基於該郵件處理程序...

Handler mHandler = new Handler() { 
    public void handleMessage(Message msg) 
    { 
     Toast toast; 
      switch(msg.what) 
      {   
       case 1: // for success 
        toast = Toast.makeText(getBaseContext(), "File has been successfully written.", Toast.LENGTH_LONG); 
        toast.show(); 
       break; 
       case 0: // for Error 
        toast = Toast.makeText(getBaseContext(), "Error occur during writting file.", Toast.LENGTH_LONG); 
        toast.show(); 
       break: 

      } 
    } 

}

+0

你的情況,你需要通過成功的消息處理程序時您的文件複製蘇當有任何異常發生時......並通過錯誤信息...... –

+0

非常感謝你的幫助很多,我一直在尋找這個解決方案,歡呼聲「Priyank」 – user1305471

+0

是的,當然不用擔心,只有一個問題我保留得到「mHandler無法解決」? – user1305471

0

您可以使用Handler類的工作,叫在你的線程結束的處理程序。

但是personnaly我會選擇AsyncTask類。 此類允許您在線程上工作,並且在線程完成後,您可以對UI進行更改。

在Android開發者網站上閱讀它。

0
private void runDialog(final int seconds) 
{ 
    progressDialog = ProgressDialog.show(this, "Please Wait...", "patch is being prepared"); 

    new Thread(new Runnable(){ 
     public void run(){ 
      try { 
       Thread.sleep(seconds * 1000); 
       progressDialog.dismiss(); 
         runOnUiThread(new Runnable() { 
        public void run() { 
           Toast.makeText(ActivityName.this,"Message",Toast.LENGTH_SHORT).show(); 
                }}); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
    }).start(); 

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

     } 

    { 
相關問題