display Android Toast success/unsuccessful message

倾然丶 夕夏残阳落幕 提交于 2019-12-13 23:46:56

问题


my app below copies a file from the raw folder to the allocated location written in the code, What i want to do is display a toast message stating whether the file has been successfully written or whether it has failed.

What code is needed and where would i place it within my existing code cheers guy's.

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;

                       }
               };
            };

          }
      }
      }
    );


}

}


回答1:


You can do this by using Handler in your code.

You should pass the message to handler what you need to display on OS....like below...

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

& show Toast in your handler based on that 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:

            }
    }

}




回答2:


You can work with the Handler class, calling a handler at the end of your thread.

But personnaly I would opt for the AsyncTask class. This class allows you to work on threads and after the thread is finished, you can make changes to the UI.

Read up on it on the Android Developers Website.




回答3:


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

      }

   {


来源:https://stackoverflow.com/questions/12406106/display-android-toast-success-unsuccessful-message

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!