0

I am trying to get an application to run 5 times after the user presses the designated button, but it only runs once and prints out my debugging statement (Log.v) five times.

What is the correct format to do this?

This is what I tried:

Button btnStart = (Button) findViewById(R.id.StartService);
btnStart.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)

{   

for (int i = 0; i < 5; i++)
{
Intent intent = new Intent(currentClass.this, different.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startService(intent);
finish();
Log.v(TAG, "testing");
}
}
});

EDIT:

I tried to make the service do my task five times, but after the first time, I get a java.io.IOException: invalid preview surface. when mMediaRecorder.prepare() is called, and startRecording() is called again.

android
  • 3
  • 5

1 Answers1

0

Your service has not yet had the chance to finish when your for() loop runs five times. You need to implement communication between your UI and the service - let the service send you a message when it's done so that you can call it again (read on service-activity communication here).

Alternatively, modify the service to do whatever it does five times. If your data is dynamic each time you want to run the service, you may have to go with the first approach.

Community
  • 1
  • 1
Oleg Vaskevich
  • 12,444
  • 6
  • 63
  • 80
  • Oleg, thank you for your response. I tried to make the service do my task five times, but after the first time, it throws a Null Pointer Exception when I try "mServiceCamera.reconnect();". Am I supposed to add anything to the onStartCommand (such as if intent != null)? I tried to but I am still getting an exception. @Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); if (mRecordingStatus == false) startRecording(); return START_STICKY; } I will re-edit this question. Thank you for your time. – android Jul 17 '11 at 17:03
  • No problem. I'm still relatively new to Android, but I'll try to help. Anyway, it seems `Camera.open()` can throw `RuntimeException`s if the user quits the cameras too often, so perhaps `try` and `catch` the exceptions. Also, I think you may need to release camera resources once you're done with them. Try `mServiceCamera.release()` and see if that helps solve your problem. – Oleg Vaskevich Jul 17 '11 at 19:14
  • Thank you for all you help! This might be a noob question, but I was just curious. Instead of implementing service-activity communication, if the application runs for a preset time, what if I just used handler.postDelay() after each time I call an application to run. Would that be the same thing as giving it a chance to finish before the for loop is completed? Much appreciated! – android Jul 18 '11 at 20:49
  • It could, but what I'd be worried about is the handler being killed and/or lost if the user exits your main application activity. You probably do know that Android can destroy your application, such as on screen orientation or if the phone needs memory (say, the user has to take a phone call). When that happens, your Handler could lose access to its `Context` (or worse, leak it). Would it be possible for you to do the delay within your Service somehow, and run whatever function it is you need? – Oleg Vaskevich Jul 18 '11 at 21:02
  • Actually, better yet, use `AlarmManager` - it's like cron, and you can tell the system to automatically wake up your application or service in a certain amount of time. Let me get a link to a good reference... – Oleg Vaskevich Jul 18 '11 at 21:04
  • Alright, try reading the Task Manager section of the Android for Dummies book - it has a good way of using the Alarm Manager (for example, you create a `BroadcastReceiver` which can receive the alarm events, and also one for when the phone boot completes (alarms are cleared when phone shuts down and you will need to restart them manually)). Hope this helps! http://jacyar.googlecode.com/files/Android.Application.Development.for.For.Dummies.pdf – Oleg Vaskevich Jul 18 '11 at 21:07
  • I apologize for replying so late, but thank you for the reference. Implementing an AlarmManager works perfectly for my application. – android Jul 25 '11 at 18:26