Abdalrhman Elsayed Abdalrhman Elsayed - 4 years ago 127
Android Question

How to use 2 ToggleButton for different 2 Different Alarms

i am using 2 toggleButtons for to different alarms as shown in the code , when i Activate anyone of these Button , both alarms works at the same time , so how can i solve that as i will use more than 2

public class MainActivity extends AppCompatActivity {

ToggleButton toggleButton , toggleButton1 ;
AlarmManager alarmManager;
PendingIntent pendingIntent;

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

toggleButton = (ToggleButton)findViewById(R.id.togelbutton);
toggleButton1 = (ToggleButton)findViewById(R.id.togelbutton1);

toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){

Intent intent = new Intent(getApplicationContext(),AlarmReceiver.class);

pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(), 1000*10,pendingIntent);
}
else{
alarmManager.cancel(pendingIntent);
Toast.makeText(getApplicationContext(),"noti ended",Toast.LENGTH_LONG).show();

}
}
});

toggleButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){

Intent intent = new Intent(getApplicationContext(),AlarmReceiver.class);

pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),2,intent,PendingIntent.FLAG_UPDATE_CURRENT);

alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(), 1000*10,pendingIntent);
}
else{
alarmManager.cancel(pendingIntent);
Toast.makeText(getApplicationContext(),"noti ended",Toast.LENGTH_LONG).show();

}
}
});

}
}



public class AlarmReceiver extends BroadcastReceiver {

NotificationCompat.Builder notificationCompat;
private static final int uniqueID = 24314;


@Override
public void onReceive(Context context, Intent intent ) {

Toast.makeText(context,"welcome here",Toast.LENGTH_LONG).show();

alarm1(context);
alarm2(context);

}

public void alarm1 (Context context){

notificationCompat = new NotificationCompat.Builder(context);
notificationCompat.setAutoCancel(true);
notificationCompat .setTicker("Notification1");
notificationCompat .setContentTitle("Notification 1");
notificationCompat .setContentText("Good Job");
notificationCompat .setSmallIcon(R.mipmap.ic_launcher);
notificationCompat.setDefaults(Notification.DEFAULT_SOUND);
notificationCompat.setVibrate(new long[] {100,2000,100,2000});
notificationCompat.setLights(Color.BLUE,400,400);


Intent intent1 = new Intent(context,MainActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent1,PendingIntent.FLAG_UPDATE_CURRENT);

notificationCompat.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(uniqueID,notificationCompat.build());

}

public void alarm2 (Context context){

notificationCompat = new NotificationCompat.Builder(context);
notificationCompat.setAutoCancel(true);
notificationCompat .setTicker("Notification2");
notificationCompat .setContentTitle("Notification 2");
notificationCompat .setContentText("Good Job");
notificationCompat .setSmallIcon(R.mipmap.ic_launcher);
notificationCompat.setDefaults(Notification.DEFAULT_SOUND);
notificationCompat.setVibrate(new long[] {100,2000,100,2000});
notificationCompat.setLights(Color.BLUE,400,400);


Intent intent1 = new Intent(context,MainActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(context,2,intent1,PendingIntent.FLAG_UPDATE_CURRENT);

notificationCompat.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(uniqueID,notificationCompat.build());

}


}

Answer Source

the answer is to use shared Preference to save the state of Toggle Buttons then set the alarm or cancel it depend on the status of Toggle Button

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download