Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 显示Android Toast成功/失败消息_Java_Android_Toast - Fatal编程技术网

Java 显示Android Toast成功/失败消息

Java 显示Android Toast成功/失败消息,java,android,toast,Java,Android,Toast,下面我的应用程序将文件从原始文件夹复制到代码中写入的分配位置,我要做的是显示一条toast消息,说明文件是否已成功写入或失败 private void runDialog(final int seconds) { progressDialog = ProgressDialog.show(this, "Please Wait...", "patch is being prepared"); new Thread(new Runnable(){ public voi

下面我的应用程序将文件从原始文件夹复制到代码中写入的分配位置,我要做的是显示一条toast消息,说明文件是否已成功写入或失败

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)
      {

      }

   {
需要什么代码,我将把它放在我现有的代码中的什么地方

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;

                       }
               };
            };

          }
      }
      }
    );


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

      }

   {

}

您可以使用Handler类,在线程末尾调用处理程序

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)
      {

      }

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

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)
      {

      }

   {

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

您可以通过在代码中使用处理程序来实现这一点

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)
      {

      }

   {
您应该将需要在操作系统上显示的信息传递给处理程序…如下所示

Message message = Message.obtain();      
message.what = 1;  // success message               
mHandler.sendMessage(message);
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)
      {

      }

   {
&基于该消息在处理程序中显示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:

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

      }

   {

}

如果您的文件复制成功,您需要将成功消息传递给处理程序…&如果出现任何异常,请传递错误消息…。非常感谢,这非常有帮助,我一直在寻找此解决方案。干杯“Priyank”是的,当然不必担心,我一直收到一个问题“mHandler无法解决”?好的……你在代码中把处理程序放在哪里?…&从哪里传递消息?我更新了我的问题,向你展示了我的代码,其中包含了传递消息和实现的处理程序。
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)
      {

      }

   {