How to detect if screen is touched in onTouchEvent() - android

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();
}

Related

how to create back button to goes to back activity

hi i have a menu activity that contains 4 buttons when i press button one the second
activity is open and.i added a back button in second activity to come back to activity
one(menu)how would it perform through code can any one help
private ListView lv;
public static ArrayList<String> your_array_list = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
lv = (ListView) findViewById(R.id.listView1);
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, your_array_list );
lv.setAdapter(arrayAdapter);
try {
DisplayM.main();
} catch (Exception e) {
e.printStackTrace();
}
// if (ViewClass.theEnd)
// your_array_list.add(ViewClass.methods);
int lst = 0;
for(int i=0; i<lst; i++)
{
}
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
this is my listview activity
Use below code in your back button
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
YourActivity.this.finish();
}
});
Write below code on your activity :-
#Override
public void onBackPressed()
{
// TODO Auto-generated method stub
super.onBackPressed();
}
or
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
or
#Override
public void onBackPressed()
{
// TODO Auto-generated method stub
// if u want to go specific activity then use this code
Intent intent = new Intent(this, YourMainActivity.class);
startActivity(intent);
finish();
}
In your second Activity on your Back Button onClick simply finish() the current (second/third ...) activity. This is what the default back button also does
in pause button you have to just call finish() method and then after finishing current activity it return to parent activity

Android how call function after amount of time in loop

I am new in Android programming, I used to program microcontroller, now I need a few assistance from you guys.
I want to call two function with delay after each function called repeatedly until the stop button pressed.
btnStart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//I want perform these series of function repeatedly
//until stop button pressed
while(true){
bluetoothDisconnect();
delay(3000);
bluetoothConnect();
delay(3000);
if(status == true){
break;
}
}
}
});
btnStop.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
status = true;
}
});
Big Thanks,
You need to thread the code in btnStart otherwise it will lock the GUI. Sorry to not be more helpful, I write Android apps using Mono (C#), I am unsure how to do it in Java.
Try this:
btnStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
status = false;
Thread t = new Thread(new Runnable() {
#Override
public void run() {
doLoop();
}
});
t.start();
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
status = true;
}
});
where
private void doLoop(){
do {
bluetoothDisconnect();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
bluetoothConnect();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (!status);
}
and status is a global variable, FALSE by default (declared as boolean status = false)
This code works as a simple solution, however I'd suggest you to look at the AsyncTask class:
http://developer.android.com/guide/components/processes-and-threads.html
Create an handler to delay the task .
Create the runnable thread and run your handler like:
Handler mhandler=new Handler();
Runnable mRunnable=new Runnable() {
#Override
public void run() {
bluetoothDisconnect();
btnStart.performClick() ;
}
};
btnStart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//I want perform these series of function repeatedly
//until stop button pressed
while(true){
bluetoothconnect();
mhandler.postDelayed(mRunnable,3*1000);
if(status == true){
break;
}
}
}
});

How do I interrupt a splash screen using onTouchListener or onTouchEvent?

public class Splash extends Activity {
MediaPlayer ourSong;
#Override
protected void onCreate(Bundle Samiloveschicken) {
// TODO Auto-generated method stub
super.onCreate(Samiloveschicken);
setContentView(R.layout.splashscreen);
ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
ourSong.start();
final Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
} catch (InterruptedException e){
e.printStackTrace();
}
finally{
Intent openMainActivity = new Intent(Splash.this, MainActivity.class);
Splash.this.startActivity(openMainActivity);
Splash.this.finish();
overridePendingTransition(R.anim.mainfadein, R.anim.splashfadeout);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
ourSong.release();
super.onPause();
finish();
}
}
I'm a beginner and I've tried onClickListener, and other various things to no avail.
I can't seem to know where to put this method. I was to be able to interrupt the sleep(5000) when the screen is touched.
Maybe you can set a global flag called interruptThread = false;
Then in on the onTouchListener() you can set interruptThread = true;
and before your sleep(5000); do a check
if(interruptThread) {
thread.interrupt();
} else {
sleep(5000);
}
Try this code, this may help you.......
public boolean onTouchEvent(MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_UP) {
ontouch = true;
startActivity(new Intent(your next activity));
finish();
return true;
}
return false;
};

Android Surfaceview thread not response

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().

Activity crashes when paused

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.

Categories

Resources