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.
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.
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
I have following code in which Progress Dialog do not shows.
#Override
public void onCompletion(MediaPlayer arg0) {
TrackAnalysis a = null;
File file = new File(songs.get(index).path);
final Track track;
try {
track = echoNest.uploadTrack(file, true);
final ProgressDialog dialog2 = ProgressDialog.show(MainGameActivity.this, "Analyzing...", "Please wait...",
true);
dialog2.setCancelable(true);
new Thread(new Runnable(){
public void run(){
try{
Thread.sleep(3000);
}
catch (Exception e){
e.printStackTrace();
}
dialog2.dismiss();
}
}).start();
track.waitForAnalysis(30000);
a = track.getAnalysis();
Log.i("TUTAJ", a.getTempo().toString() + track.getArtistName() + track.getTitle());
} catch (EchoNestException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
OnCompletion method is from overloaded MediaPlayer.OnCompletionListener.
My question is why progress bar do not shows? When I placed ProgressBar code to onClicked method, then it works fine.
Check if you are calling start on media player before you set onCompletionlistener.
on clicked method does not require media player to start before its set.
You need to run the UI related code on main thread. You could either use runOnUiThread or make use of Handler. There are many examples available in the API Guides section of Android Developers site. Please take a look at Processes and Threads for a start.
I have an android application with QR Code scan. Everything works fine until I installed GMD Hide Bar.
The screen will turn completely black after I successfully scan QR Code. But my app continues to run in black screen since I can hear the sound. I have to click the power button on my device twice (to turn off the screen once and turn it on again), then I can see the screen again.
Since they works individually so I'm not sure what cause this. Here is the code when the QR Code scanner scans a code and quit, for which is the moment when the screen turns black.
private final Handler mUnityHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
try {
Class<UnityPlayer> c = com.unity3d.player.UnityPlayer.class;
Method method = c.getMethod("UnitySendMessage", new Class[] { String.class, String.class, String.class });
method.invoke(null, "CodeScannerBridge", "onScannerMessage", mDataStr); // reveiver null
// because
// UnitySendMessage
// is
// static
} catch (NoSuchMethodException e) {
e.printStackTrace();
return;
} catch (Exception e) {
e.printStackTrace();
return;
}
finish();
}
Any idea?
EDIT It doesnt happen 100% of the time, but often. And I have another function:
mHandler.postDelayed(new Runnable() {
public void run() {
finish();
}
}, 10000);
I tested for this. It will turn black screen sometimes too.
my app wants to update pictures on the web on a regular basis. This doesn't require UI feedback, so I just start a new Thread and let it run. The problem is, that this update-method may be called before the previous one has been finished. How can I make sure that the second call doesn't start a new thread but is queued and automatically started when the previous one finished? Are handlers the right solution here as well?
Here the code:
public static void updatePictureOnPicasa(final PictureEntry pe) {
new Thread() {
public void run() {
try {
if(pe.isUpdated())
picasa.updatePicture(pe.getUrl(), pe.getDescription(), pe
.getTags());
} catch (Exception e) {
Log.e(Prototype.TAG, "Unable to update picture on Picasa "
+ pe.getUrl());
}
}
}.start();
}
You can use a service feature in Android.Please refer these links:
http://www.brighthub.com/mobile/google-android/articles/34861.aspx
http://marakana.com/forums/android/examples/60.html
Hope this will help you.