Android service works again and again

I have a button that says the following code

  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 that services are being created, it must destroy a specific action, but after each destruction, it starts again. here is my class of service

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 set alarm.cancle , but it throws an exception since alarm is already null

I tried this too

    @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, the service starts all over again. One more thing, if I did not close the application, than everything that works fine, the alarm clock is canceled, but when I close the application and expect it to launch it against the same background, it will start creating again and again.

Help me please.

+4
2

 handler.setText(Formact.sendform, Formact.listString);

Formact.sendform=null
 Formact.listString=null

, , catch

    MyService.alarm.cancel(MyService.pintent);
    MyService.this.stopService();

.

-1

1. onStart(), :

onStart(Intent intent, int startId)
This method was deprecated in API level 5. Implement onStartCommand(Intent, int, int) instead.

onStartCommand return START_NOT_STICKY START_STICKY not startId.
2. , handler.stratConnection();, - bindService(), unbindService(mConnection). :

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.

, null onBind().

@Override
public IBinder onBind(Intent intent) {
    return null;
}

3. super.onDestroy() :

@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. alarm.cancel(pintent) 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. alarm pintent . . 6. , context.stopService(intent), , Intent intent = new Intent(Formact.this, MyService.class);.

+2

All Articles