Image capture in Android automatically - android

Is there any way to use the following code to take pictures automatically i.e. no button clicks at all. Just after sometime image can be taken automatically and stored on the SD card.
protected void startCameraActivity() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(file_name)));
startActivityForResult(intent, 1);
finish();
}

No, once you start an Intent you really have no control over the Activity you start (assuming it's not one that you wrote yourself). In your case, you have to make your own Activity and use the Camera API.
Check out this tutorial:
http://marakana.com/forums/android/examples/39.html

You can use Timer & TimerTask Class together for your requirement. Just study the following code and modify it according to your usage.
import java.util.Timer;
import java.util.TimerTask;
class MyTimerTask extends TimerTask
{
public void run()
{
// Put your camera capturing and photo saving code here
}
}
public class MainClass
{
public static void main(String args[])
{
MyTimerTask myTask = new MyTimerTask();
Timer myTimer = new Timer();
/*
* Set an initial delay of 15 second, then repeat every 10 second.
*/
myTimer.schedule(myTask, 15000, 1000);
}
}

Related

Call an async task repeatedly (lets say every 10 seconds) using tasktimer

My requirement is to call an async task repeatedly every 10 seconds so that the webservice will fetch data an do the update (updating of a map and image) accordingly.
I searched throughout and found out that one can use a tasktimer for this. The problem I am facing is that a parameter is passed into my asynctask. But I am not able to pass that parameter into the tasktimer.
I found out that for this a separate class should be created which extends timer task. But I have no idea how to get that done according to my need.
Please be kind enough to help me. The code for passing a parameter to the async task is given below.
new AsyncLoadGpsDetails().execute(userName);
I want to repeatedly perform the async task. PLease help me, I don't know how to create the class which extends tasktimer as I'm a newbie to this.
Thanks & Regards in advance
You can use either use scheduleAtFixedRate or scheduleWithFixedDelay...
scheduleAtFixedRate
// this task for specified time it will run Repeat
repeatTask.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
// Here do something
// This task will run every 10 sec repeat
}
}, 0, 10);
scheduleWithFixeDelay
scheduler.scheduleWithFixedDelay(new TimerTask() {
#Override
public void run() {
// Here do something
// This task will run every 10 sec Delay
}
},, 0, 10, TimeUnit.SECONDS);
Find the difference between them Here
Here is an example of a Timer and TimerTask:
private Timer mTimer;
private TimerTask mTimerTask;
private void launchTimerTask() {
mTimer = new Timer();
mTimerTask = new TimerTask() {
#Override
public void run() {
// Perform your recurring method calls in here.
new AsyncLoadGpsDetails().execute(userName);
}
};
mTimer.schedule(mTimerTask, // Task to be executed multiple times
0, // How long to delay in Milliseconds
10000); // How long between iterations in Milliseconds
}
private void finishTimerTask() {
if (mTimerTask != null) {
mTimerTask.cancel();
}
if (mTimer != null) {
mTimer.purge();
mTimer.cancel();
}
}
For the TimerTask you will need the following imports:
import java.util.Timer;
import java.util.TimerTask;
If possible, I would use a ScheduledExecutor (Java Timer vs ExecutorService?). There are many examples around, but here is a quick snippet:
private ScheduledExecutorService mService;
private ScheduledFuture mFuture;
private void launchScheduledExecutor() {
mService = Executors.newSingleThreadScheduledExecutor();
mFuture = mService.scheduleAtFixedRate(new Runnable() {
#Override
public void run() {
// Perform your recurring method calls in here.
new AsyncLoadGpsDetails().execute(userName);
}
},
0, // How long to delay the start
10, // How long between executions
TimeUnit.SECONDS); // The time unit used
}
private void finishScheduledExecutor() {
if (mFuture != null) {
mFuture.cancel(true);
}
if (mService != null) {
mService.shutdown();
}
}
Be sure to shutdown the ExecutorService when you're done.
For the above snippet you'll need the following imports:
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
You can call launch anywhere (an onClickListener or onCreate perhaps). Just be sure to call 'finish' at some point or they will run indefinitely (in the onDestroy for example)

Taking a picture with 2 secs of delay

Is there a way to take a picture two seconds after the Camera.takePicture method is invoked? For some reason, I do not want to use handler/timer to schedule the invocation of takePicture.
Precisely, I would like to use a different solution than the following one:
final Handler handler = new Handler();
Timer t = new Timer();
t.schedule(new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
<here takePicture is invoked>
}
});
}
}, 2000);
You could use AlarmManager with a PendingIntent and handle taking the camera capture in your Activity.onNewIntent method, but it is a very confusing solution for what you are trying to solve (a much better use of AlarmManager is to schedule tasks so that they are performed even if the user exits your application). This solution also requires more code and is less precise/reliable and less efficient than using a Handler.
EDIT: You can also use a ScheduledThreadPoolExecutor along with a Runnable.
(Personal opinion following) If you are exploring the APIs available in Android for performing timed tasks, that is ok, but I wouldn't use AlarmManager to schedule timed tasks within an Activity that's already running.
If you just want to have the code a bit more organised, you can make an inner class that implements Runnable and schedule your action like this:
class MyCameraActivity extends Activity
{
class TakePictureTask implements Runnable
{
public void run()
{
MyCameraActivity.this.takePicture();
}
}
void scheduleCameraShot()
{
(new Handler(this.getMainLooper())).postDelayed(new TakePictureTask(), 2000);
}
}

Splash screen android phonegap timing

I found in this answer this code:
super.onCreate(savedInstanceState);
super.setIntegerProperty("splashscreen", R.drawable.splash);
super.loadUrl("file:///android_asset/www/index.html",5000);
And it works, but like this, result is:
Splash screen for 5 seconds
Black screen until the app is ready
index.html when app is ready
So I was wondering if there is any chance of running this
super.loadUrl("file:///android_asset/www/index.html");
As a callback of some ready function, is there a way?
-EDIT-
Changing it to 10 seconds doesn't show me the black screen but I would like to show index.html the exact same moment that the app is ready (not sooner, not much later :D)
// Show LOGO ,start to MainActivity that watting for some seconds
new Handler().postDelayed(new Runnable() {
public void run() {
/*
* Create an Intent that will start the Main WordPress
* Activity.
*/
//
RedirectMainActivity();
}
}, 4000);
Android does not provide any of native API to deal with Splash Screen
but you can use Handler to show fake splash screen.
//load the splash screen
super.loadUrl("file:///android_asset/www/someSplashScreen.html");
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// splash screen successfully timeout
//start new activity or load html layout
super.loadUrl("file:///android_asset/www/index.html");
}
}, 4000);//timeout after 4 sec
Have you alredy tried this?
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class Splash extends Activity {
private final int SPLASH_DISPLAY_LENGHT = 1000;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splashscreen);
/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(Splash.this,Menu.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, SPLASH_DISPLAY_LENGHT);
}
}
In your link to a previous question there is a further link to a Blog
It claims, that with version 1.8.0 of PhoneGap you can call navigator.splashscreen.hide();
Check the Blog (read thru all of it as it is a bit missleading on the first two paragraphs).

Android countup timer for all activities

I'm making an application contains about 20 activities and I want to start a count up timer when the activity 1 starts and finish counting on the last activity. I've found a way to make a subclass and call the timer in 1 activity But I didn't know how to pass the value of the timer from activity 1 to activity 2 and from 2 to 3 . this is my code
subclass
package com.mytimer;
import java.lang.ref.WeakReference;
import java.util.TimerTask;
import android.os.Handler;
import android.widget.TextView;
public class IncrementTask extends TimerTask {
WeakReference<TextView> mRef;
int counter = 0;
Handler handler = new Handler();
public IncrementTask(TextView text) {
mRef = new WeakReference<TextView>(text);
}
public void run() {
handler.post(new Runnable() {
public void run() {
mRef.get().setText("counter " + counter);
counter++;
}
});
}
}
in my activity 1
TextView mTextView = (TextView)findViewById(R.id.text);
Timer timer = new Timer();
IncrementTask task = new IncrementTask(mTextView);
timer.scheduleAtFixedRate(task, 0, 1000);
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent i = new Intent (MainActivity.this, Page2.class);
startActivity(i);
}
});
I want to know how to pass the value of the timer to the next activity not start the timer from 0
any help please
In my opinion use Fragments instead of Activity because if you use fragments it is able to show two fragment on screen which contains timer and your remaining content.
If you use activity it is not possible to show accurate timer values while you move from once screen to other screen.
How do you start the subsequent activities? I suppose with startActivity() or startActivivtForResult() , if this is the case you cold pass the time in the intent.
But it is not a good solution because in the process you would "lose time" because the activity staring is not exact to the second.
Why not create a timer activity that starts at the end of the process and finishes at the end?

How do I set up a Timer.scheduleAtFixedRate() that actually works?

I am using Eclipse for Android. I am trying to make a simple repeating Timer that has a short delay.
It will start after a TextView timerTV is clicked. This code is in the onCreate method:
timerTV = (TextView) findViewById(R.id.timerTV);
timerTV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Timer gameTimer = new Timer();
TimerTask doThis;
int delay = 5000; // delay for 5 sec.
int period = 1000; // repeat every sec.
doThis = new TimerTask() {
public void run() {
Toast.makeText(getApplicationContext(), "timer is running", Toast.LENGTH_SHORT).show();
}
};
gameTimer.scheduleAtFixedRate(doThis, delay, period);
Everytime I try to run it, a "Class File Editor" pops up with the error:
"Source not found"
The JAR file C:\Program Files\Android\android-sdk\platforms\android-8\android.jar has no source attachment.
You can attach the source by clicking Attach Source below:
[Attach Source...]
When I click this, Eclipse asks me to select the location folder containing 'android.jar'
I tried to do this, but cannot navigate all the way to the folder it is located in anyway.
I presume the issue is in my code somewhere.
I have been searching for hours, even copied and pasted code many times.
Using an actual Timer (java.util.Timer) in conjunction with runOnUiThread() is one way to solve this issue, and below is an example of how to implement it.
public class myActivity extends Activity {
private Timer myTimer;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
#Override
public void run() {
TimerMethod();
}
}, 0, 1000);
}
private void TimerMethod()
{
//This method is called directly by the timer
//and runs in the same thread as the timer.
//We call the method that will work with the UI
//through the runOnUiThread method.
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable() {
public void run() {
//This method runs in the same thread as the UI.
//Do something to the UI thread here
}
};
}
SOURCE: http://steve.odyfamily.com/?p=12
Try using Project -> Clean then right click your project and find Fix Project Properties. Check your Build Path. It could be any one of these things. Restart eclipse, make sure your Android Manifest is targeting the correct API, 8 I assume?

Categories

Resources