Search code examples
androidandroid-intentbroadcastreceiverandroid-servicealarmmanager

How to cancel an alarm in service from an activity?


I have a brodcastReciver , an activity and a service , in the activity i send an intent to service to set an alarm , and alarm is sending broadcast to brodcastReciver to show toast. but i cant cancel alarm from activiy. here is my code :

public class MainActivity extends Activity {

Button start, stop;

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

    start = (Button) findViewById(R.id.button1);
    stop = (Button) findViewById(R.id.button2);

    start.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            start();
        }
    });

    stop.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            stop();
        }
    });

}

void stop() {
    Intent i = new Intent(this, Reci.class);
    i.putExtra("c", "cancel");
    startService(i);
    Toast.makeText(this, "stop clicked!", Toast.LENGTH_SHORT).show();
}

void start() {
    Intent i = new Intent(this, Serv.class);
    i.putExtra("d", "do");
    startService(i);
    Toast.makeText(this, "start clicked!", Toast.LENGTH_SHORT).show();

}

 }

service :

public class Serv extends Service {

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

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

    if (intent.getStringExtra("d").equalsIgnoreCase("do")) {
        Toast.makeText(this, "onStart", Toast.LENGTH_SHORT).show();

        Intent i = new Intent(this, Reci.class)
                .setAction("ir.test.code.Reci.noti");
        PendingIntent pi = PendingIntent.getBroadcast(this, 1234, i, 0);
        G.AM.setRepeating(AlarmManager.RTC_WAKEUP,
                System.currentTimeMillis() + 5000, 5000, pi);

    } else if (intent.getStringExtra("c").equalsIgnoreCase("cancel")) {

        Toast.makeText(this, "onCancel", Toast.LENGTH_SHORT).show();
        Intent i = new Intent(this, Reci.class)
                .setAction("ir.test.code.Reci.noti");
        PendingIntent pi = PendingIntent.getBroadcast(this, 1234, i, 0);
        G.AM.cancel(pi);
    }

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

broadcastReciver

public class Reci extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equalsIgnoreCase("ir.test.code.Reci.noti")) {
        Toast.makeText(context, "ir.test.code.Reci.noti",
                Toast.LENGTH_SHORT).show();
    }

}

}

and Application class :

public class G extends Application{

public static AlarmManager AM;

@Override
public void onCreate() {
    super.onCreate();
    AM = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
}

}

what should i do?


Solution

  • you are not starting service because of diffent class name you provided ...change void stop() method

    Intent i = new Intent(this, Reci.class);

    to

    Intent i = new Intent(this, Serv.class);
    

    in stop() method of mainactivity