android runonuithread() function not executing - android

I am developing an application that uses a third party tts, named flite. I am doing something like as the tts speaks a sentence. I want to highlight each word as it is spoken. For this, I have managed to get the word-level callbacks from the tts.
the workflow is like this--"speak text" button pressed.It starts the tt
s service and then it sends the text to the flite tts which is in C language and has been combined with the app.
Now, from C code, after every word I make two callbacks to two different java activities:
one to the tts service to speak the word
second to my test java activity to highlight the word.
I am successfully getting the word-level callbacks in my test activity but after that I am not able to do any UI work.
Following is the code that I execute when I get the callback:
this is the function that gets called from the C code.
private void WordCallback(int isword) {// from
// callback
if (isword == -1) {
Log.d(LOG_TAG, "its not a word");
} else if (isword == -2) {
Log.d(LOG_TAG, "yeah..its the end");
} else {
Log.d(LOG_TAG, "its word no " + isword);
int word = isword;
Log.d(LOG_TAG, "highlightwords");
highlightwords(isword);
if (isword == 4) {
Log.d(LOG_TAG, "in if");
new Thread(new Runnable() {
#Override
public void run() {
Log.d(LOG_TAG, "thread started");
try {
Flitetest.this.runOnUiThread(new Runnable() {
#Override
public void run() {
Log.d(LOG_TAG, "run on ui");
textview.setText("#" + isword);
}
});
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
}
FliteTest is the name of the activity.
the Log prints till "Thread started" but the code inside runonuithread() is never executed and there is no error also.
Also, If write textview.settext("something") without the thread and runonuithread(), it gives the error:
fatal signal 11(sigsegv) at 0x6fc64e87(code=1), thread 20292(SynthThread).
What is the reason for that behaviour?

On Android ICS (4.0+), if you have android:anyDensity=false in your AndroidManifest.xml, AND you have forced GPU rendering enabled on your ICS phone. This is a problem for native Android apps as well.
Your options are to either not use the android:anyDensity=false options, or to disable forced GPU rendering on the phone. Since you can't really control the latter on other people's phones, the former seems like the best solution. Remember to use the 'dp' suffix on all your dimensions then.

Related

How to automatically capture photos using an android app in every n seconds

I am trying to make an android app that captures photos after every 5 seconds. Currently the technique i'm using, uses the phone's camera app to capture the photo. It requires the user to capture the photo and then to press ok and only after that the control returns back to the android app. I found some codes to do the same without human interaction, but since i'm new to android, i'm not able to understand codes them because most of them are incomplete and divided into multiple activities. Can this be done using just one activity?
EDIT :
The codes mentioned above can be found here
I suggest to you to use this library (here documentation) instead the official Camera Api of Android that could be really tricky to use for beginners
Then your code could be like this
private final Handler handler = new Handler(); //This should be declared before OnCreate
private Runnable photoRunnable; //This also
CameraView camera = findViewById(R.id.camera);
camera.addCameraListener(new CameraListener() {
public void onPictureTaken(PictureResult result) {
//Handle result here!
}
});
photoRunnable = new Runnable() {
#Override
public void run() {
try {
camera.takePicture(); //The result will be in onPictureTaken
}
catch (Exception e) {
e.printStackTrace();
//Handle Exception!
}
finally{
//also call the same runnable to call it at regular interval
handler.postDelayed(this, 10*1000); //10*1000 is your interval (in this case 10 seconds)
}
}
};
//runnable must be execute once
handler.post(photoRunnable);
Remember to manage the lifecycle of handler

how to stop a running thread with button toggle (android app)

I finally got my app working, i just have one issue which i would like to correct.
I have a button which controls a thread that runs a couple function in the background. The functions in the background eventually stop the thread whenever a certain value is reached. What i am having issues doing is pressing that same button again to just stop the thread manually. Currently I can only start the thread and wait for itself to finish. I am able to do other things in the app, so the thread is running on its own, i just want to kill it manually.
public void onMonitorClick(final View view){
if (isBLEEnabled()) {
if (!isDeviceConnected()) {
// do nothing
} else if (monitorvis == 0) {
showMonitor();
DebugLogger.v(TAG, "show monitor");
//monitorStop = 4;
Kill.runThread(); // I want a function here that would kill the
// thread below, or is there something that
// can be modified in runThread()?
// I did try Thread.Iteruppted() without luck
shutdownExecutor();
} else if (monitorvis == 1) {
hideMonitor();
DebugLogger.v(TAG, "hide monitor");
monitorStop = 0;
runThread(); //The running thread that works great on its own
}
}
else {
showBLEDialog();
}
}
private void runThread() {
new Thread() {
int i;
public void run() {
while (monitorStop != 3) { //This is where the thread stops itself
try {
runOnUiThread(new Runnable() {
#Override
public void run() {
((ProximityService.ProximityBinder) getService()).getRssi();
rssilevel = ((ProximityService.ProximityBinder) getService()).getRssiValue();
mRSSI.setText(String.valueOf(rssilevel) + "dB");
detectRange(rssilevel);
}
});
Thread.sleep(750);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
On first look, you could simply set monitorStop = 3, which would cause the thread to eventually stop after it's timeout completes.
The problem with this, is that I presume if you push the button again or your code modifies monitorStop at some point in the future, then the thead you wanted dead, might stay alive. ie: monitorStop will need to stay equal to three for at least 750ms to assure the thread will comlete it's loop and die.
The correct way to do this would be to create your thread as a new class with it's own monitorStop parameter. When you create the thread, you would keep a reference to it and modify the thread's monitorStop parameter. This way the thread would finish without interruption. If you wanted to create a new thread, then this would not affect the old thread from finishing appropriately.

Android - Proper way to listen for variable change, and do something upon change?

The app I'm making requires that a bit of code be executed whenever the value of a particular variable changes from 0 to 1.
The handler example below is the method I'm currently using to do this (I copied it from someone else).
I have a feeling it's not a proper method though because having just three of these handlers in my app causes the UI to be fairly unresponsive, and causes the device (a phone) to become quite hot.
As you can see, I've put 10ms delays in the handlers to try to deal with this.
Isn't there something more like OnClickListener that can listen at all times for a variable value change without putting such stress on the CPU?
I'm pretty new to Java and Android so a simple example would be very much appreciated.
final Handler myHandler1 = new Handler();
new Thread(new Runnable()
{
#Override
public void run()
{
while (true)
{
try
{
Thread.sleep(10);
myHandler1.post(new Runnable()
{
#Override
public void run()
{
if (myVariable == 1)
{
myVariable = 0;
//do stuff
}
}
});
} catch (Exception e) {}
}
}
}).start();
You must set your variable via a setter method. Then, you can be reactive to that change.
public void setMyVariable(int value) {
this.myVariable = value;
if (myVariable == 1) {
doSomethingWhen1();
} else if (myVariable == 0) {
doSomethingWhen0();
}
}
A more elegant way to do that will be an observer pattern, Here you can find more detailed documentation about it.
You must certainly avoid while(true) loops on mobile device, it will drain your battery and you are also blocking the UI thread. That's the reason why your UI is unresponsive and your cellphone it's quite hot.

How to access layout in TTS OnUtteranceCompleted callback?

I am making a simple Android program that runs mnemonics or math exercises for training purposes.
I have a "new exercise" button that changes question and answer. I want to implement a "text to speech" mode, where the software reads the question and the answer. After they are spoken, it should calculate a new question and start again.
I am using OnUtteranceCompleted to determine if the speech has ended. My problem is that I can’t access my "new exercise" button from the callback.
I am using this:
private TextToSpeech.OnUtteranceCompletedListener onUtteranceCompleted = new TextToSpeech.OnUtteranceCompletedListener()
{
#Override
public void onUtteranceCompleted(String utteranceId)
{
if(0 == utteranceId.compareToIgnoreCase(END_OF_SPEECH))
{
Log.i("TTS","Completed");
if (TTSMode == TTS_MODE_ON) {
//Start new
Log.i("TTS","TTS mode is on: start new exercize");
NewExercize();
btnNewEx.performClick();
}
}
}
};
I got the following error:
$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
I am able to generate the new question/answer, but I can’t use the button to change the fields with the new question or answer, wich are used by the TTS engine.
Any ideas?
You need to use runOnUiThread:
context.runOnUiThread( new Runnable() {
public void run() {
btnNewEx.performClick();
}
});
(you can omit "context." if "this" is an Activity or Service subclass)

how do you read text files off the internet in Android 4.0.3

I am a relatively new Android programmer and I was wondering how you could get read text off the internet in 4.0.3. I keep finding code that gives me a Network on Main exception: http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html and was wondering if anyone could provide me some sample code to get around this, for reference I got the code I tried to use here: http://android-er.blogspot.com/2011/04/read-text-file-from-internet-using-java.html. Thanks a lot.
In Honeycomb and Ice Cream Sandwich (i.e. Android 3.0+) , you cannot connect to the internet in the main thread (onCreate(), onPause(), onResume() etc.), and you have to instead start a new thread. The reason why this has changed is because network operations can make the app wait for a long time, and if you're running them in the main thread, the whole application becomes unresponsive. If you try to connect from the main thread, Android will throw a NetworkOnMainThreadException.
To bypass this, you can run networking code from a new thread, and use runOnUiThread() to do things in the main thread, such as update the user interface. Generally, you can do something like:
class MyActivity extends Activity {
public onCreate(Bundle savedInstanceState) {
super.onCreate();
// Create thread
Thread networkThread = new Thread() {
#Override
public void run() {
try {
// this is where your networking code goes
// I'm declaring the variable final to be accessible from runOnUiThread
final String result = someFunctionThatUsesNetwork();
runOnUiThread(new Runnable() {
#Override
public void run() {
// this is where you can update your interface with your results
TextView myLabel = (TextView) findViewById(R.id.myLabel);
myLabel.setText(result);
}
}
} catch (IOException e) {
Log.e("App", "IOException thrown", e);
}
}
}
}
}
You need to complete an HTTP Request. There are a lot of examples available on line. Try here for starts.

Categories

Resources