Reputation: 1
I have a problem with android service, When i close the app the main activity closed and the service is recreated - oncreated method call automatic and onstart also called automatic - and all the state is gone.
This is my activity code
public class ServicesDemo extends Activity implements OnClickListener
{
private static final String TAG = "ServicesDemo";
Button buttonStart, buttonStop;
@Override
public void onCreate(Bundle savedInstanceState)
{
if (savedInstanceState != null)
{
Log.d(TAG, "ServicesDemo:onCreate WITH savedInstanceState)");
}
Log.d(TAG, "ServicesDemo:onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
}
public void onClick(View src)
{
switch (src.getId())
{
case R.id.buttonStart:
Log.d(TAG, "onClick: starting srvice");
startService(new Intent(this, MyService.class));
break;
case R.id.buttonStop:
// Log.d(TAG, "onClick: stopping srvice");
stopService(new Intent(this, MyService.class));
break;
}
}
}
This is the service Code:
public class MyService extends Service
{
private static final String TAG = "ServicesDemo";
private static MyThread t = new MyThread();
static int yy = 90;
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public void onCreate()
{
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
// MockGPSLocationModel.getInstance().Counter++;
// Log.d(TAG, "MockGPSLocationModel.getInstance().Counter :: " +
// MockGPSLocationModel.getInstance().Counter);
// MockGPSLocationModel.getInstance().Counter++;
}
static public class MyThread extends Thread
{
MediaPlayer player;
public Context ctx;
@Override
public void run()
{
try
{
for (int i = 0; i < 100; i++)
{
Log.i(TAG, "lOOP - " + i);
Thread.sleep(2000);
}
player = MediaPlayer.create(ctx, R.raw.braincandy);
player.setLooping(false); // Set looping
player.start();
}
catch (Exception e)
{
Log.e(TAG, e.toString());
}
finally
{
}
}
}
@Override
public void onDestroy()
{
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
// player.stop();
}
@Override
public void onStart(Intent intent, int startid)
{
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
t.ctx = this;
t.start();
// player.start();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0" >
<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
android:name=".ServicesDemo"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyService"
android:enabled="true" />
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
Please help. I stuck with that 3 days.
Thx, Alon
Upvotes: 0
Views: 3024
Reputation: 884
The first problem with your code is that you allow starting the thread in MyServcie
class multiple times. Each time a user presses the "Start" button, the thread start
method is called, which is illegal according to Java API specification:
It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.
You should defend against repeated starting the thread in your Service class for example by using a flag telling whether the thread has been started.
Please also note that Service#onStart method is deprecated since API level 5. Instead, you should use Service#onStartCommand if possible.
From this method, as Sanket noted, you should return START_NOT_STICKY
if you want to execute the code of MyThread#run
method only once. Also you may want to explicitly stop the service in the finally block.
Upvotes: 1
Reputation: 10856
use this method in service class
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
// do your logic here not in onStart().
return START_NOT_STICKY;
}
START_NOT_STICKY and START_STICKY
here nice answer for START_NOT_STICKY and START_STICKY
https://stackoverflow.com/a/9441795/942224
and if you want to stop service when app close than use stopService
in onDestroy
Upvotes: 0
Reputation: 1368
see if this helps - use shared preferences to save some string for the state, and retrieve back when activity reopens.
Upvotes: 0