通过AsyncTask循环执行方法-Android? [英] Executing a method in loop via AsyncTask - Android?

查看:294
本文介绍了通过AsyncTask循环执行方法-Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程方面的菜鸟,但我在这里向您寻求建议.我正在制作一个应用程序,为您提供系统信息,例如CPU负载,RAM等.但是,当我按下启动新活动的按钮时会出现一个问题,在该活动中我可以看到系统信息,而且在该活动中,我在onCreate()方法中运行了方法 getSystemInfo(),该方法可以让我我需要的信息,这需要一些时间才能完成,这意味着黑屏,用户必须等待.

I am kinda noob in programming, but I am here to ask you for an advice. I am making an app, which serves you an system informations like CPU load, RAM, etc.. I've got all I need, and it works! But there is a problem when I push button which starts new activity in which I can see the system info, also in that activity I run a method getSystemInfo() in onCreate() method which gets me all the information I need and this takes some time until it is done which means a black screen and the user has to wait..

我想在执行getSystemInfo()方法之前启动并加载活动,该方法应该在后台循环运行,直到用户按下goBack按钮为止.

I'd like to start and load the activity before executing getSystemInfo() method which should be running in background in a loop until the user's pressed goBack button.

我已经看到了:如何在Android中正确使用AsyncTask 但是我仍然很难做.

I've seen this: How to use AsyncTask correctly in Android but it is still too difficult for me to do.

任何帮助将不胜感激.谢谢你一百万次!

Any help would be apreciated. Thank you a million times!

我的代码在这里:

public class SystemInfoActivity extends Activity{

TextView outCPU;
TextView outMEM;
TextView outTASKS;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_systeminfo);
    Log.d("MSG:", "running");

    outCPU = (TextView) findViewById(R.id.outCPU);
    outMEM = (TextView) findViewById(R.id.outMEM);
    outTASKS = (TextView) findViewById(R.id.outTASKS);

    getSystemInfo();

}

protected void getSystemInfo() {

    //**********CPU********
    RunCommandTOP top = new RunCommandTOP();
    int[] cpuUsage = top.CommandTOP();

    int user = cpuUsage[0];
    int system = cpuUsage[1];
    int total = (system + user);
    outCPU.setText("CPU usage: " + user + "% user, " + system + "% system, " + total + "% total");


    //**********RAM********
    ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
    activityManager.getMemoryInfo(memoryInfo);
    long availibleMB = memoryInfo.availMem / 1048576L;
    long totalMB = memoryInfo.totalMem / 1048576L;
    long usedMB = totalMB - availibleMB;

    outMEM.setText("RAM usage: " + usedMB + "/" + totalMB + "MB");

    //**********TASKS********
    RunCommandPS ps = new RunCommandPS();
    String commandLines = ps.CommandPS();

    int NumberLines = countTasks(commandLines);
    int Tasks = NumberLines - 1;

    outTASKS.setText("Tasks: " + Tasks);

}

private static int countTasks(String str) {
    String[] lines = str.split("\r\n|\r|\n");
    return lines.length;
}

public void onBackPressed() {
    Log.i("MSG", "Going back");
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
    finish();
    //return true;
}
}

推荐答案

简单易用.

只需在onCreate方法的下一行调用 新的Async().execute();

just call in onCreate method next line new Async().execute();

但是在onPostExecute中设置Text并更新UI, 您可以在doInBackground方法中返回一些参数,并在onPostExecute中获取它们

but set Text and update UI in onPostExecute, you can return some params in doInBackground method and get them in onPostExecute

private class Async extends AsyncTask<Void, Void, String> {

   @Override
   protected String doInBackground(Void... params) {
       String myParamsForUi = getSystemInfo();
       return myParamsForUi;
   }

   @Override
   protected void onPostExecute(String result) {
       super.onPostExecute(result);

       outTASKS.setText("Tasks: " + result);

   }

}

这篇关于通过AsyncTask循环执行方法-Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆