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,151
  • 12
  • 51
  • 67

3 Answers3

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,828
  • 13
  • 69
  • 129
0

Try

MainActivity.this

instead of getBaseContext()

HpTerm
  • 8,151
  • 12
  • 51
  • 67
Prateek Yadav
  • 932
  • 6
  • 8
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..