拷贝程序
AndroidManifest.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.administrator.testapp"> 4 5 <application 6 android:allowBackup="true" 7 android:icon="@mipmap/ic_launcher" 8 android:label="@string/app_name" 9 android:supportsRtl="true" 10 android:theme="@style/AppTheme"> 11 <activity android:name=".MainActivity" /> 12 <activity android:name=".test_activity6" /> 13 <activity android:name=".TestActivity7"></activity> 14 <activity android:name=".TestActivity8" /> 15 <activity android:name=".Main2Activity"> 16 <intent-filter> 17 <action android:name="android.intent.action.MAIN" /> 18 <category android:name="android.intent.category.LAUNCHER" /> 19 </intent-filter> 20 </activity> 21 </application> 22 23 </manifest>
activity_mani2.xml
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 tools:context="com.example.administrator.testapp.Main2Activity" 7 android:orientation="vertical"> 8 9 <Button 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:text="模拟拷贝" 13 android:id="@+id/copy"/> 14 <Button 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:text="消息机制" 18 android:id="@+id/xj"/> 19 </LinearLayout>
普通方法:
1 package com.example.administrator.testapp; 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.Button; 10 import android.widget.TextView; 11 import android.widget.Toast; 12 13 public class Main2Activity extends AppCompatActivity 14 { 15 Button copy,xj; 16 @Override 17 protected void onCreate(Bundle savedInstanceState) 18 { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main2); 21 22 copy=(Button)findViewById(R.id.copy); 23 copy.setOnClickListener(new View.OnClickListener() 24 { 25 @Override 26 public void onClick(View v) 27 { 28 final ProgressDialog progressDialog=new ProgressDialog(Main2Activity.this); 29 progressDialog.setTitle("拷贝进度"); 30 progressDialog.setMessage("拷贝中..."); 31 progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 32 progressDialog.show(); 33 new Thread() 34 { 35 @Override 36 public void run() 37 { 38 for (int i=0;i<=100;i++) 39 { 40 progressDialog.setProgress(i); 41 try 42 { 43 Thread.sleep(100); 44 } 45 catch (Exception e) 46 { 47 e.printStackTrace(); 48 } 49 } 50 progressDialog.dismiss(); 51 runOnUiThread(new Runnable() 52 { 53 @Override 54 public void run() 55 { 56 Toast.makeText(Main2Activity.this, "拷贝完成", Toast.LENGTH_SHORT).show(); 57 } 58 }); 59 } 60 }.start(); 61 } 62 });
消息机制
1 public class Main2Activity extends AppCompatActivity 2 { 3 Button copy,xj; 4 @Override 5 protected void onCreate(Bundle savedInstanceState) 6 { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.activity_main2); 9 10 xj=(Button)findViewById(R.id.xj); 11 xj.setOnClickListener(new View.OnClickListener() 12 { 13 @Override 14 public void onClick(View v) 15 { 16 final ProgressDialog progressdialog=new ProgressDialog(Main2Activity.this); 17 progressdialog.setTitle("拷贝进度"); 18 progressdialog.setMessage("请稍后...."); 19 progressdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 20 progressdialog.show(); 21 new Thread(new Runnable() 22 { 23 @Override 24 public void run() 25 { 26 for (int i=0;i<100;i++) 27 { 28 progressdialog.setProgress(i); 29 try 30 { 31 Thread.sleep(100); 32 } 33 catch (Exception e) 34 { 35 e.printStackTrace(); 36 } 37 } 38 progressdialog.dismiss(); 39 runOnUiThread(new Runnable() 40 { 41 @Override 42 public void run() 43 { 44 Toast.makeText(Main2Activity.this, "拷贝完成!", Toast.LENGTH_SHORT).show(); 45 } 46 }); 47 } 48 }).start(); 49 } 50 }); 51 } 52 }