Process getRuntime taking a long time - Android - android

I am working on a new application and I need to input some Shell command into android , however it seems that my command is taking quite a long time to start
Process process = Runtime.getRuntime().exec("top -m 10 -n 10");
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
while ((line = reader.readLine()) != null) {
//Parsing result to a StringBuffer
(...)
}
Almost half of the total time (34sec) is used to start the process , is it normal ?
Is there a way to do something. This is in an Async Task , however for the first 17sec there is nothing display on the textView which is quite annoying ...
If you have any idea , just comment !

-n 10 means update the display 10 times and then exit. You don't need 10 updates, 1 is enough. Use this command instead and it should be much faster:
top -m 10 -n 1

Related

What is the difference between ThisTime & TotalTime? why some times both values are equal, some times different?

We can able to measure the launching time of any app using "adb shell am start -w -n yourpackagename/.activityname" (Wait for launch to complete). This adb shell command will print TotalTime & ThisTime.
But I have following questions?
What is the difference between TotalTime & ThisTime?
What is the right metric to measure the performance ? ThisTime or TotalTime ?
Why sometimes both ThisTime & TotalTime values are equal? why sometimes both are different ?
Example:
adb shell am start -W -n com.android.settings/.ApplicationSettings
ThisTime: 1554
TotalTime: 42815
Please, look at the answer of this question
"thisTime": just current activity launched time
"totalTime":the activity you started may be on the bottom of activity stack. So it refers to the total time from activity searching
to current activity launched.

Android - Problems with logging the start up latency

I am trying to log the start up latency of my app. They way I am doing it is setting the start time of the app on Application.onCreate and provide a public method that returns the time.
MyApplication extends Application {
Date startUpTime;
//Declare variables
#Override
public void onCreate() {
super.onCreate();
setStartupTime();
//other initializations
}
private void setStartUpTime() {
startUpTime = new Date();
}
public Date getStartUpTime() {
return startUpTime;
}
}
MyActivity extends Activity {
.
.
.
#Override
public void onStart(){
logStartUpLatency();
//other onStart stuff
}
private void logStartUpLatency() {
Date currentTime = new Date();
Date startTime = (MyApplication)getApplicationContext().getStartUpTime();
long latency = currentTime.getTime() - startTIme.getTime();
Log.d("Start up Latency is ", Long.toString(latency)):
}
This is how I am testing my start up latency:
adb install myapk
run the app to get the first start up latency. I can see the latency logged is correct for the first start
run the app again to test the start latency. The latency logged is correct for the start(or any number of subsequent starts)
Now I increase my app's version code and name by 1. To simulate an upgrade, I used the command adb install -r myapk.
Now I run the app again to test the first start latency after upgrade, even though it takes 3 seconds, the latency logged is off the charts.
Does any one know why that might happen?
Update
So if I install the apk using "adb install -r myapk", the app isn't going through the Myapplication.onCreate().
I suggest the use of the TimingLogger class. As per the documentation, you can easily track the elapsed time and even add splits in the process.
This
TimingLogger timings = new TimingLogger(TAG, "methodA");
// ... do some work A ...
timings.addSplit("work A");
// ... do some work B ...
timings.addSplit("work B");
// ... do some work C ...
timings.addSplit("work C");
timings.dumpToLog();
produces
D/TAG (3459): methodA: begin
D/TAG (3459): methodA: 9 ms, work A
D/TAG (3459): methodA: 1 ms, work B
D/TAG (3459): methodA: 6 ms, work C
D/TAG (3459): methodA: end, 16 ms
The latency you are calculating is in milliseconds. Date#getTime() returns the number of milliseconds since Jan. 1, 1970, midnight GMT.
The observed time of 3 seconds is because of the overhead for uninstalling the old and installing the new app build.
So if I install the apk using "adb install -r myapk", the app isn't going through the Myapplication.onCreate(). So that answers this question. I will ask a separate question on why installing an application using "adb install -r myapk" and then starting myapk doesn't go through MyApplication.onCreate()

How can I get a process starting and ending time in android

I want to get starting and ending time of a process in android. I tried to use process command PS but in android it doesn't provide any process time. In Linux OS we can get a process starting time but this command doesn't work with android. Can anybody help me.
If you desire to find time taken some process you can use that:
long startTime = System.nanoTime();
// here you write your code
long endTime = System.nanoTime();
System.out.println("Took "+(endTime - startTime) + " ns");
if you want time in ms do : (endTime - startTime)/0.000001

android - Bluetooth: program stuck at inputstream reading

Helo.
Im developing an application that transferes data over bluetooth(with a flight recorder device). When i am recieving a lot of data data(3000 - 40000 lines of text, depends of the file size) my aplication seems to stop recieving the data. I recieve the data with InputStream.read(buffer). For example: I send a command to the flight recorder, it starts sending me a file(line by line), on my phone i recieve 120 lines and then the app stucks.
Intresting is that on my HTC Desire the app stucks just sometimes, on the Samsung Galaxy S phone the application stucks every single time i try to recive more than 50 lines.
The code is based on the BluetoothChat example. This is the part of code where i am listening to the BluetoothSocket:
byte[] buffer = new byte[1024];
int bytes =0;
while(true)
{
bytes = mmInStream.read(buffer);
readMessage = new String(buffer, 0, bytes);
Log.e("read", readMessage);
String read2 = readMessage;
//searching for the end of line to count the lines(the star means the start of the checksum)
int currentHits = read2.replaceAll("[^*]","").length();
nmbrOfTransferedFligts += currentHits;
.
.
.
//parsing and saving the recieved data
I must say that i am running this in a while(true) loop, in a Thread, that is implemented in an android Service. The app seems to stuck at "bytes = mmInStream.read(buffer);"
I have tried to do this with BufferedReader, but with no success.
Thanks.
The app seems to stuck at "bytes = mmInStream.read(buffer);"
But that is normal behavior: InputStream.read(byte[]) blocks when there is no more data available.
This suggests to me that the problem is on the other end or in the communication between the devices. Is is possible that you have a communication problem (which is a bit different on the Galaxy vs. the Desire) that is preventing more data from being received?
Also, I would suggest that you wrap a try/catch around the read statement to be sure that you catch any possible IOException's. Though I guess you would have seen it in logcat if that were happening.
Speaking of logcat, I would suggest that you look at the logcat statements that Android itself it generating. I find that it generates a lot for Bluetooth and this might help you to figure out whether there really is any more data to be read().

Unable to execute sendevent shell command through the android code

I have tried following code snippet to execute the batch of command for sendevent to click the coordinate 44,129 on the emulator. But it is not showing any result. But if i am giving the same batch of command to the shell prompt it is able to click the mentioned coordinate succesfully.
String[] cmmandemulatorarr = {"/system/bin/sendevent /dev/input/event0 3 0 44",
"/system/bin/sendevent /dev/input/event0 3 1 129",
"/system/bin/sendevent /dev/input/event0 1 330 1",
"/system/bin/sendevent /dev/input/event0 0 0 0",
"/system/bin/sendevent /dev/input/event0 1 330 0",
"/system/bin/sendevent /dev/input/event0 0 0 0", };
for (int i = 0; i < cmmandemulatorarr.length; i++) {
Process process =
Runtime.getRuntime().exec(cmmandemulatorarr[i]);
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
Is there is anything i am missing here or i have to try something else to get click event on some coordinate through the code.
Note :: I am not getting any exception in the log while executing the code which seems that command is executed successfully.
Regards
Pinu
But it is not showing any result.
This is a good thing.
But if i am giving the same batch of command to the shell prompt it is able to click the mentioned coordinate succesfully.
The shell runs with root-level privileges. Your SDK application does not, unless you root your device and arrange to execute your code that way.
Bear in mind that not all devices will have a /system/bin/sendevent command and it can be removed at any time. This is not part of the Android SDK.
i have to try something else to get click event on some coordinate through the code.
This is not possible from the Android SDK for ordinary devices, for obvious security reasons.
Here you have answer. You need to find touch event id with getevent. Sendevent use decimal space, getevent use hex. This code from first "answer" works on 7.0.

Categories

Resources