Camera flash blinking stalls app - java

I want to add a new feature to my app: I created a progress bar and every time the progress changes, the flash will blink with a delay equal to the value of the progress in milliseconds.
When I modify the progress, the flash begins to blink but I can't do anything until it reaches the number of blinks (20). I want to be able to modify the delay even if the flash is still blinking.
private void blink(int sleepMS) throws Exception{
//int sleepMS=(1/10)*50;
int flashCount=20;
getCamera();
camera.setPreviewTexture(new SurfaceTexture(0));
camera.startPreview();
Thread thr = new Thread();
thr.start();
for(int i=0;i<flashCount;i++) {
flipFlash();
thr.sleep(sleepMS);
flipFlash();
thr.sleep(sleepMS);
}
camera.stopPreview();
//camera.release();
}
private void flipFlash(){
if (isFlashOn) {
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
isFlashOn = false;
} else{
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
isFlashOn = true;
}
}
Where getCamera() gets camera parameters. And the code for the SeekBar listener:
volumeControl = (SeekBar) findViewById(R.id.volume_bar);
volumeControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int progressChanged = 0;
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progressChanged = progress;
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
try {
blink(100 - progressChanged);
} catch (Exception e) {
}
}
});

Because your for loop is on you main thread it is blocking all other operations. From you code, it seems like you meant to do something like this:
Thread thr = new Thread(new Runnable() {
#Override
public void run() {
for(int i=0;i<flashCount;i++) {
flipFlash();
thr.sleep(sleepMS);
flipFlash();
thr.sleep(sleepMS);
}
}
});
thr.start();

Related

Pausing and resuming a runnable thread in an activity

I am making a app for training. In my app one video is playing after some second one alert dialog show the dialog contain question and answer student read the question and select the answer until my thread want to wait. Here i successfully get the video timing. I want to do pause the thread until alert dialog close.
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
running = true;
final int duration = mVideoView.getDuration();
new Thread(new Runnable() {
boolean myRunning;
public void run() {
synchronized (mSync) {
myRunning = mSync.running;
}
do {
textView.post(new Runnable() {
public void run() {
int time = Math.round(mVideoView.getCurrentPosition() / 1000);
textView.setText(time + "");
if (time == 5) {
running = false;
mVideoView.pause();
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Page02.this);
alertDialogBuilder.setMessage("Are you sure,You wanted to make decision");
alertDialogBuilder.setPositiveButton("yes",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
onResume();
mVideoView.start();
arg0.dismiss();
}
});
alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
});
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (!running) break;
}
while (mVideoView.getCurrentPosition() < duration);
}
}).start();
}
});
}
#Override
protected void onPause() {
super.onPause();
synchronized (mSync) {
running = false;// you can not set it here because
// it is possible for the thread to read it and exit the loop before he posts your message
mSync.mustBePost = true;
mSync.message = "Main Activity is going to pause";
}
}
#Override
protected void onResume() {
super.onResume();
threadNameCounter++;
synchronized (mSync) {
running = true;
mSync.mustBePost = true;
mSync.message = "Main Activity is going to resume";
}
t = new Thread("My Name is " + String.valueOf(threadNameCounter));
t.start();
}
}

Flashlight with strobe: strobe doesn’t turn OFF

I am trying to create a flashlight app with a strobe effect. My goal is that I can turn ON/OFF the flashlight with a switch Btn and increase the strobe frequency with a seekbar. If the seekbar value is 0 and the switch Btn is ON the flashlight should be ON (without any strobe). But right now it is still blinking (strobe effect ON) if the seekbar value is 0. Do you know how I can change that?
Here is some of the code which I think is important (I really can't find the part which keeps the stobe effect still on:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySwitch = (Switch) findViewById(R.id.mySwitch);
//listener to check changes in state
mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
turnOnOff(isChecked);
}
});
skBar = (SeekBar) findViewById(R.id.seekBar);
skBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
freq = progress;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
#Override
protected void onResume() {
super.onResume();
try {
cam = Camera.open();
camParams = cam.getParameters();
cam.startPreview();
hasCam = true;
}catch(Exception e){
}
}
private void turnOnOff(boolean on){
if(on) {
if(freq != 0){
sr = new StroboRunner();
t = new Thread(sr);
t.start();
return;
}else{
camParams.setFlashMode(Parameters.FLASH_MODE_TORCH);
}
}else if(!on){
if(t != null){
sr.stopRunning = true;
t =null;
return;
}else{
camParams.setFlashMode(Parameters.FLASH_MODE_OFF);
}
}
cam.setParameters(camParams);
cam.startPreview();
}
private class StroboRunner implements Runnable {
boolean stopRunning = false;
#Override
public void run() {
Camera.Parameters paramsOn = cam.getParameters();
Camera.Parameters paramsOff = camParams;
paramsOn.setFlashMode(Parameters.FLASH_MODE_TORCH);
paramsOff.setFlashMode(Parameters.FLASH_MODE_OFF);
try {
while (!stopRunning) {
cam.setParameters(paramsOn);
cam.startPreview();
Thread.sleep(100 - freq);
cam.setParameters(paramsOff);
cam.startPreview();
Thread.sleep(100 - freq);
}
}catch(Exception e){
}
}
}
}

Exit activity after killing all threads android

I am trying to play with progress bars. I have this (below) simple activity which runs a progress bar N times one after the other, when I call Progress(N). It is working great but the problem I am facing is, if I press back button. I get into the mainActivity but the progress bars (the threads) are still running in background one after the other. As soon as they finish N loops, the intent is called and whatever I would be doing would be interrupted by this LOOP_OVER activity.
I tried solving this by my own. I tried using variable of Thread class (before I was directly doing it). And tried to interrupt() it at onDestroy() or even just before the intent is called but its not helping. How should I go about it?
public class Loop extends Activity {
private ProgressBar progressBar;
private CircleProgress circleProgress;
private int progressStatus = 0;
private Handler handler = new Handler();
private TextView myView;
private int started = 0, doneLoop=0;
private Thread th;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loop);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
circleProgress = (CircleProgress) findViewById(R.id.circle_progress);
myView = (TextView) findViewById(R.id.instruction);
progressBar.setScaleY(3f);
// Start long running operation in a background thread
Progress(3);
}
#Override
public void onDestroy() {
// Below, everything I am just
th.interrupt();
Loop.this.finish();
Thread.currentThread().interrupt();
super.onDestroy();
}
public void Progress(final int numberOfRuns){
// QueView.setText(Que);
if(numberOfRuns == 0){
th.interrupt();
Intent myIntent = new Intent(Loop.this, LOOP_OVER.class);
startActivity(myIntent);
super.onDestroy();
finish();
}
th = new Thread(new Runnable() {
public void run() {
genNextSet();
while (progressStatus < 100) {
progressStatus += 1;
// Update the progress bar and display the
//current value in the text view
handler.post(new Runnable() {
public void run() {
circleProgress.setProgress(progressStatus);
progressBar.setProgress(progressStatus);
textView.setText(progressStatus+"/"+progressBar.getMax());
}
});
try {
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
myView.setText(Que);
}
});
// Sleep for 200 milliseconds.
//Just to display the progress slowly
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
progressStatus = 0;
Progress(numberOfRuns - 1);
}
});
th.start();
}
private void genNextSet() {
// so some cool here!
}
}
You can think of a class variable that is shared among all threads.
Try to add something like this:
private Boolean LOOP = true;
then
while (progressStatus < 100 && LOOP) {
and
#Override
public void onBackPressed() {
LOOP = false
}
also
if(LOOP == true){
// call intent
}
finish();
Your activity does not get destroyed, if you press the "Back"-key, thus onDestroy() will not be called.I'd override onBackPressed(), if I where you.Alternatively, you could try to put it into the onPause()-method.
You haven't override the back button pressed..try this
#Override
public void onBackPressed() {
th.interrupt();
Loop.this.finish();
Thread.currentThread().interrupt();
super.onBackPressed();
// add finish() if you want to kill current activity
}

Progress Bar loading according to timer

Am using a timer to record video in my App and it is based on JavaCvRecorder app.
When I press the screen the recording starts and we are able to see the time in leftside of screen.
Now instead of showing the timer I have to show a progress bar to load according to the time can any one help me on this as i don't know where to show the progress bar. The below is code for showing time while recording video. Please help me friends
private void initiateRecording(boolean isActionDown) {
isRecordingStarted = true;
firstTime = System.currentTimeMillis();
recording = true;
totalPauseTime = 0;
pausedTime = 0;
txtTimer.setVisibility(View.VISIBLE);
// Handler to show recoding duration after recording starts
mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.postDelayed(mUpdateTimeTask, 100);
btnRecorderControl.setVisibility(View.VISIBLE);
btnRecorderControl.setText(getResources().getString(R.string.stop));
}
Use AsynkcTask method
private void call_timerprogressbar() { //......call this method first
try {
callprogressbar sessionrunner = new callprogressbar(getActivityTaskHandler);
sessionrunner.execute();
} catch (Exception ex) {
}
}
public class callprogressbar extends AsyncTask<String, String, String> {
Handler statushandler;
public callprogressbar(Handler getActivityTaskHandler) {
statushandler = getActivityTaskHandler;
}
#Override
protected String doInBackground(String... params) {
//........call your timer function
}
#Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setMessage("Please wait... ");
progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressBar.setIndeterminate(true);
progressBar.setCanceledOnTouchOutside(false);
progressBar.setTitle(" ");
progressBar.setCancelable(false);
progressBar.show();
}
#Override
protected void onPostExecute(String result) {
try
{
super.onPostExecute(result);
Message msg = new Message();
msg.obj = result;
statushandler.sendMessage(msg);
progressBar.dismiss();
}catch(Exception ex){
}
}
}
public Handler getActivityTaskHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
progressBar.dismiss();
}
};

setBackgroundResource doesn't set the image

Handler hnd = new Handler() {
#Override
public void handleMessage(Message msg) {
int id = sequence.get(msg.arg1);
if(msg.arg1 % 2 == 0) {
sq.get(id-1).setBackgroundResource(R.drawable.square_show);
} else {
sq.get(id-1).setBackgroundResource(R.drawable.square);
}
}
};
#Override
public void onResume() {
super.onResume();
Thread background = new Thread(new Runnable() {
public void run() {
try {
for(int i = 0; i < sequence.size()-1; i++) {
record_tv.setText(""+i);
Thread.sleep(200);
Message msg = hnd.obtainMessage();
msg.arg1 = i;
msg.sendToTarget();
}
} catch(Throwable t) {
}
}
});
background.start();
}
[CODE UPDATED] now it goes through the first loop and stops
do you have any idea why the code in the first runOnUiThread gets executed but it doesn't do what i want?
what i want is: change the image to "square", wait 2 seconds, change the image to "square_show", wait 2 secs and repeat the loop
i've been struggling for an hour now...
You can easily set image using following code.
sq.get(id-1).setImageResource(R.drawable.square_show);
sq.get(id-1).setImageResource(R.drawable.square);
public void show(int size) {
// CICLE THROUGH EACH SQUARE
for(int i = 0; i <= size-1; i++) {
Thread thrd = new Thread() {
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
sq.get(id-1).setImageResource(R.drawable.square_show);
// System.out.println("1st..........");
try {
sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sq.get(id-1).setImageResource(R.drawable.square);
// System.out.println("2nd..........");
try {
sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
};
thrd.start();
}
}
This is a wrong way to achieve it. This may help you.
http://developer.android.com/guide/topics/graphics/2d-graphics.html#tween-animation
I would suggest you to use a handler
int drawablebkg[] ={R.drawable.ic_launcher,R.drawable.icon};
Handler m_handler;
Runnable m_handlerTask ;
ImageView iv;
iv = (ImageView)findViewById(R.id.imageView1);
m_handler = new Handler();
m_handlerTask = new Runnable()
{
#Override
public void run() {
iv.setImageResource(android.R.color.transparent);
if(i<2)
{
ivsetBackgroundResource(drawablebkg[i]);
i++;
}
else
{
i=0;
}
m_handler.postDelayed(m_handlerTask, 2000);
}
};
m_handlerTask.run();
In onPause() of your activity
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
//_t.cancel();
m_handler.removeCallbacks(m_handlerTask);
}
Another way
iv= (ImageView)findViewById(R.id.imageView1);
AnimationDrawable animation = new AnimationDrawable();
animation.addFrame(getResources().getDrawable(R.drawable.ic_launcher), 2000);
iv.setImageResource(android.R.color.transparent);
animation.addFrame(getResources().getDrawable(R.drawable.icon), 2000);
animation.setOneShot(false);
iv.setBackgroundDrawable(animation);
//set setBackgroundDrawable(animation) is decprecreated i guess. not sure in which api
// start the animation!
animation.start();
Another way
Define background.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="#drawable/ic_launcher" android:duration="2000" />
<item android:drawable="#drawable/icon" android:duration="2000" />
</animation-list>
I your activity onCreate();
ImageView iv = (ImageView)findViewById(R.id.imageView1);
iv.setBackgroundResource(R.drawable.background);
AnimationDrawable animation= (AnimationDrawable)loadingRaven.getBackground();
loadingRaven.setImageResource(android.R.color.transparent);
animation.start();
Note to stop the animation you need to call animation.stop()
Because the resource changes in the UI thread and you are sleeping your background thread. The UI thread is running normally.
Use handlers:
public class MainActivity extends Activity {
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button1);
}
Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
if (msg.arg1 % 2 == 0) {
b.setBackgroundResource(R.drawable.analytic_icon);
} else {
b.setBackgroundResource(R.drawable.ic_launcher);
}
}
};
#Override
public void onResume() {
super.onResume();
Thread background = new Thread(new Runnable() {
public void run() {
try {
for (int i = 0; i < 20; i++) {
Thread.sleep(2000);
Message msg = handler.obtainMessage();
msg.arg1 = i;
msg.sendToTarget();
}
} catch (Throwable t) {
// just end the background thread
}
}
});
background.start();
}
}
Try this,It will work:
public void show(final int size) {
Thread thrd = new Thread(new Runnable() {
#Override
public void run() {
for (int i = 0; i <= size - 1; i++) {
id = (Integer) sequence.get(i);
runOnUiThread(new Runnable() {
#Override
public void run() {
sq.get(id - 1).setBackgroundResource(
R.drawable.square_show);
}
});
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
#Override
public void run() {
sq.get(id - 1).setBackgroundResource(
R.drawable.square);
}
});
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thrd.start();
}

Categories

Resources