Reputation:
I have a button on which following code is written
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 10);
Intent intent = new Intent(Formact.this, MyService.class);
MyService.pintent = PendingIntent.getService(Formact.this, 0, intent, 0);
MyService.alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
MyService.alarm.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(), 5000, MyService.pintent);
now once the services is created it has to destroy on a specific action but after each time it destroy it starts again. here is my service class
public class MyService extends Service {
public static int counter = 0;
public static PendingIntent pintent;
public static AlarmManager alarm;
Boolean save=false;
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
return new Binder() ;
}
@Override
public void onCreate() {
Toast.makeText(this, "Service was Created", Toast.LENGTH_SHORT).show();
}
@Override
public void onStart(Intent intent, int startId) {
counter++;
Toast.makeText(this, " Service Started" + " " + counter, Toast.LENGTH_SHORT).show();
SaveForm handler = new SaveForm(getApplicationContext());
handler.setobj(getApplicationContext());
handler.setText(Formact.sendform, Formact.listString);
handler.stratConnection();
String m = "";
int val = 0;
try{
Log.e("val",SaveForm.msg);
if(SaveForm.msg!=null)
{
m=SaveForm.msg.substring(SaveForm.msg.length() - 1);
}
val=Integer.parseInt(m);
Log.e("val",m);
if(val>0)
{
Toast toast = Toast.makeText(getApplicationContext(), "Data saved", 100);
toast.show();
save=true;
MyService.this.stopSelf();
// alarm.cancel(pintent);
if(alarm!=null)
{
try{
alarm.cancel(pintent);
}
catch(Exception e)
{
Toast toasdst = Toast.makeText(getApplicationContext(), "Massi", 100);
toasdst.show();
}
alarm=null;
}
}
}
catch(Exception e)
{
Toast toast = Toast.makeText(getApplicationContext(), "Data Not saved", 100);
toast.show();
///responseStr = responseStrr;
}
}
@Override
public void onDestroy() {
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
super.onDestroy();
if(save)
{
try{
stopSelf();
}
catch(Exception e)
{
Toast.makeText(this, "Head Bang", Toast.LENGTH_SHORT).show();
super.onDestroy();
}
}
}
}
I had set the alarm.cancle but it throws exception because alarm is already null
I had also tried this
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
try{
// String parameter = intent.getStringExtra("param_name");
if(save){
stopSelf();
}
}catch(Exception ex){
}
return startId;
}
But nothing works service starts again and again. One more thing if I did not close the application than every thing works perfect the alarm is canceled but when I close the application and expect to run it same it background it start creating again and again.
Help please.
Upvotes: 4
Views: 277
Reputation: 2926
1.Do not use onStart()
, in docs:
onStart(Intent intent, int startId)
This method was deprecated in API level 5. Implement onStartCommand(Intent, int, int) instead.
Use onStartCommand
instead and return START_NOT_STICKY
or START_STICKY
not startId
.
2. It seems that you are starting a connection handler.stratConnection();
if you are binding anything to it using bindService()
, then you need to unbind it from the service using unbindService(mConnection)
. As given in docs:
Disconnect from an application service. You will no longer receive calls as the service is restarted, and the service is now allowed to stop at any time.
If you are not binding anything then return null
in onBind()
.
@Override
public IBinder onBind(Intent intent) {
return null;
}
3. Use super.onDestroy()
like this:
@Override
public void onDestroy() {
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
super.onDestroy();
/***** No need to put this as it is already going to be destroyed
if(save)
{
try{
stopSelf();
}
catch(Exception e)
{
Toast.makeText(this, "Head Bang", Toast.LENGTH_SHORT).show();
super.onDestroy();
}
}
}
*****/
}
4. Cancel the alarm by alarm.cancel(pintent)
before calling stopSelf()
.
if(alarm!=null)
{
try{
alarm.cancel(pintent);
}
catch(Exception e)
{
Toast toasdst = Toast.makeText(getApplicationContext(), "Massi", 100);
toasdst.show();
}
alarm=null;
}
MyService.this.stopSelf();
5. You are using alarm
and pintent
without initializing it. It is not initialized in your code.
6. Stop service in your activity by using context.stopService(intent)
, using the same intent as Intent intent = new Intent(Formact.this, MyService.class);
.
Upvotes: 2
Reputation: 5207
As you are storing the data to the server by using
handler.setText(Formact.sendform, Formact.listString);
Simply once the data is saved make
Formact.sendform=null
Formact.listString=null
when these values are null the application will throw a exception catch it in a catch block and use this line to close
MyService.alarm.cancel(MyService.pintent);
MyService.this.stopService();
service will not start again.
Upvotes: -1