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.
Related
I wants to create a custome Input Method with word suggestions from a webservice in an asynchronous way. If it is not asysnchronouse , phone get stuck while connecting to internet. If I use Thread it cause an excpetion "ui can be touch only by the tread created ". I don't know runOnUIthread can be used or how. I understood that runOnUiThread activity method. Anybody please help. I used android Example app softkeybord.
I'm not sure I understand,
If the definition of the Thread is inside the Activity,
You can just call:
new Thread() {
public void run() {
while (i++ < 1000) {
try {
runOnUiThread(new Runnable() {
#Override
public void run() {
btn.setText("#" + i);
}
});
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
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 want when my app exit, after exit if there is no action perform on screen for 5 minutes a video will play every time, this video id define in my app.
Any help would be appreciated. Thankx in advance.
I have the following class but it is not worked fine, how can make the same code as Services ?
public class IdlePhoneState extends Activity {
Handler hl_timeout = new Handler();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
try{
hl_timeout.postDelayed(DoOnTimeOut, 120000); // 2 min
}catch(Exception e)
{
e.printStackTrace();
}
}
// Toast
Thread DoOnTimeOut = new Thread() {
public void run() {
try{
Toast.makeText(getApplicationContext(), "System is idle", Toast.LENGTH_LONG).show();
}catch(Exception e)
{
e.printStackTrace();
}
}
};
#Override
public void onUserInteraction()
{
super.onUserInteraction();
//Remove any previous callback
try{
hl_timeout.removeCallbacks(DoOnTimeOut);
hl_timeout.postDelayed(DoOnTimeOut, 120000);
System.out.println("ggggggggggggggggggggg");
Intent intr= new Intent(getApplicationContext(), VideoPlayerActivity.class);
startActivity(intr);
}catch(Exception e)
{
e.printStackTrace();
}
}
}
Do like below.
you should set an id on the outer-most element of your layout:
android:id="#+id/entire_view"
In java file find it like below.
View view = getViewById(R.id.entire_view);
Write touchlistener code for that root layout.
view.setOnTouchListener( ...
in the touch save the touched time to shared prefereces are something like that. Then compare the saved time with current time. if the difference exceeds five mins then play the video.
For time duration checking try to use Alarm Manager else use CountDownTimer.
so I am building an experiment app where the background will change colour at random intervals.
I am stuck on the background change.
I have working code that changes the background colour, but when I put it in a thread/ try and catch bracket, the application is forced to close and doesnt give me an error?
Here is the code that works when used in the oncreate method:
View view = this.getWindow().getDecorView();
view.setBackgroundColor(Color.RED);
But when I want to make it "sleep" for 1 second and then change it to red, it bombs out.
Please note that this method is a separate method from the oncreate and is called from within there and will not work for some reason?
public void changeBackground(final View v){
Thread timer = new Thread(){
public void run(){
try{
sleep(1000);
}catch (InterruptedException e) {
e.printStackTrace();
}finally{
v.setBackgroundColor(Color.RED);
}
}
};
timer.start();
}
What am I doing wrong?
What I need:
when the app starts, it must wait for 1 second and then change the background colour without it bombing out.
Thanks in advance!
You cannot access UI thread from your custom thread. You have to run your runnable in UI thread. Change your changeBackground method to the following,
public void changeBackground(final View v) {
Thread timer = new Thread() {
public void run() {
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
v.setBackgroundColor(Color.RED);
}
}
};
this.runOnUiThread(timer);
}
Or you can use Asynctask, this handles that issue for you. Here, and here.
Manipulate view state on UI thread:
v.post(new Runnable() {
#Override
public void run() {
v.setBackgroundColor(Color.RED);
}
});
More about it: http://developer.android.com/guide/components/processes-and-threads.html
UI operations can't be done in a Thread
v.setBackgroundColor(Color.RED);
Remove above line from finally and Use a runOnUIthread() to update UI in Finally.
and your final code will look like this
public void changeBackground(final View v){
Thread timer = new Thread(){
public void run(){
try{
sleep(1000);
}catch (InterruptedException e) {
e.printStackTrace();
}finally{
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
v.setBackgroundColor(Color.RED);
}
});
}
}
};
timer.start();
}
Solutions to fix this issue:-
Update you App
Clear Gmail Storage Data
Clear App Cache and Data
Reset App Preferences
Reset Smartphone Factory Settings
I found these steps with the help of YouTube.
Here is that link:-
youtube.com/watch?v=fx8Fv8RXag8
It's my first question on SO, I hope this question won't be bad.
I have a service, it starts working when user launchs an app and works until user will kill it via task killer or turn off his device.
This service has a background thread which does some work with data. I need to bind activities (from activities, not by service) and sometimes (1-2 times per 30 seconds) send data to binded activities.
Structure of my service:
public class myserv extends Service {
public static boolean started=false;
public class workwithdata extends Thread {
#Override
public synchronized void start() {
super.start();
//.. Not important.
}
#Override
public void run() {
if (running) return;
while (true) {
if(condition) mythread.sleep(30000);
else {
Object data = recieveMyData();
if (!data.isEmpty()) {
//.. Some work with recieved data, not important.
sendDataToBindedActivities(data); //This is what I need.
}
mythread.sleep(10000);
}
}
}
}
#Override
public void onCreate() {
super.onCreate();
this.started=true;
mythread = new workwithdata();
mythread.start();
}
}
Well, I found one question but my problem has a little differences: I don't need to send any data to the service, I need just send some data to all binded activities (which service doesn't know at all).
Structure for which I'm looking for:
public class myact extends Activity {
#Override
public void onCreate(Bundle bun) {
super.onCreate(bun);
if(!myserv.started) {
Intent service = new Intent(getApplicationContext(), myserv.class);
getApplicationContext().startService(service);
}
bindToService(this);
}
#Override
public void onRecievedData(Object data) {
//work with recieved data from service "myserv".
}
}
I also tried to find some solutions in android documentation but I didn't find what I need.
So, main question is: is it possible to work with communications from service to activities?. If no: What should I use for this purpose? If yes, just, sorry, can I ask for some code or class names, because I tried to find and didn't...
Thank you.
You need to use a RemoteCallbackList
When your clients bind to the service, you will need to register them using RemoteCallbackList.register().
When you want to send data to the bound clients, you do something like this:
int count = callbackList.beginBroadcast();
for (int i = 0; i < count; i++) {
try {
IMyServiceCallback client = callbackList.getBroadcastItem(i);
client.onRecievedData(theData); // Here you callback the bound client's method
// onRecievedData() and pass "theData" back
} catch (RemoteException e) {
// We can safely ignore this exception. The RemoteCallbackList will take care
// of removing the dead object for us.
} catch (Exception e) {
// Not much we can do here except log it
Log.e("while calling back remote client", e);
}
}
callbackList.finishBroadcast();
An example can be found here It is kinda complicated, but maybe you don't need everything this offers. In any case, have a look.