private void cameraOn() {
// TODO Auto-generated method stub
camera = Camera.open();
parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
}
private void cameraOff() {
// TODO Auto-generated method stub
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.release();
camera = null;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent finis = new Intent (v.getContext(), MainActivity.class);
startActivityForResult(finis, 0);
}
I have these two methods and the button, and I want both methods work in a loop every second each, untill I press the botton for break the loop and go to another screen. These methods are for switch on and off the flash. So I want the flash blinking all the time until I press the button. I tried with threads but Im not able to do every method work just for one second, so I hope someone can help me. Thanks!!
I'm sure there's some kind if timer object you could use, but I would do something like:
long lastTime = 0;
boolean torchIsOn = false;
long currentTime = System.currentTimeMillis();
if (currentTime - lastTime >= 1000) {
if (torchIsOn) {
cameraOff();
torchIsOn = false;
} else {
cameraOn();
torchIsOn = true;
}
lastTime = currentTime;
}
That's just from the top of my head, so hopefully that works. ;)
Related
Hello i am trying to create splash screen in android with one image but i want to display multiple images at a time in splash screen when app is launch.
but it only displays one image please any solution for this please help me.
Here is my code.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<5;i++)
{
progress +=50;
h.post(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
pgr.setProgress(progress);
if(progress==pgr.getMax())
{
//pgr.setVisibility(4);
Intent in= new Intent(getApplicationContext(),Home.class);
startActivity(in);
}
}
});
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO: handle exception
}
}
}
}).start();
}
}
The simplest solution would be the use of CountDown timer,
like,
below code would change activity after 10 sec and also change image after every 1 sec (change it according to your login). int variable i to keep track of image changing number.
int i = 0;
new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
i++;
if(i == 1){
imageview.setImageResource(R.drawable.image1);
}
else if(i == 2){
imageview.setImageResource(R.drawable.image2);
}
else if(i == 3){
imageview.setImageResource(R.drawable.image3);
}
//and so on..........................
}
public void onFinish() {
//finish your splash screen activity
SplashActivity.this.finish();
}
}.start();
How can I compute the time taken by a function or request using threads? In more details, calling start function before the function to be measured and calling end function after it. One more thing, if I want to measure the time taken by multiple functions how can I do that? How to link end function with its corresponding start function. Any help will be much appreciated.
Can I write something like this?
public void start() {
long start = SystemClock.uptimeMillis();
thrd = new Thread( new Task(start) );
thrd.start();
}
public void end() {
thrd.interrupt();
}
class Task implements Runnable{
long start;
Task(long start)
{
this.start = start;
}
#Override
public void run() {
// TODO Auto-generated method stub
if( Thread.currentThread().isInterrupted() )
{
myHandler.post(new Runnable(){
#Override
public void run() {
// TODO Auto-generated method stub
long end = SystemClock.uptimeMillis();
long elapsed = end - start;
}
});//post
}//if
}//run
}//Runnable
before your function starts:
long startTime = System.currentTimeMillis();
after your function ended:
long difference = System.currentTimeMillis() - startTime;
difference / 1000
will give you the difference in seconds. Hope this helps.
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();
}
My code consists of 2 countdown timers, one after finishing says GO! then the other one starts, however the second timer starts immediately, and the GO! message disappears immediately, how to delay the GO! message or the appearing of the second timer? Here's the code.
public class WorkoutGymEasy1 extends Activity {
CountDownTimer cdt = null;
MediaPlayer mp = null;
TextView c;
Button b;
ImageView image;
TextView pushuptext;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.workout_gym_easy1);
final TextView c = (TextView) findViewById(R.id.timer_gym_easy1);
c.setAlpha(0f);
final Button b = (Button) findViewById(R.id.button1);
b.setAlpha(1f);
final ImageView image = (ImageView) findViewById(R.id.imagePushUp);
b.setAlpha(1f);
final TextView pushuptext = (TextView) findViewById(R.id.textPushUp);
b.setAlpha(1f);
RelativeLayout rl5 = (RelativeLayout) findViewById(R.id.rl5);
rl5.setBackgroundColor(Color.BLACK);
mp = MediaPlayer.create(WorkoutGymEasy1.this, R.raw.countdown);
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
b.setOnClickListener(new OnClickListener() {
public void onClick (View v) {
b.setAlpha(0f);
c.setAlpha(1f);
mp.start();
new CountDownTimer(6000, 1000) { //20 seconds count down with 1s interval (1000 ms) //access TextView element on the screen to set the timer value
public void onTick(long millisUntilFinished) { // Code in this method is executed every 1000ms (1s)
c.setText("" + (millisUntilFinished / 1000) + ""); //update the timer
//if (millisUntilFinished == 0) {
//c.setText("GO!");
//}
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
c.setText("GO!");
// Intent myIntent = new Intent(getApplicationContext(), WorkoutGymEasy2.class);
// startActivity(myIntent);
new CountDownTimer(31000, 1000) { //30 seconds count down with 1s interval (1000 ms) //access TextView element on the screen to set the timer value
public void onTick(long millisUntilFinished) { // Code in this method is executed every 1000ms (1s)
c.setText("" + (millisUntilFinished / 1000) + ""); //update the timer
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
Intent myIntent = new Intent(getApplicationContext(), WorkoutGymEasy2.class);
startActivity(myIntent);
finish(); // call finish() method to destroy this activity and return to instructions screen
}
}.start();
}
}.start();
}
});
}
protected void OnPause() {
super.onPause();
mp.release();
finish();
}
}
You would need 3 timers to simulate a delay. So the first timer would start and finish. Starting the "Go" timer. Then when the "Go" timer is finish, the second official timer would go off.
When I press this button:
case R.id.btlento:
Timer timers = new Timer();
timers.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
cameraOn();
},deloy1,poriod1);
Timer timers2 = new Timer();
timers2.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
cameraOff();
}
},deloy,poriod);
The methods cameraOn and cameraOff are:
private void cameraOff() {
// TODO Auto-generated method stub
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.release();
camera = null;
}
private void cameraOn() {
// TODO Auto-generated method stub
camera = Camera.open();
parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
}
and the variables are:
int deloy = 2000;
int poriod = 2000;
int deloy1 = 1000;
int poriod1 = 2000;
All this is to make the camera flash blink, but I don't know how break the loop, because never stops.
How can i stop the loop?
Use a flag,
// member variables
private boolean cameraIsFlashing = false;
private Timer onTimer;
private Timer offTimer;
// constructor & other stuff
// button click method
case R.id.btlento:
toggleCameraFlash();
break;
private void toggleCameraFlash(){
if(cameraIsFlashing){
stopFlashing();
} else {
startFlashing();
}
}
private void startFlashing(){
cameraIsFlashing = true;
// Start your timers
onTimer = new Timer();
onTimer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
cameraOn();
},deloy1,poriod1);
}
private void stopFlashing(){
cameraIsFlashing = false;
// Cancel / Stop your timers
onTimer.cancel;
}
call cancel() and purge() on thread timer to stop the thread.
as shown below.
private void cameraOff() {
// TODO Auto-generated method stub
if(timer != null){
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.release();
camera = null;
timer.cancel();// cancel and releases
timer.purge();
}
}
Make a variable for timer task instead of using anonymous class. First cancel timer task then cancel timer.