将文件从一个位置复制到另一个位置

发布于 2024-11-02 17:52:19 字数 112 浏览 3 评论 0原文

我正在将文件从 SD 卡中的一个位置复制到另一个位置。 现在,在复制开始时,我使用进度对话框,但是我如何知道我的文件已传输以关闭进度对话框。我无法关闭进度栏,因为我不知道如何获取传输消息完全的。 请帮我出去..

I am copying file from one location to another location in sdcard.
now at the time when copy starts i use a progress Dialog,but how can i know that my file has been transferred to close the progress dialog.I am not able to close progress Bar because I don't know how to get message of transfer complete.
Please help me Out..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人练习一个人 2024-11-09 17:52:19

看看这篇最近的帖子和弗拉基米尔的回答,我认为服务可能有点矫枉过正(取决于您的具体情况)。 AsyncTask 可能是正确的选择。

想要在Activity的onCreate中显示AlertDialog - android

Have a look at this very recent post and Vladimir's answer, I think a Service may be overkill (depending on your exact situation). AsyncTask would probably be the way to go.

Want to display AlertDialog in onCreate of Activity - android

心意如水 2024-11-09 17:52:19

启动将文件复制到 SD 卡的服务。我推荐 IntentService,因为它会自动为您创建一个单独的线程。文件复制完成后,从您的服务发送包含您意图的广播。然后回到您的活动类中创建一个将处理广播的广播接收器。不要忘记将您的服务包含在 androidmanifest.xml 文件中。这是一些代码:

此代码将在您的服务结束时

Intent i = new Intent();
i.setAction("com.me.custom.intent.filecopied.success");
sendBroadcast(i);

如果文件复制失败,请执行此操作

Intent i = new Intent();
i.setAction("com.me.custom.intent.filecopied.fail");
sendBroadcast(i);

然后回到您的主活动类,您必须收到意图:

MyBroadcastReceiver intentReceiver = new MyBroadcastReceiver ();

    public class MyBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            pdialog.dismiss();//dismiss your dialog
            if (arg1.getAction().toString().equals("com.me.custom.intent.filecopied.success")) {
                //Do whatever on file being copied successfully
            } else if (arg1.getAction().toString().equals("com.me.custom.intent.filecopied.fail"L)) {
                //Do whatever on file not being copied successfully
            }
        }

    }

此外,在您的活动中,您必须注册/取消注册广播

@Override
protected void onPause() {
    // TODO Mark time user logged out
    unregisterReceiver(intentReceiver );
    super.onPause();
}

@Override
protected void onResume() {
    // TODO Add login check
    IntentFilter filter = new IntentFilter("com.me.custom.intent.filecopied.success");
    filter.addAction("com.me.custom.intent.filecopied.fail");
    registerReceiver(intentReceiver , filter);
    super.onResume();
}

Start a service that will copy the file to the sd card. I recommend IntentService because it automatically creates a separate thread for you. Once the file is done being copied send a Broadcast from your service with your intent. Then back in your activity class create a Broadcast Receiver that will handle the broadcast. Don't forget to include your service in the androidmanifest.xml file. Here is some code:

This code will go at the end of your service

Intent i = new Intent();
i.setAction("com.me.custom.intent.filecopied.success");
sendBroadcast(i);

If the file failed to copy do this

Intent i = new Intent();
i.setAction("com.me.custom.intent.filecopied.fail");
sendBroadcast(i);

Then back in your main activity class you must receive the intent:

MyBroadcastReceiver intentReceiver = new MyBroadcastReceiver ();

    public class MyBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            pdialog.dismiss();//dismiss your dialog
            if (arg1.getAction().toString().equals("com.me.custom.intent.filecopied.success")) {
                //Do whatever on file being copied successfully
            } else if (arg1.getAction().toString().equals("com.me.custom.intent.filecopied.fail"L)) {
                //Do whatever on file not being copied successfully
            }
        }

    }

Also in your activity you must register/unregister the broadcast

@Override
protected void onPause() {
    // TODO Mark time user logged out
    unregisterReceiver(intentReceiver );
    super.onPause();
}

@Override
protected void onResume() {
    // TODO Add login check
    IntentFilter filter = new IntentFilter("com.me.custom.intent.filecopied.success");
    filter.addAction("com.me.custom.intent.filecopied.fail");
    registerReceiver(intentReceiver , filter);
    super.onResume();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文