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..
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.
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!
My code here:
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;
}
}