-1

我下面的应用程序将文件从原始文件夹复制到代码中写入的分配位置,我想要做的是显示一条 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;

                       }
               };
            };

          }
      }
      }
    );


}

}

4

3 回答 3

0

您可以使用 Handler 类,在线程结束时调用处理程序。

但是我个人会选择 AsyncTask 类。此类允许您在线程上工作,并且在线程完成后,您可以对 UI 进行更改。

在 Android 开发者网站上阅读它。

于 2012-09-13T12:26:16.663 回答
0

您可以通过在代码中使用Handler来做到这一点。

您应该将消息传递给处理您需要在操作系统上显示的内容......如下所示......

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

并根据该消息在您的处理程序中显示 Toast...

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:

            }
    }

}

于 2012-09-13T12:37:38.130 回答
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)
      {

      }

   {
于 2012-09-13T12:48:11.433 回答