Android——模拟文件拷贝
Posted 柒寒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android——模拟文件拷贝相关的知识,希望对你有一定的参考价值。
模拟文件拷贝:
要求:要用progressDialog和子线程来模拟显示拷贝进度;进度完成后在主界面提示拷贝完成,分别使用普通方式和消息机制编写。
layout文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.hanqi.testapp2.PractiseActivity6" 11 android:orientation="vertical"> 12 13 <Button 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:text="文件拷贝__普通方式" 17 android:onClick="bt_kaobei1"/> 18 <Button 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:text="文件拷贝__消息机制" 22 android:onClick="bt_kaobei2"/> 23 </LinearLayout>
java类:
1 package com.hanqi.testapp2; 2 3 import android.app.ProgressDialog; 4 import android.os.Bundle; 5 import android.os.Handler; 6 import android.os.Message; 7 import android.support.v7.app.AppCompatActivity; 8 import android.view.View; 9 import android.widget.Toast; 10 11 public class PractiseActivity6 extends AppCompatActivity { 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_practise6); 17 } 18 public void bt_kaobei1(View v) 19 { 20 final ProgressDialog progressDialog = new ProgressDialog(this); 21 progressDialog.setTitle("文件复制"); 22 progressDialog.setMessage("复制中,请等待……"); 23 progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 24 progressDialog.setCancelable(false); 25 progressDialog.show(); 26 new Thread(){ 27 @Override 28 public void run() { 29 for (int i=0;i<=100;i++) 30 { 31 progressDialog.setProgress(i); 32 try { 33 Thread.sleep(3000); 34 } 35 catch (Exception e) 36 { 37 38 } 39 } 40 runOnUiThread(new Runnable() { 41 @Override 42 public void run() { 43 44 Toast.makeText(PractiseActivity6.this, "文件拷贝完成", Toast.LENGTH_SHORT).show(); 45 } 46 }); 47 progressDialog.dismiss(); 48 } 49 }.start(); 50 } 51 Handler h = new Handler(){ 52 @Override 53 public void handleMessage(Message msg) { 54 super.handleMessage(msg); 55 if (msg.what ==1) 56 { 57 final ProgressDialog progressDialog = new ProgressDialog(PractiseActivity6.this); 58 progressDialog.setTitle("文件复制"); 59 progressDialog.setMessage("复制中,请等待……"); 60 progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 61 progressDialog.setCancelable(false); 62 progressDialog.show(); 63 new Thread(){ 64 @Override 65 public void run() { 66 for (int i=0;i<=100;i++) 67 { 68 progressDialog.setProgress(i); 69 try { 70 Thread.sleep(3000); 71 } 72 catch (Exception e) 73 { 74 75 } 76 } 77 runOnUiThread(new Runnable() { 78 @Override 79 public void run() { 80 81 Toast.makeText(PractiseActivity6.this, "文件拷贝完成", Toast.LENGTH_SHORT).show(); 82 } 83 }); 84 progressDialog.dismiss(); 85 } 86 }.start(); 87 } 88 } 89 }; 90 public void bt_kaobei2(View v) 91 { 92 new Thread(){ 93 @Override 94 public void run() { 95 Message m = Message.obtain(); 96 m.what = 1; 97 h.sendEmptyMessage(1); 98 } 99 }.start(); 100 } 101 }
效果为:
以上是关于Android——模拟文件拷贝的主要内容,如果未能解决你的问题,请参考以下文章
Android 逆向修改运行中的 Android 进程的内存数据 ( 运行环境搭建 Android 模拟器安装 | 拷贝 Android 平台可执行文件和动态库到 /data/system )(代码片