-3

我对 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/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;  // success message
                                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(), "Created By MRxBIGxSTUFF", Toast.LENGTH_SHORT);
                        toast.show();
                        break;
                    case 0: // for Error
                        toast = Toast.makeText(getBaseContext(), "Error... Application has shutdown", Toast.LENGTH_LONG);
                        toast.show();
                        break;
                    }
                }
        };
}
4

2 回答 2

0

要展示 Toast,您必须自己制作并展示它。像这样:

CharSequence text = "Hello toast!";
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();

在此处阅读有关 Toast的更多信息。

而且你的代码是错误的,因为你硬编码/sdcard/错误的。使用getExternalStorageDirectory()

于 2012-09-13T16:15:23.667 回答
0

我建议使用 AsyncTask 在后台运行您的进程。这不会冻结复制文件大小变大的用户界面事件。

您可以在加载操作时显示进度条。

public class CopyFiles extends AsyncTask<Void, Void, Void>
{  
@Override
protected void onPreExecute()
{  
    super.onPreExecute();
    dialog.setMessage("Please wait...");
    dialog.setCancelable(false);
    dialog.show();

} 
@Override 
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
    // Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
 }

@Override 
protected void onPostExecute(Void result)

{ 
      //Post Execute
       dialog.cancel();
      //Use toast here to toast the message
      //Check condition toast message
      Toast.makeText(getBaseContext(), "Created By MRxBIGxSTUFF", Toast.LENGTH_SHORT);
}
@Override
protected Void doInBackground(Void... params) {

  // Your operation..Dont Edit Any Views here

    return null;
}
} 

我认为这是处理文件的最佳方式之一(即复制、下载等)

希望能帮助到你

于 2012-09-13T16:18:53.153 回答