1

i have created a simple service just periodically Toasting. But when service is stopped once it creates again by own and again continuously starting. Following is my MainActivity code.

MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Start service using AlarmManager

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 10);

    Intent intent = new Intent(this, TestService.class);

    PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);

    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    int i;
    i=15;
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
            i* 1000, pintent);

    Button startBtn = (Button) findViewById(R.id.startBtn);
    startBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            startService(new Intent(getBaseContext(), TestService.class));
        }
    });

    Button stopBtn = (Button) findViewById(R.id.stopBtn);
    stopBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            stopService(new Intent(getBaseContext(), TestService.class));
        }
    });

}

Following is my service class

Testservice

public class TestService extends Service {

    @Override
        public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub

        Toast.makeText(getApplicationContext(), "Service Created", 1).show();
        Log.i("TestService", "SERVICE START");
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "Service Destroy", 1).show();
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub

        Toast.makeText(getApplicationContext(), "Service Running ", 1).show();

        return super.onStartCommand(intent, flags, startId);
    }
}

That's all my code when button clicked to stop service it once stopped but again created but it's own what's I am doing wrong?? Thanks in advance..

HpTerm
  • 8,041
  • 12
  • 50
  • 67

4 Answers4

0

Try to use START_STICKY :

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub

    Toast.makeText(getApplicationContext(), "Service Running ", 1).show();

    super.onStartCommand(intent, flags, startId);
    return START_STICKY;
}

If its still not working, try returning START_REDELIVER_INTENT instead of START_STICKY.

Check this link for more information :

START_STICKY and START_NOT_STICKY

Community
  • 1
  • 1
Blaze Tama
  • 10,517
  • 11
  • 65
  • 125
0

Try

MainActivity.this

instead of getBaseContext()

HpTerm
  • 8,041
  • 12
  • 50
  • 67
Prateek Yadav
  • 852
  • 6
  • 8
0

Just use the START_STICKY and START_REDELIVER_INTENT

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);

    // START_REDELIVER_INTENT
    return START_STICKY;
}

Definition from Android SDK Documentation.

public static final int START_STICKY

Added in API level 5 Constant to return from onStartCommand(Intent, int, int): if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don't retain this delivered intent. Later the system will try to re-create the service. Because it is in the started state, it will guarantee to call onStartCommand(Intent, int, int) after creating the new service instance; if there are not any pending start commands to be delivered to the service, it will be called with a null intent object, so you must take care to check for this.

This mode makes sense for things that will be explicitly started and stopped to run for arbitrary periods of time, such as a service performing background music playback.

Constant Value: 1 (0x00000001)

public static final int START_REDELIVER_INTENT

Added in API level 5 Constant to return from onStartCommand(Intent, int, int): if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then it will be scheduled for a restart and the last delivered Intent re-delivered to it again via onStartCommand(Intent, int, int). This Intent will remain scheduled for redelivery until the service calls stopSelf(int) with the start ID provided to onStartCommand(Intent, int, int). The service will not receive a onStartCommand(Intent, int, int) call with a null Intent because it will will only be re-started if it is not finished processing all Intents sent to it (and any such pending events will be delivered at the point of restart).

Constant Value: 3 (0x00000003)

samsad
  • 1,336
  • 1
  • 10
  • 15
  • But why my service is again created once it stopped? –  Nov 11 '14 at 07:12
  • i have tried both return command but it stopped once and again created i want to stop it when stopservice() method is called –  Nov 11 '14 at 07:13
  • Because service definition is when task is completed it's automatically call StopSelf() method. That means automatically destroy the service – samsad Nov 11 '14 at 07:15
0

I have fixed my problem as My AlarmManager was periodically sending intent request to my service so I stopped service and also stopped my AlarmManger by alarm.cancel(pintent); Thanks to all of you..