Android 使用Asynctask显示progressbar
我有一个应用程序,我在其中进行函数调用,根据用户输入进行计算需要一些时间。我试图使用Android 使用Asynctask显示progressbar,android,android-asynctask,Android,Android Asynctask,我有一个应用程序,我在其中进行函数调用,根据用户输入进行计算需要一些时间。我试图使用AsyncTask实现进度条显示,但是当我在doinbackGround中声明函数调用时,它给出了一个错误,因为它与UI线程交互。我为函数调用创建了一个单独的线程,在UI线程中称为AsyncTask类,并同步了它们的计时,即将AsyncTask的doinbackground中的睡眠时间设置为一个较大的值,以便在该时间内完成函数调用 但这并不是一个好的解决方案,因为方法调用完成所需的时间取决于用户输入,而我现在还不
AsyncTask
实现进度条显示,但是当我在doinbackGround
中声明函数调用时,它给出了一个错误,因为它与UI线程交互。我为函数调用创建了一个单独的线程,在UI线程中称为AsyncTask类,并同步了它们的计时,即将AsyncTask的doinbackground
中的睡眠时间设置为一个较大的值,以便在该时间内完成函数调用
但这并不是一个好的解决方案,因为方法调用完成所需的时间取决于用户输入,而我现在还不知道。我还希望我的进度条连续显示,而不是离散显示。我提供了Asynctask调用类和Function调用的代码
异步任务调用类
package com.integrated.mpr;
public class Progess extends Activity {
static String[] display = new String[Model.n];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress);
Thread threada=new Thread(){
public void run() {
display = new Logic().finaldata();
// this is the function call
}
};
threada.start();
new loadSomeStuff().execute(" ");
}
public class loadSomeStuff extends AsyncTask<String,Integer,String>{
ProgressDialog dialog;
protected void onPreExecute(){
dialog = new ProgressDialog(Progess.this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(100);
dialog.setTitle("Generating the most sensitive positions");
dialog.show();
}
@Override
protected String doInBackground(String... arg0) {
for(int i=0;i<20;i++){
publishProgress(5);
try {
Thread.sleep(1200);// the timing set to a large value
} catch (InterruptedException e) {
e.printStackTrace();
}
}
dialog.dismiss();
return null;
}
protected void onProgressUpdate(Integer...progress){
dialog.incrementProgressBy(progress[0]);
}
protected void onPostExecute(String result){
Intent openList = new Intent("com.integrated.mpr.SENSITIVELIST");
startActivity(openList);
}
}
}
package com.integrated.mpr;
公共类进程扩展了活动{
静态字符串[]显示=新字符串[Model.n];
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.progress);
Thread threada=新线程(){
public void run(){
显示=新逻辑()。最终数据();
//这是函数调用
}
};
threada.start();
new loadSomeStuff().execute(“”);
}
公共类loadSomeStuff扩展异步任务{
进程对话;
受保护的void onPreExecute(){
dialog=新建ProgressDialog(progress.this);
设置ProgressStyle(ProgressDialog.STYLE_水平);
dialog.setMax(100);
对话框.setTitle(“生成最敏感位置”);
dialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…arg0){
对于(int i=0;i使用AsyncTask。它可以在onProgressUpdate和onPostExecute中与UI线程交互。
您必须将计算保留在doInBackground()中,并且只在onProgressUpdate()中更新UI。
将您的逻辑类分解为更多的方法,并像这样做
public class LogicAsync extends AsyncTask<Void, Integer, Void> {
@Override
protected Void doInBackground(Void... p) {
Logic logic = new Logic();
logic.loadArrays();
publishProgress(10); //10% done
try {
Thread.sleep(1000);
} catch {
}
logic.sortArraysAndMatrix();
publishProgress(20); //20% done
logic.copyAndSortWeightages();
publishProgress(30);
logic.finalData();
publishProgress(100);
}
@Override
protected void onProgressUpdate(Integer... progress) {
updateUIWithPercent(progress[0]);
}
}
公共类LogicAsync扩展了AsyncTask{
@凌驾
受保护的空位背景(空位…p){
逻辑=新逻辑();
logic.loadArrays();
出版进度(10);//完成10%
试一试{
睡眠(1000);
}抓住{
}
logic.sortArraysAndMatrix();
出版进度(20);//完成20%
logic.copyAndSortWeightages();
出版进度(30);
logic.finalData();
出版进度(100);
}
@凌驾
受保护的void onProgressUpdate(整数…进度){
updateUIWithPercent(进度[0]);
}
}
这是一个修改后的逻辑类,其中包含一些分解的方法
package com.integrated.mpr;
import org.apache.commons.math.linear.RealMatrix;
import org.apache.commons.math.stat.correlation.Covariance;
import org.apache.commons.math.stat.correlation.PearsonsCorrelation;
import org.apache.commons.math.util.FastMath;
import android.util.Log;
public class Logic {
int n = Model.n;
int ns = Model.ns;
double final_matrix[][] = new double[n][5];
double swap =0;
double weightage_matrix[] = new double[n];
double sorted_weightage[] = new double[n];
String display[] = new String[n];
double[] peak_matrix = new double[n];
double[] sd_matrix = new double[n];
double[] rms_matrix = new double[n];
double[] cf_matrix = new double[n];
double[] mean_matrix = new double[n];
int[] sensitive_positions = new int[n];
double[] new_sensitive = new double[n];
int[] sortsensi = new int[n];
public void loadArrays() {
for(int i=0;i<n;i++){
peak_matrix[i] = Model.timedata[i*5+0];
sd_matrix[i] = Model.timedata[i*5+1];
rms_matrix[i] = Model.timedata[i*5+2];
cf_matrix[i] = Model.timedata[i*5+3];
mean_matrix[i] = Model.timedata[i*5+4];
}
}
public void sortArraysAndMatrix() {
// Arrays sorted in asecnding order
java.util.Arrays.sort(peak_matrix);
java.util.Arrays.sort(sd_matrix);
java.util.Arrays.sort(rms_matrix);
java.util.Arrays.sort(mean_matrix);
java.util.Arrays.sort(cf_matrix);
Log.d("matrices", "sorted");
for(int i = 0;i<n;i++){
final_matrix[i][0]= peak_matrix[i];
final_matrix[i][1]= sd_matrix[i];
final_matrix[i][2]= rms_matrix[i];
final_matrix[i][3]= cf_matrix[i];
final_matrix[i][4]= mean_matrix[i];
}
}
public void copyAndSortWeightages() {
Log.d("final ", "matrix");
double temp =0;
for(int i=0;i<n;i++){
for(int j=0;j<5;j++){
temp = final_matrix[i][j];
for(int k =0;k<n;k++){
if(temp==Model.timedata[k*5+j]){
weightage_matrix[k] = weightage_matrix[k]+(i+1)*n;
}
}
}
}
//copying the values into sorted matrix;
for(int i=0;i<n;i++){
sorted_weightage[i] = weightage_matrix[i];
}
//sorting weighatge matrix in descending order
for (int i = 0;i<n; i++ )
{
for ( int j = 0 ; j < n-i-1 ; j++ )
{
if ( sorted_weightage[j] <sorted_weightage[j+1] ) {
swap = sorted_weightage[j];
sorted_weightage[j] = sorted_weightage[j+1];
sorted_weightage[j+1] = swap;
}
}
}
Log.d("sorted weightage", "matrix");
}
public String[] finaldata(){
for(int i =0;i<n;i++){
temp = sorted_weightage[i];
for(int j =0;j<n;j++){
if(temp==weightage_matrix[j]){
sensitive_positions[i]=j+1;
}
}
}
RealMatrix pcorrdata = new PearsonsCorrelation().computeCorrelationMatrix(Model.input_matrix);
// the above statement takes time depending on the user input
for(int i =0;i<n;i++){
for(int j =0;j<n;j++){
if(pcorrdata.getEntry(i, j)<0){
pcorrdata.setEntry(i, j, pcorrdata.getEntry(i, j)*-1);
}
}
}
for(int i =0;i<n;i++){
for(int j =0;j<n;j++){
Log.d(" "+i+" "+j, ""+pcorrdata.getEntry(i, j));
}
}
for(int i =0;i<n;i++){
Log.d("sensitive osition before correlation", ""+sensitive_positions[i]);
}
int[] perm_sensitive = sensitive_positions;
for(int i =0;i<ns;i++){
int temp1 = perm_sensitive[i]-1;
if(i+1<n){
for(int j=i+1;j<ns;j++){
int temp2 = perm_sensitive[j]-1;
if(pcorrdata.getEntry(temp1, temp2)>0.5){
sensitive_positions =append((temp2)+1,sensitive_positions);
}
}
perm_sensitive = sensitive_positions;
Log.d("perm", ""+perm_sensitive[0]);
Log.d("perm", ""+perm_sensitive[1]);
}
}
for(int i =0;i<n;i++){
Log.d("values",""+perm_sensitive[i]);
}
for(int i =0;i<n;i++){
display[i] = Model.posnames[perm_sensitive[i]-1];
}
return display;
}
private int[] append(int j, int[] sensitive_positions) {
// TODO Auto-generated method stub
int[] sort_sensitive = new int[n];
int z = 0;
for(int i =0;i<n;i++){
if(sensitive_positions[i]!=j){
sort_sensitive[z]=sensitive_positions[i];
z = z+1;
}
}
sort_sensitive[n-1] = j;
return sort_sensitive;
}
}
package com.integrated.mpr;
导入org.apache.commons.math.linear.RealMatrix;
导入org.apache.commons.math.stat.correlation.convariance;
导入org.apache.commons.math.stat.correlation.pearsonscorration;
导入org.apache.commons.math.util.FastMath;
导入android.util.Log;
公共类逻辑{
int n=模型n;
int ns=Model.ns;
双最终_矩阵[][]=新双[n][5];
双交换=0;
双权重矩阵[]=新的双[n];
双重排序的_权重[]=新的双重[n];
字符串显示[]=新字符串[n];
双[]峰值矩阵=新双[n];
double[]sd_矩阵=新的double[n];
double[]rms_矩阵=新的double[n];
double[]cf_矩阵=新的double[n];
double[]均值矩阵=新的double[n];
int[]敏感位置=新的int[n];
双精度[]新的敏感度=新的双精度[n];
int[]sortsensi=新int[n];
公共void加载数组(){
对于(int i=0;i使用AsyncTask。它可以在onProgressUpdate和onPostExecute中与UI线程交互。
您必须将计算保留在doInBackground()中,并且只在onProgressUpdate()中更新UI。
将您的逻辑类分解为更多的方法,并像这样做
public class LogicAsync extends AsyncTask<Void, Integer, Void> {
@Override
protected Void doInBackground(Void... p) {
Logic logic = new Logic();
logic.loadArrays();
publishProgress(10); //10% done
try {
Thread.sleep(1000);
} catch {
}
logic.sortArraysAndMatrix();
publishProgress(20); //20% done
logic.copyAndSortWeightages();
publishProgress(30);
logic.finalData();
publishProgress(100);
}
@Override
protected void onProgressUpdate(Integer... progress) {
updateUIWithPercent(progress[0]);
}
}
公共类LogicAsync扩展了AsyncTask{
@凌驾
受保护的空位背景(空位…p){
逻辑=新逻辑();
logic.loadArrays();
出版进度(10);//完成10%
试一试{
睡眠(1000);
}抓住{
}
logic.sortArraysAndMatrix();
出版进度(20);//完成20%
logic.copyAndSortWeightages();
出版进度(30);
logic.finalData();
出版进度(100);
}
@凌驾
受保护的void onProgressUpdate(整数…进度){
updateUIWithPercent(进度[0]);
}
}
这是一个修改后的逻辑类,其中包含一些分解的方法
package com.integrated.mpr;
import org.apache.commons.math.linear.RealMatrix;
import org.apache.commons.math.stat.correlation.Covariance;
import org.apache.commons.math.stat.correlation.PearsonsCorrelation;
import org.apache.commons.math.util.FastMath;
import android.util.Log;
public class Logic {
int n = Model.n;
int ns = Model.ns;
double final_matrix[][] = new double[n][5];
double swap =0;
double weightage_matrix[] = new double[n];
double sorted_weightage[] = new double[n];
String display[] = new String[n];
double[] peak_matrix = new double[n];
double[] sd_matrix = new double[n];
double[] rms_matrix = new double[n];
double[] cf_matrix = new double[n];
double[] mean_matrix = new double[n];
int[] sensitive_positions = new int[n];
double[] new_sensitive = new double[n];
int[] sortsensi = new int[n];
public void loadArrays() {
for(int i=0;i<n;i++){
peak_matrix[i] = Model.timedata[i*5+0];
sd_matrix[i] = Model.timedata[i*5+1];
rms_matrix[i] = Model.timedata[i*5+2];
cf_matrix[i] = Model.timedata[i*5+3];
mean_matrix[i] = Model.timedata[i*5+4];
}
}
public void sortArraysAndMatrix() {
// Arrays sorted in asecnding order
java.util.Arrays.sort(peak_matrix);
java.util.Arrays.sort(sd_matrix);
java.util.Arrays.sort(rms_matrix);
java.util.Arrays.sort(mean_matrix);
java.util.Arrays.sort(cf_matrix);
Log.d("matrices", "sorted");
for(int i = 0;i<n;i++){
final_matrix[i][0]= peak_matrix[i];
final_matrix[i][1]= sd_matrix[i];
final_matrix[i][2]= rms_matrix[i];
final_matrix[i][3]= cf_matrix[i];
final_matrix[i][4]= mean_matrix[i];
}
}
public void copyAndSortWeightages() {
Log.d("final ", "matrix");
double temp =0;
for(int i=0;i<n;i++){
for(int j=0;j<5;j++){
temp = final_matrix[i][j];
for(int k =0;k<n;k++){
if(temp==Model.timedata[k*5+j]){
weightage_matrix[k] = weightage_matrix[k]+(i+1)*n;
}
}
}
}
//copying the values into sorted matrix;
for(int i=0;i<n;i++){
sorted_weightage[i] = weightage_matrix[i];
}
//sorting weighatge matrix in descending order
for (int i = 0;i<n; i++ )
{
for ( int j = 0 ; j < n-i-1 ; j++ )
{
if ( sorted_weightage[j] <sorted_weightage[j+1] ) {
swap = sorted_weightage[j];
sorted_weightage[j] = sorted_weightage[j+1];
sorted_weightage[j+1] = swap;
}
}
}
Log.d("sorted weightage", "matrix");
}
public String[] finaldata(){
for(int i =0;i<n;i++){
temp = sorted_weightage[i];
for(int j =0;j<n;j++){
if(temp==weightage_matrix[j]){
sensitive_positions[i]=j+1;
}
}
}
RealMatrix pcorrdata = new PearsonsCorrelation().computeCorrelationMatrix(Model.input_matrix);
// the above statement takes time depending on the user input
for(int i =0;i<n;i++){
for(int j =0;j<n;j++){
if(pcorrdata.getEntry(i, j)<0){
pcorrdata.setEntry(i, j, pcorrdata.getEntry(i, j)*-1);
}
}
}
for(int i =0;i<n;i++){
for(int j =0;j<n;j++){
Log.d(" "+i+" "+j, ""+pcorrdata.getEntry(i, j));
}
}
for(int i =0;i<n;i++){
Log.d("sensitive osition before correlation", ""+sensitive_positions[i]);
}
int[] perm_sensitive = sensitive_positions;
for(int i =0;i<ns;i++){
int temp1 = perm_sensitive[i]-1;
if(i+1<n){
for(int j=i+1;j<ns;j++){
int temp2 = perm_sensitive[j]-1;
if(pcorrdata.getEntry(temp1, temp2)>0.5){
sensitive_positions =append((temp2)+1,sensitive_positions);
}
}
perm_sensitive = sensitive_positions;
Log.d("perm", ""+perm_sensitive[0]);
Log.d("perm", ""+perm_sensitive[1]);
}
}
for(int i =0;i<n;i++){
Log.d("values",""+perm_sensitive[i]);
}
for(int i =0;i<n;i++){
display[i] = Model.posnames[perm_sensitive[i]-1];
}
return display;
}
private int[] append(int j, int[] sensitive_positions) {
// TODO Auto-generated method stub
int[] sort_sensitive = new int[n];
int z = 0;
for(int i =0;i<n;i++){
if(sensitive_positions[i]!=j){
sort_sensitive[z]=sensitive_positions[i];
z = z+1;
}
}
sort_sensitive[n-1] = j;
return sort_sensitive;
}
}
package com.integrated.mpr;
导入org.apache.commons.math.linear.RealMatrix;
导入org.apache.commons.math.stat.correlation.convariance;
导入org.apache.commons.math.stat.correlation.pearsonscorration;
导入org.apache.commons.math.util.FastMath;
导入android.util.Log;
公共类逻辑{
int n=模型n;
int ns=Model.ns;
双最终_矩阵[][]=新双[n][5];
双交换=0;
双权重矩阵[]=新的双[n];
双重排序的_权重[]=新的双重[n];
字符串显示[]=新字符串[n];
双[]峰值矩阵=新双[n];
double[]sd_矩阵=新的double[n];
double[]rms_矩阵=新的double[n];
double[]cf_矩阵=新的double[n];
double[]均值矩阵=新的double[n];
int[]敏感位置=新的int[n];
双精度[]新的敏感度=新的双精度[n];
int[]sortsensi=新int[n];
公共void加载数组(){
对于(int i=0;i,可以在onPreExecute()方法中显示进度对话框,如下所示:
private class SaveProfileData extends AsyncTask<Void, Void, Void> {
public SaveProfileData(){
super();
}
@Override
protected void onPreExecute(){
pd = ProgressDialog.show(YourActivity.this, null, YourActivity.this.getString(R.string.lodingMessage), true);
}
@Override
protected Void doInBackground(Void... params) {
runOnUiThread(new Runnable() {
public void run() {
yourMethod();
}});
return null;
}
@Override
protected void onPostExecute(Void result){
pd.dismiss();
}
}
私有类SaveProfileData扩展异步任务{
公共存储配置文件数据(){
超级();
}
private class SaveProfileData extends AsyncTask<Void, Void, Void> {
public SaveProfileData(){
super();
}
@Override
protected void onPreExecute(){
pd = ProgressDialog.show(YourActivity.this, null, YourActivity.this.getString(R.string.lodingMessage), true);
}
@Override
protected Void doInBackground(Void... params) {
runOnUiThread(new Runnable() {
public void run() {
yourMethod();
}});
return null;
}
@Override
protected void onPostExecute(Void result){
pd.dismiss();
}
}