I have a problem with a thread in surfaceview. I can't understand how to onPause/onResume when I lock my phone. Whatever I do, the thread doesn't respond after locking/unlocking the phone.
In the activity:
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
surfaceView.SurfaceView_OnResume();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
surfaceView.SurfaceView_OnPause();
}
In the surfaceview
public void SurfaceView_OnResume() {
if (null != surfaceViewThread) {
surfaceViewThread.setRunning(true);
surfaceViewThread.notify();
}
}
public void MySurfaceView_OnPause() {
surfaceViewThread.setRunning(false);
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
boolean retry = true;
myGameThread.setRunning(false);
while (retry) {
try {
myGameThread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
In the thread:
public void setRunning(boolean run) {
runFlag = run;
}
Thread.setRunning(boolean b) is not in the Android API.
You can check it here. http://developer.android.com/reference/java/lang/Thread.html
And if I need a Thread, I prefer to use Runnable(Interface), not Thread(Object).
To control my Thread cycle, I will design my method:run() like this...
run(){
while(threadRun){
...//What you want to do in the thread.
while(threadPause){
}
}
}
The Boolean:threadRun will turn to false in Activity.onDestroy(), or any other time you really want to shut down the thread.
The Boolean:threadPause ... turn to false in Activity.onPause() and turn to true in Activity.onResume().
Related
I need to understand on how to detect if user has touched the screen.
Expected Result:-Whenever user touches screen it should skip Splash Screen and move to main activity.
Problem:-Whenever user touches the screen Splash Screen is skipped but in the background sleep(10500) in try block keeps running and as it elapses the Main Activity starts again i.e. it opens two times.
What have I done so far:-I tried do while loop and gave a condition,if the condition is satisfied(of Touch) then break.But I don't seem to get the correct working condition.
Splash Screen Code:-
#Override
protected void onCreate(Bundle splashState) {
// TODO Auto-generated method stub
super.onCreate(splashState);
setContentView(R.layout.splash);
ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
ourSong.start();
Thread timer = new Thread() {
public void run() {
do
{
try {
//if(onTouchEvent(null))
// break;
sleep(10500);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
startActivity(new Intent("com.first.MAINACTIVITY"));
}
}while(false);
}
};
timer.start();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
startActivity(new Intent("com.first.MAINACTIVITY"));
finish();
ourSong.release();
}
return super.onTouchEvent(event);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
ourSong.release();
}
If Statement is provided in the try block so if condition is satisfied it would break.But the condition is unknown to me.Need help with the condition.
Thanks.
private boolean isSplashRunning = true;
#Override
protected void onCreate(Bundle splashState) {
// TODO Auto-generated method stub
super.onCreate(splashState);
setContentView(R.layout.splash);
ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
ourSong.start();
Thread timer = new Thread() {
public void run() {
do
{
try {
//if(onTouchEvent(null))
// break;
sleep(10500);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if(isSplashRunning)
startActivity(new Intent("com.first.MAINACTIVITY"));
}
}while(false);
}
};
timer.start();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
isSplashRunning = false; //or in onPause
startActivity(new Intent("com.first.MAINACTIVITY"));
finish();
ourSong.release();
}
return super.onTouchEvent(event);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
isSplashRunning = false;
super.onPause();
finish();
ourSong.release();
}
I was wondering, how can I keep the device listening for voice commands with voice recognition while the device is asleep? The idea I have is that I would like the device to respond to my voice, even if I have the screen locked or the screen has timed out.
Is this possible? I have tried using this as a service and an interface and it stops listening once the screen locks. Can I receive any help with this? This is my class.
public class VoiceEngineService extends Activity {
private boolean isSpeakingDone = false; // default setting
private SpeechRecognizer sr = SpeechRecognizer.createSpeechRecognizer(this);
private AudioManager mAudioManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.wait_for_speech);
// mute beep sound
mAudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
sr.setRecognitionListener(new listener());
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); // LANGUAGE_MODEL_WEB_SEARCH
i.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getApplication()
.getClass().getName());
i.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 6);
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "");
i.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 5500);
sr.startListening(i);
}
class listener implements RecognitionListener {
#Override
public void onReadyForSpeech(Bundle params) {
// TODO Auto-generated method stub
}
#Override
public void onBeginningOfSpeech() {
// TODO Auto-generated method stub
}
#Override
public void onRmsChanged(float rmsdB) {
// TODO Auto-generated method stub
}
#Override
public void onBufferReceived(byte[] buffer) {
// TODO Auto-generated method stub
}
#Override
public void onEndOfSpeech() {
// TODO Auto-generated method stub
}
#Override
public void onError(int error) {
// TODO Auto-generated method stub
if (SharedPref.getVoiceController() == false) {
sr.cancel();
Intent i = new Intent();
sr.startListening(i);
} else {
sr.stopListening();
sr.destroy();
finish();
}
}
#Override
public void onResults(Bundle results) {
// TODO Auto-generated method stub
isSpeakingDone = true;
ArrayList<String> mDataList = results
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
Intent i = new Intent();
i.putStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS, mDataList);
setResult(RESULT_OK, i);
finish();
}
#Override
public void onPartialResults(Bundle partialResults) {
// TODO Auto-generated method stub
}
#Override
public void onEvent(int eventType, Bundle params) {
// TODO Auto-generated method stub
}
} // end listener class
#Override
protected void onPause() {
if ((isSpeakingDone == false)) {
finish();
}
super.onPause();
}
#Override
protected void onStop() {
// when speaking is true finish() has already been called
if (isSpeakingDone == false) {
finish();
}
super.onStop();
}
#Override
protected void onDestroy() {
sr.stopListening();
sr.destroy();
super.onDestroy();
}
}
You have to implement your voice listener in a service. Create a class that extends 'Service' and make up some logic to take care of recording.
If you tried already the service, then it might be that you tried to redirect commands to an activity which most likely has been stopped by Android OS. Generally when talking about doing stuff when phone is in lock mode, you only can hope to accomplish tasks in one or more services coupled togethe.
When you are in Activity, of course wen the activity goes out of scope it will be shut down by Android OS. but services can still run in background unless shut dow mm explicitly by your own code or in rare cases that Android will recognize that it needs memory and processor power for other tasks.
The below code is starting the thread only once, but I want to stop and start thread again by calling below method.
Thread th;
int t=45;
onstartbuttton()
{
th= new Thread(new callmymethod());
th.start();
}
onstopbutton()
{
}
public class callmymethod implements Runnable {
// TODO Auto-generated method stub
#SuppressWarnings("null")
#Override
public void run() {
// TODO Auto-generated method stub
while(t>-1){
try{
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
time_btn.setText(""+t);
if(t==0)
{
Toast.makeText(getApplicationContext(), "Thread over", Toast.LENGTH_SHORT).show();
}
}
});Thread.sleep(1000);
// Log.i("Thread", "In run"+t);
t=t-1;
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
Now I want to stop thread, so what I have to write in onstopbutton() method and how to start again by calling onstartbutton() method.
You need to add a flag to your thread indicating that it should stop running.
You can use an AtomicBoolean:
final AtomicBoolean flag = new AtomicBoolean();
onstartbuttton() {
th= new Thread(new callmymethod(flag));
flag.set(true);
th.start();
}
onstopbutton() {
flag.set(false); // indicate that the thread should stop
}
public class callmymethod implements Runnable {
public AtomicBoolean flag;
public callmymethod(AtomicBoolean flag) {
this.flag = flag;
}
#Override
public void run() {
int t = 45; // start back from 45
while(t>-1 && flag.get()){
// do as before
}
}
}
Have been looking on some tutorials for drawing canvas using SurfaceView, but the only thing that shows up is a black background.
public class FighterActivity extends Activity implements OnTouchListener {
/** Called when the activity is first created. */
SurfaceController surface;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
surface = new SurfaceController(this);
surface.setOnTouchListener(this);
setContentView(surface);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
surface.pause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
surface.resume();
}
public class SurfaceController extends SurfaceView implements Runnable{
Thread thread = null;
SurfaceHolder holder;
public SurfaceController(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder = getHolder();
System.out.println("HERE");
}
public void run() {
// TODO Auto-generated method stub
System.out.println("Hello World2");
while(true){
if(!holder.getSurface().isValid()){
System.out.println("NOT VALID");
continue;
}
System.out.println("VALID!");
Canvas can = holder.lockCanvas();
can.drawARGB(255, 150, 150, 0);
holder.unlockCanvasAndPost(can);
}
}
public void pause(){
}
public void resume(){
}
}
public boolean onTouch(View view, MotionEvent me) {
// TODO Auto-generated method stub
return false;
}
}
It gets to the System.out.println("HERE"); and prints out HERE, but nothing more happends, In other words the thread does not get started since "Hello World2" is not printed, what is the problem?
Thanks for any help
I'm assuming you're building off of this: http://android-coding.blogspot.ca/2011/05/drawing-on-surfaceview.html
You'll notice there the onResumeMySurfaceView and onPauseMySurfaceView (resume and pause in your SurfaceController, respectively) start the actual thread. You'll need to do that in your code, too, e.g. in SurfaceController:
protected boolean running = false;
public void resume() {
running = true;
thread = new Thread(this);
thread.start();
}
I have an activity that keeps crashing onPause. Theres a looped thread that draws on a canvas. And When I paused the application or try to move to another activity it crashes.
I have it so it closes out onPause, because I don't want it to post a force close. But I want to be able to go back to the activity
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
boolean retry = true;
_canDraw = false;
while(retry) {
try {
DrawThread.join();
retry = false;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
android.os.Process.killProcess(android.os.Process.myPid());
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
android.os.Process.killProcess(android.os.Process.myPid());
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.face_button:
_canDraw = false;
try {
DrawThread.join();
whack.this.startActivity(selectFace);
} catch (InterruptedException e) {
e.printStackTrace();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
android.os.Process.killProcess(android.os.Process.myPid());
}
You're wondering why it crashes onPause? You're crashing it.