jabirfatah91
Lurker
My alarm clock uses a toggle switch to start my alarm. What I want is as soon as the user click on the button again, it should stop the alarm (ring). Toggling between ON and OFF will start and stop respectively.
Right now, I am only able to start my alarm. In fact it never gets stopped. If I switch the button OFF the sound gets played even faster, that is very strange. How can I stop it? I have pasted the only part of code where I am assuming the problem occurs. If you need to look at my whole app's code I will post it. [NOTE: everything else works fine]
MainActivity:
AlarmReceiver:
Right now, I am only able to start my alarm. In fact it never gets stopped. If I switch the button OFF the sound gets played even faster, that is very strange. How can I stop it? I have pasted the only part of code where I am assuming the problem occurs. If you need to look at my whole app's code I will post it. [NOTE: everything else works fine]
MainActivity:
Code:
package com.mycompany.alarmclock;
//I haven't post the imported stuff here, they are in my file
import android.support.v7.app.ActionBarActivity;
import java.util.Calendar;
public class MainActivity extends Activity {
AlarmManager alarmManager;
private PendingIntent pendingIntent;//an action to be performed by other/foreign application
private TimePicker alarmTimePicker;//In where the user set the time
private static MainActivity inst;
private TextView alarmTextView;//the area where alarm message/notification will be displayed
public static MainActivity instance() {
return inst;
}
public void onStart() {
super.onStart();
inst = this;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
alarmTextView = (TextView) findViewById(R.id.alarmText);
ToggleButton alarmToggle = (ToggleButton) findViewById(R.id.alarmToggle);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
}
public void onToggleClicked(View view) {
if (((ToggleButton) view).isChecked()) {//if toggle button is "ON" do the alarming function
Log.d("MainActivity", "Alarm On");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
//In this else statement I need to add the logic so that the alarm clock stops after clicking the toggle button
} else {//else if the toggle button is pressed again, switch off the alarm /sound
alarmManager.cancel(pendingIntent);
setAlarmText("");
Log.d("MyActivity", "Alarm Off");
}
}
public void setAlarmText(String alarmText) {
alarmTextView.setText(alarmText);
}
}
AlarmReceiver:
Code:
package com.mycompany.alarmclock;
//I haven't posted my imported stuff here, they are on my file.
import android.support.v4.content.WakefulBroadcastReceiver;
import java.net.URI;
public class AlarmReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
MainActivity inst=MainActivity.instance();
inst.setAlarmText("Get Up! Get up!");
Uri alarmUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null) {
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
ringtone.play();
//this will send a notification message
ComponentName comp = new ComponentName(context.getPackageName(),
AlarmService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}