Get the amount of steps taken without a trigger / onSensorChanged - android

Using Androids sensor libs it is possible to extract the steps taken in a trigger function called "onSensorChanged".
#Override
public void onSensorChanged(SensorEvent event) {
String steps = String.valueOf(event.values[0]);
...
}
The array "event.values[0]" will hold the current steps taken since last phone reboot as the Android Dev Docs says. Beginning to walk will call "onSensorChanged", and the steps can be collected.
Is there a way to collect the last known step count without calling "onSensorChanged" or even walking from inside any onCreate method or alike?
Example imaginary code:
SensorEventValues sev = new SensorEventValues();
Toast.(This,sev.values[0],Toast.LENGTH_LONG).show();

Related

Is it possible to see which functions are called in Logcat?

I know, there is a way to see which functions are called in log-cat is to write a log message on top for every function like this
#Override
protected void onDestroy() {
super.onDestroy();
Log.d("myTag","onDestroy function is called!");
// some logic
}
But it becomes irritating when you have more function.
So, I wonder if there is a way to see which functions are called in adb-logcat without writing log messages for every function.
I hope they can be fetched from somewhere in the stack but I couldn't find it.
You can try Hugo. In that case you have to annotate your methods with #DebugLog only. Then Hugo will generate logs for you (and will print out arguments and return value!). Example from GitHub:
#DebugLog
public String getName(String first, String last) {
SystemClock.sleep(15); // Don't ever really do this!
return first + " " + last;
}
And log output:
V/Example: ⇢ getName(first="Jake", last="Wharton")
V/Example: ⇠ getName [16ms] = "Jake Wharton"
Instead of printing log in every function. I (or most of the people) would suggest you to put debug.
To use debug first create breakpoints inside every function you want to check. To apply breakpoints simply left click in the area to the left of your code (refer image the pink circle represents a break-point).
Then to use Debug you have to press this button after successfully running your application.
As soon as the first method is called your application will pause at the break-point, then you can use F8 (or F6 if you are using eclipse settings) to move to next line, to move to next break-point you can press F9(or F8 if you are using eclipse settings). this way to can check all the functions being called.
This break-point method is really helpful if you just want to make sure that a particular function is being called.
Other than this if you still insist to know the details of all the functions you can store the stacktrace.
final StackTraceElement[] trace = new Throwable().getStackTrace())
StackTraceElement STrace = trace[1];
String className = STrace.getMethodName();

Proximity Sensor Counter gives double value in android

I am trying to make a counter that counts how many times the proximity sensor is triggered in Android.
But the problem that I am facing is, the counter counts twice every time the proximity sensor is triggered.
First, for the entry time and second for the exit time.
The code that I have so far is
public void onSensorChanged(SensorEvent event) {
textView1 = (TextView) findViewById(R.id.textView2);
if (event.values[0] == 0) {
} else {
counter++;
textView1.setText(""+counter);
}
}
For instance if the sensor is triggered the counter variable returns value of 2, how do I correct it?
Any help is appreciated.
Please note that sensor is triggered when something is close to it (e.g. hand, face, any object) and when you move object away. It means, when you put your hand close to the sensor and immediately move it away, sensor will be triggered two times. Sensor should give you two values: one for the situation, when object is close to the sensor and another one when nothing is close to the sensor. One of the values from the SensorEvent changes. I don't remember which one. You should check, which value changes and what are sensor readings for situation when object is close and when there's no object. After that, you can use this value inside the conditional "if" statement depending on the fact which moment you want to detect (object is moved close to sensor or object is moved away from the sensor).

SpeechRecognizer offline ERROR_NO_MATCH

SpeechRecognizer return ERROR_NO_MATCH in onResults when the device is offline while it's returning the partial results in onPartialResults() call back. The last time I played around with SpeechRecognizer it was working fine offline, I wonder if anyone has found a solution to it.
As a work around I use the partialResults returned in onPartialResults().
In the returned bundle "SpeechRecognizer.RESULTS_RECOGNITION" has all the terms minus the last term and "android.speech.extra.UNSTABLE_TEXT" has the last missing recognized term.
#Override
public void onPartialResults(Bundle partialResults) {
ArrayList<String> data = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
ArrayList<String> unstableData = partialResults.getStringArrayList("android.speech.extra.UNSTABLE_TEXT");
mResult = data.get(0) + unstableData.get(0);
}
To make the answer a little bit more clear, you need to enable partial results first, and to call UNSTABLE_TEXT in a specific fashion:
// When creating the intent, set the partial flag to true
intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS,true);
// When requesting results in onPartialResults(), the UNSTABLE_TEXT parameter to getSTtringArrayList() must be in quotes
ArrayList<String> unstableMatches = partialResults.getStringArrayList("android.speech.extra.UNSTABLE_TEXT");
onPartialResults() gets called multiple times now and onError() still gets called with ERROR_NO_MATCH. I ended up using a solution similar to the one listed here: https://github.com/nenick/QuAcc/blob/master/app/src/main/java/de/nenick/quacc/speechrecognition/speech/RecognizerListenerWithOfflineWorkaround.java
In a nutshell:
Keep track of partial results and whether an error was shown
Reset both in onBeginningOfSpeech()
Store partial results in the variable when onPartialResults() gets called
When onError() gets called check if result is ERROR_NO_MATCH and combine SpeechRecognizer.RESULTS_RECOGNITION with "android.speech.extra.UNSTABLE_TEXT" into your partial results variable
Call onResults()

Collect device data on android regularly

I managed to create an application that collects basic info from the device like android version,CPU,total RAM,free RAM,etc. Some of the data i need to collect is dynamic so i want my application to collect that data every minute.I also want my application to do so in the background. Here is how my code is setup:
public class Sniffer extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sniffer);
model_device=(TextView) findViewById(R.id.model1);
versiune_android=(TextView) findViewById(R.id.versiune1);
total_RAM=(TextView) findViewById(R.id.total_RAM1);
free_RAM=(TextView) findViewById(R.id.free_RAM1);
total_disk=(TextView) findViewById(R.id.total_disk1);
free_disk=(TextView) findViewById(R.id.free_disk1);
CPU=(TextView) findViewById(R.id.CPU1);
nivel_baterie=(TextView) findViewById(R.id.baterie1);
//get the requested data and set the corresponding text
getDeviceData();
}
Inside getDeviceData i read the /proc files and whatnot to obtain everything. Can i create a service that can run in the background and every x minutes update the data by calling getDeviceData again ? Let's take this scenario : start my app,check the data, leave app on, go check my email, listen to a song on youtube then i come back to my app (without closing it)and the collected data should have changed. How would i go about doing so using a service, i think this is the best solution. I need just a bump in the right direction, maybe a skeleton code on where my stuff should go. Thanks

Measure time for rendering ListView

How can I measure time that ListView/ListActivity takes for rendering its list?
I have a ListActivity that takes a long time to show (on older devices it's ~3s) and I'd like to optimize that.
EDIT:
I already have time between Activity1 and Activity2. During this time, Activity2 is doing some stuff (initializing). Among other things, it renders its list. I want to get time this activity takes to render that list. If total time between activities is 3s, I want to know whether rendering list takes 2.9s or 0.5s....
You could simply ouput the time. For example you could use the logcat
final long t0 = System.currentTimeMillis();
// code to measure
Log.w(TAG, "TEXT" + System.currentTimeMillis()-t0);
Of course you could use any other system for the ouput like a dialog or stuff. Just use what you like.
EDIT:
If you don't want to use a debug message in your code all the time you could do it like this:
Create a class called settings:
public class Settings {
public static final boolean DEBUG = true;
// If you prefer you could do use an enum
// enum debugLevel {SHOW_EVERYMESSAGE, ERRORS, IMPORTANT_MESSAGES, ...}
// In your classes you would have to check that DEBUG is less or equal than
// the debugLevel you want
}
In classes where you want to use a debug message simply do this
import xxx.yyy.Settings
class foo {
final static boolean DEBUG = Settings.DEBUG;
if(DEBUG){
// Debug messages
}
}
Now if you want to disable DEBUG messages you could simply set DEBUG = false in your Settings class.
If you want to measure between two activities you could use intents and send t0 with an intent to the other activity to compute the time. Of course you could include this with if(DEBUG){ /* code */ } statements to spare the sending of the intent in the final release. The if statements should not increase the computation of your code too dramatically.
I cannot tell if Java offers a better implementation using System.currentTimeMillis() or System.nanoTime(). Nevertheless, you should give the TimingLogger class a try. Take a look at this article describing the usage of the TimingLogger helper class.

Categories

Resources