复制文件后显示Android Toast消息
我对安卓系统的开发相当陌生,所以我想我应该从一个基本的应用程序开始。当按下按钮时,它会将文件复制到代码中写入的位置(请参见下面的“我的代码”)。当我按下安装按钮并将文件复制到其位置时,我希望toast消息显示“成功安装或复制文件时出错”。我将如何实现这一点复制文件后显示Android Toast消息,android,toast,Android,Toast,我对安卓系统的开发相当陌生,所以我想我应该从一个基本的应用程序开始。当按下按钮时,它会将文件复制到代码中写入的位置(请参见下面的“我的代码”)。当我按下安装按钮并将文件复制到其位置时,我希望toast消息显示“成功安装或复制文件时出错”。我将如何实现这一点 public class TrialActivity extends Activity { private ProgressDialog progressDialog; /** Called when the activit
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;
}
}
};
}
要展示烤面包,你必须自己制作并展示。像这样:
CharSequence text = "Hello toast!";
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
阅读更多关于祝酒的内容
当您硬编码
/sdcard/
时,您的代码出现错误,这是错误的。使用我建议使用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;
}
}
public类CopyFiles扩展异步任务
{
@凌驾
受保护的void onPreExecute()
{
super.onPreExecute();
setMessage(“请稍候…”);
对话框。可设置可取消(false);
dialog.show();
}
@凌驾
受保护的void onProgressUpdate(void…值){
super.onProgressUpdate(值);
//正在执行长时间运行的操作时要执行的操作。例如更新ProgessDialog
}
@凌驾
受保护的void onPostExecute(void结果)
{
//后执行
dialog.cancel();
//在这里使用toast来祝酒
//检查条件toast消息
Toast.makeText(getBaseContext(),“由MRxBIGxSTUFF创建”,Toast.LENGTH_SHORT);
}
@凌驾
受保护的Void doInBackground(Void…参数){
//您的操作..不要在此处编辑任何视图
返回null;
}
}
我认为这是处理文件(即复制、下载等)的最佳方式之一
希望它能有所帮助据我所知,您只是在出现异常情况时向处理程序发送一条消息。对不起,我的坏消息并不是要包含处理程序位,我在网上找到了一些不同的代码,但没有成功,我将使用什么代码来捕获文件是否已复制?@user1305471因此,您在使用代码时根本不了解它在做什么?它迟早会给你带来麻烦的。