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.
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.