I have this code, it works well if I use lights(); once
but if I use it multiple times like lights();lights();lights();... not so much. when the app run they seems to run all at the same time
public void lights() {
try {if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
cam = Camera.open();
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();}
} catch (Exception e) {e.printStackTrace();}
}
}
mHandler.postDelayed(new Runnable() {public void run() {stop();}
}, 1000);
}
public void stop(){
try {if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH))
{cam.stopPreview();cam.release();cam = null;}} catch (Exception e) {e.printStackTrace();}
}
How can I make the handlers to wait one after another? To wait for the first one to finish. I want to make an alarm but if I use ThreadSleep(); too much my app crash, probably thinks the UI is unresponsive
Related
i want to capture a picture periodically for every minute. without user interaction.something like capture the image in service. any help will be appreciated.
thanks in advance.
This is just suggestion.
Use alarm manager, Which is help you access the camera in every minute.
I think that you can have a Service that launchs at startup. That service will be part of an Application that has permission to use the camera.
Inside that service you can have something like this:
private class threadPhoto extends Thread {
#Override
public synchronized void start() {
super.start();
}
public void force() {
this.force = true;
}
#Override
public void run() {
super.run();
while (true) {
if (force) {
try {
//Code for taking the photo
}
catch (Exception e) {
Log.e("", "Error");
}
force = false;
}
try {
sleep(TIME_INTERVAL_IN_MILLIS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
And in the Service onCreate you should create the threadPhoto object, and periodically call the force() method that it has.
For learn to take photos in Android you should read this article on Android Developers.
Good day everyone.
I'm working with AsyncTasks and calling publishProgress(); from doInBackground() method. I'm pretty sure there's no error until here.
I need to change an ImageView's source, e.g. in every 3 seconds from onProgressUpdate() method.
If i only do this;
agiz.setImageResource(R.drawable.image1);
initial image successfully changes to image1.
But if i try to do this (which is my goal actually)
imageView.setImageResource(R.drawable.image1);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
imageView.setImageResource(R.drawable.image2);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
imageView.setImageResource(R.drawable.image3);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
imageView WAITS 6 SECONDS, THEN turns to image3, without showing image2.
What am i missing ?
Thanks in advance.
I think you need a postDelayed method. It will be more readable and effective, because this method works in UI thread.
imageView.postDelayed(new Runnable(){
#Override
public void run() {
imageView.setImageResource(R.drawable.image1);
}}
,3000);
imageView.postDelayed(new Runnable(){
#Override
public void run() {
imageView.setImageResource(R.drawable.image2);
}}
,6000);
imageView.postDelayed(new Runnable(){
#Override
public void run() {
imageView.setImageResource(R.drawable.image3);
}}
,9000);
Explantation your problem.
I suppose that you block UI thread. Thread.sleep() method block current thread immediatly without waiting while your imageView was repainted, so you getting a lock result.
I am working on an application that automatically invokes an activity to take a video every few seconds.
The activity is started from a service as follows.
Intent intent1 = new Intent(context,CwacCamActivity.class);
intent1.setAction(GlobalVariables.TAKE_VIDEO_ACTION);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent1.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);//have tried without including this too.
context.startActivity(intent1);
As per the recommendations on the best time to start recording the video,
I start recording the video in
public void autoFocusAvailable()
Here is the code
try {
record();
} catch (Exception e) {
e.printStackTrace();
}
//THread to stop the video after stipulated time ( 5 seconds for example)...
new Thread(new Runnable() {
#Override
public void run() {
//RUnnable to let the record go on for the requested time...
try {
Thread.sleep(5000);
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
try {
stopRecording();
getActivity().finish();
} catch (IOException e) {
e.printStackTrace();
Log.v(GlobalVariables.TAG,"error is"+e.getMessage());
}
}
});
} catch (Exception e) {
Log.v(GlobalVariables.TAG,"error is"+e.getMessage()
}
}
}).start();
When i try the above code by making the activity as MAIN and Launcher, it closes perfectly fine but when running the activity from the Service, it keeps restarting the activity and the whole app crashes in the process.
When taking a picture, it makes sense to finish the activity in the SavePicture().I am not sure if this is the right place to finish the activity or even stopRecording for that matter.However stopRecoring works and the videos are saved as they are supposed to .
I have tried a ton of different things but to no avail.I feel like I am missing something very simple.
Any help is appreciated as I am out of ideas at this point.
I am using this library to establish a connection and data transfer between two android devices. what i want to do is: one phone send the same message every 2 seconds, everytime the receiver reads the message, it starts some work..
I tried doing that with Timers and TimerTasks, but it is not synchronized at all. i get weird different delays.
public void onClick(View v){
new Timer().schedule(new TimerTask()
{
public void run()
{
bt.send("Start");//////////////////////////////////////////////////////
Log.i("time", "data sent "+System.nanoTime());
}
try {
Thread.currentThread();
Thread.sleep(50, 0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
s.playSound();
//
}
}
, 0, 3000);
and also sleep() is not a good way to produce delays, because sometimes its interrupted.
Is there any other way to do that?
I have to change ttyS1 port's baudrate every second. So i need to wake-up remote machine on 9600 Bauds and after communicate with it on 19200 Bauds. But there is a time limit between wake-up signal and real data communication. I use Handler&Thread for this trick.
I done it and seems okay with Handler&Thread. I toggled postdelayed every entrance for 1 milliseconds and 500 milliseconds. But it works bad. Sometimes 1 milliseconds task takes almost 10-15 milliseconds.
Also i noticed that when i add "runOnUiThread" with some UI update, result goes worst like 30milliseconds.
Note: I need to send Wake-up signal everytime not just one time.
Any idea?
public void serial_query(){
try {
Runnable cookimageupdate = new Runnable(){
public void run(){
try {
if (mOutputStream != null) {
mSerialPort.close();
if (mLAP==0) //First LAP is used to HOLTEK Wake-Up. Second one is real data.
{mLAP=1; mSerialPort=new SerialPort(new File("/dev/ttyS1"), 9600, 0); mBufferbuf = (byte)0x00; mOutputStream.write(mBufferbuf);}
else {mLAP=0; mSerialPort=new SerialPort(new File("/dev/ttyS1"), 19200, 0); mOutputStream.write(mBuffer);}
} else {
return;
}
} catch (IOException e) {
e.printStackTrace();
return;
}
try{
runOnUiThread(new Runnable() {
public void run() {
//meat_temp.setText(_meatprobe_temp.toString());
if (_pt1000_status==1) {pt_status.setText(" PT1000 open-circuit");}
else if (_pt1000_status==2){pt_status.setText(" PT1000 short-circuit");}
else if (_pt1000_status==0){pt_status.setText(" -");}
}
});
}
catch (Exception e)
{
e.getLocalizedMessage();
}
if (mLAP==1)
{handler_serial.postDelayed(this, 1);}
else {handler_serial.postDelayed(this, 500);}
}
};
handler_serial.postDelayed(cookimageupdate, 50); //start with 50mSec. delay
}
catch(Exception e){
e.printStackTrace();
return;
}
};
1 millisecond is too short a time delay to post a delayed runnable. Most handlers take more time than that to process your message.
For such low delays you would be better off using
Thread.sleep().