How to stop the alarm ringing in loop
up vote
1
down vote
favorite
Hi guys i have been trying to add an alarm to my app.
But after i set the alarm it's not stopping, i plays forever.
The java classes i used is alarm receiver,activity-alarm.java and xml.
And how to add stop the alarm from the notification. Thanks in advance.
This is the main activity.
` public class MainActivity extends AppCompatActivity {
AlarmManager alarmManager;
private PendingIntent pending_intent;
private TimePicker alarmTimePicker;
private TextView alarmTextView;
private AlarmReceiver alarm;
MainActivity inst;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.context = this;
//alarm = new AlarmReceiver();
alarmTextView = (TextView) findViewById(R.id.alarmText);
final Intent myIntent = new Intent(this.context, AlarmReceiver.class);
// Get the alarm manager service
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
// set the alarm to the time that you picked
final Calendar calendar = Calendar.getInstance();
alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
Button start_alarm= (Button) findViewById(R.id.start_alarm);
start_alarm.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onClick(View v) {
calendar.add(Calendar.SECOND, 3);
//setAlarmText("You clicked a button");
final int hour = alarmTimePicker.getCurrentHour();
final int minute = alarmTimePicker.getCurrentMinute();;
Log.e("MyActivity", "In the receiver with " + hour + " and " +
minute);
setAlarmText("You clicked a " + hour + " and " + minute);
calendar.set(Calendar.HOUR_OF_DAY,
alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE,
alarmTimePicker.getCurrentMinute());
myIntent.putExtra("extra", "yes");
pending_intent = PendingIntent.getBroadcast(MainActivity.this,
0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pending_intent);
// now you should change the set Alarm text so it says
something nice
setAlarmText("Alarm set to " + hour + ":" + minute);
//Toast.makeText(getApplicationContext(), "You set the alarm",
Toast.LENGTH_SHORT).show();
}
});
Button stop_alarm= (Button) findViewById(R.id.stop_alarm);
stop_alarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int min = 1;
int max = 9;
Random r = new Random();
int random_number = r.nextInt(max - min + 1) + min;
Log.e("random number is ", String.valueOf(random_number));
myIntent.putExtra("extra", "no");
sendBroadcast(myIntent);
alarmManager.cancel(pending_intent);
setAlarmText("Alarm canceled");
//setAlarmText("You clicked a " + " canceled");
}
});
}
public void setAlarmText(String alarmText) {
alarmTextView.setText(alarmText);
}
@Override
public void onStart() {
super.onStart();
inst = this;
}
}`
java


add a comment |
up vote
1
down vote
favorite
Hi guys i have been trying to add an alarm to my app.
But after i set the alarm it's not stopping, i plays forever.
The java classes i used is alarm receiver,activity-alarm.java and xml.
And how to add stop the alarm from the notification. Thanks in advance.
This is the main activity.
` public class MainActivity extends AppCompatActivity {
AlarmManager alarmManager;
private PendingIntent pending_intent;
private TimePicker alarmTimePicker;
private TextView alarmTextView;
private AlarmReceiver alarm;
MainActivity inst;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.context = this;
//alarm = new AlarmReceiver();
alarmTextView = (TextView) findViewById(R.id.alarmText);
final Intent myIntent = new Intent(this.context, AlarmReceiver.class);
// Get the alarm manager service
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
// set the alarm to the time that you picked
final Calendar calendar = Calendar.getInstance();
alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
Button start_alarm= (Button) findViewById(R.id.start_alarm);
start_alarm.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onClick(View v) {
calendar.add(Calendar.SECOND, 3);
//setAlarmText("You clicked a button");
final int hour = alarmTimePicker.getCurrentHour();
final int minute = alarmTimePicker.getCurrentMinute();;
Log.e("MyActivity", "In the receiver with " + hour + " and " +
minute);
setAlarmText("You clicked a " + hour + " and " + minute);
calendar.set(Calendar.HOUR_OF_DAY,
alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE,
alarmTimePicker.getCurrentMinute());
myIntent.putExtra("extra", "yes");
pending_intent = PendingIntent.getBroadcast(MainActivity.this,
0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pending_intent);
// now you should change the set Alarm text so it says
something nice
setAlarmText("Alarm set to " + hour + ":" + minute);
//Toast.makeText(getApplicationContext(), "You set the alarm",
Toast.LENGTH_SHORT).show();
}
});
Button stop_alarm= (Button) findViewById(R.id.stop_alarm);
stop_alarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int min = 1;
int max = 9;
Random r = new Random();
int random_number = r.nextInt(max - min + 1) + min;
Log.e("random number is ", String.valueOf(random_number));
myIntent.putExtra("extra", "no");
sendBroadcast(myIntent);
alarmManager.cancel(pending_intent);
setAlarmText("Alarm canceled");
//setAlarmText("You clicked a " + " canceled");
}
});
}
public void setAlarmText(String alarmText) {
alarmTextView.setText(alarmText);
}
@Override
public void onStart() {
super.onStart();
inst = this;
}
}`
java


What do you mean, "plays forever"? Can you show the code that plays the sound?
– greeble31
Nov 9 at 18:00
It dosen't stop playing the tune, i followed this tutorial link @greeble31
– 5NIP3R Xd
Nov 10 at 8:01
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Hi guys i have been trying to add an alarm to my app.
But after i set the alarm it's not stopping, i plays forever.
The java classes i used is alarm receiver,activity-alarm.java and xml.
And how to add stop the alarm from the notification. Thanks in advance.
This is the main activity.
` public class MainActivity extends AppCompatActivity {
AlarmManager alarmManager;
private PendingIntent pending_intent;
private TimePicker alarmTimePicker;
private TextView alarmTextView;
private AlarmReceiver alarm;
MainActivity inst;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.context = this;
//alarm = new AlarmReceiver();
alarmTextView = (TextView) findViewById(R.id.alarmText);
final Intent myIntent = new Intent(this.context, AlarmReceiver.class);
// Get the alarm manager service
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
// set the alarm to the time that you picked
final Calendar calendar = Calendar.getInstance();
alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
Button start_alarm= (Button) findViewById(R.id.start_alarm);
start_alarm.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onClick(View v) {
calendar.add(Calendar.SECOND, 3);
//setAlarmText("You clicked a button");
final int hour = alarmTimePicker.getCurrentHour();
final int minute = alarmTimePicker.getCurrentMinute();;
Log.e("MyActivity", "In the receiver with " + hour + " and " +
minute);
setAlarmText("You clicked a " + hour + " and " + minute);
calendar.set(Calendar.HOUR_OF_DAY,
alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE,
alarmTimePicker.getCurrentMinute());
myIntent.putExtra("extra", "yes");
pending_intent = PendingIntent.getBroadcast(MainActivity.this,
0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pending_intent);
// now you should change the set Alarm text so it says
something nice
setAlarmText("Alarm set to " + hour + ":" + minute);
//Toast.makeText(getApplicationContext(), "You set the alarm",
Toast.LENGTH_SHORT).show();
}
});
Button stop_alarm= (Button) findViewById(R.id.stop_alarm);
stop_alarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int min = 1;
int max = 9;
Random r = new Random();
int random_number = r.nextInt(max - min + 1) + min;
Log.e("random number is ", String.valueOf(random_number));
myIntent.putExtra("extra", "no");
sendBroadcast(myIntent);
alarmManager.cancel(pending_intent);
setAlarmText("Alarm canceled");
//setAlarmText("You clicked a " + " canceled");
}
});
}
public void setAlarmText(String alarmText) {
alarmTextView.setText(alarmText);
}
@Override
public void onStart() {
super.onStart();
inst = this;
}
}`
java


Hi guys i have been trying to add an alarm to my app.
But after i set the alarm it's not stopping, i plays forever.
The java classes i used is alarm receiver,activity-alarm.java and xml.
And how to add stop the alarm from the notification. Thanks in advance.
This is the main activity.
` public class MainActivity extends AppCompatActivity {
AlarmManager alarmManager;
private PendingIntent pending_intent;
private TimePicker alarmTimePicker;
private TextView alarmTextView;
private AlarmReceiver alarm;
MainActivity inst;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.context = this;
//alarm = new AlarmReceiver();
alarmTextView = (TextView) findViewById(R.id.alarmText);
final Intent myIntent = new Intent(this.context, AlarmReceiver.class);
// Get the alarm manager service
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
// set the alarm to the time that you picked
final Calendar calendar = Calendar.getInstance();
alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
Button start_alarm= (Button) findViewById(R.id.start_alarm);
start_alarm.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onClick(View v) {
calendar.add(Calendar.SECOND, 3);
//setAlarmText("You clicked a button");
final int hour = alarmTimePicker.getCurrentHour();
final int minute = alarmTimePicker.getCurrentMinute();;
Log.e("MyActivity", "In the receiver with " + hour + " and " +
minute);
setAlarmText("You clicked a " + hour + " and " + minute);
calendar.set(Calendar.HOUR_OF_DAY,
alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE,
alarmTimePicker.getCurrentMinute());
myIntent.putExtra("extra", "yes");
pending_intent = PendingIntent.getBroadcast(MainActivity.this,
0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pending_intent);
// now you should change the set Alarm text so it says
something nice
setAlarmText("Alarm set to " + hour + ":" + minute);
//Toast.makeText(getApplicationContext(), "You set the alarm",
Toast.LENGTH_SHORT).show();
}
});
Button stop_alarm= (Button) findViewById(R.id.stop_alarm);
stop_alarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int min = 1;
int max = 9;
Random r = new Random();
int random_number = r.nextInt(max - min + 1) + min;
Log.e("random number is ", String.valueOf(random_number));
myIntent.putExtra("extra", "no");
sendBroadcast(myIntent);
alarmManager.cancel(pending_intent);
setAlarmText("Alarm canceled");
//setAlarmText("You clicked a " + " canceled");
}
});
}
public void setAlarmText(String alarmText) {
alarmTextView.setText(alarmText);
}
@Override
public void onStart() {
super.onStart();
inst = this;
}
}`
java


java


edited Nov 11 at 9:31
asked Nov 9 at 17:04


5NIP3R Xd
157
157
What do you mean, "plays forever"? Can you show the code that plays the sound?
– greeble31
Nov 9 at 18:00
It dosen't stop playing the tune, i followed this tutorial link @greeble31
– 5NIP3R Xd
Nov 10 at 8:01
add a comment |
What do you mean, "plays forever"? Can you show the code that plays the sound?
– greeble31
Nov 9 at 18:00
It dosen't stop playing the tune, i followed this tutorial link @greeble31
– 5NIP3R Xd
Nov 10 at 8:01
What do you mean, "plays forever"? Can you show the code that plays the sound?
– greeble31
Nov 9 at 18:00
What do you mean, "plays forever"? Can you show the code that plays the sound?
– greeble31
Nov 9 at 18:00
It dosen't stop playing the tune, i followed this tutorial link @greeble31
– 5NIP3R Xd
Nov 10 at 8:01
It dosen't stop playing the tune, i followed this tutorial link @greeble31
– 5NIP3R Xd
Nov 10 at 8:01
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
I have found the solution.
After adding this alarm stops:
Intent intent = new Intent(this, RingtonePlayingService.class);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
working Mainactivity.java
public class MainActivity extends AppCompatActivity {
AlarmManager alarmManager;
private PendingIntent pending_intent;
private TimePicker alarmTimePicker;
private TextView alarmTextView;
private AlarmReceiver alarm;
MainActivity inst;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.context = this;
//alarm = new AlarmReceiver();
alarmTextView = (TextView) findViewById(R.id.alarmText);
final Intent myIntent = new Intent(this.context, AlarmReceiver.class);
// Get the alarm manager service
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
// set the alarm to the time that you picked
final Calendar calendar = Calendar.getInstance();
alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
Button start_alarm= (Button) findViewById(R.id.start_alarm);
start_alarm.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onClick(View v) {
calendar.add(Calendar.SECOND, 3);
//setAlarmText("You clicked a button");
final int hour = alarmTimePicker.getCurrentHour();
final int minute = alarmTimePicker.getCurrentMinute();;
Log.e("MyActivity", "In the receiver with " + hour + " and " +
minute);
setAlarmText("You clicked a " + hour + " and " + minute);
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
myIntent.putExtra("extra", "yes");
pending_intent = PendingIntent.getBroadcast(MainActivity.this, 0,
myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pending_intent);
// now you should change the set Alarm text so it says something nice
setAlarmText("Alarm set to " + hour + ":" + minute);
//Toast.makeText(getApplicationContext(), "You set the alarm",
Toast.LENGTH_SHORT).show();
}
});
Intent intent = new Intent(this, RingtonePlayingService.class);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Button stop_alarm= (Button) findViewById(R.id.stop_alarm);
stop_alarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int min = 1;
int max = 9;
Random r = new Random();
int random_number = r.nextInt(max - min + 1) + min;
Log.e("random number is ", String.valueOf(random_number));
myIntent.putExtra("extra", "no");
sendBroadcast(myIntent);
//setAlarmText("You clicked a " + " canceled");
}
});
}
public void setAlarmText(String alarmText) {
alarmTextView.setText(alarmText);
}
@Override
public void onStart() {
super.onStart();
inst = this;
}
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I have found the solution.
After adding this alarm stops:
Intent intent = new Intent(this, RingtonePlayingService.class);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
working Mainactivity.java
public class MainActivity extends AppCompatActivity {
AlarmManager alarmManager;
private PendingIntent pending_intent;
private TimePicker alarmTimePicker;
private TextView alarmTextView;
private AlarmReceiver alarm;
MainActivity inst;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.context = this;
//alarm = new AlarmReceiver();
alarmTextView = (TextView) findViewById(R.id.alarmText);
final Intent myIntent = new Intent(this.context, AlarmReceiver.class);
// Get the alarm manager service
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
// set the alarm to the time that you picked
final Calendar calendar = Calendar.getInstance();
alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
Button start_alarm= (Button) findViewById(R.id.start_alarm);
start_alarm.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onClick(View v) {
calendar.add(Calendar.SECOND, 3);
//setAlarmText("You clicked a button");
final int hour = alarmTimePicker.getCurrentHour();
final int minute = alarmTimePicker.getCurrentMinute();;
Log.e("MyActivity", "In the receiver with " + hour + " and " +
minute);
setAlarmText("You clicked a " + hour + " and " + minute);
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
myIntent.putExtra("extra", "yes");
pending_intent = PendingIntent.getBroadcast(MainActivity.this, 0,
myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pending_intent);
// now you should change the set Alarm text so it says something nice
setAlarmText("Alarm set to " + hour + ":" + minute);
//Toast.makeText(getApplicationContext(), "You set the alarm",
Toast.LENGTH_SHORT).show();
}
});
Intent intent = new Intent(this, RingtonePlayingService.class);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Button stop_alarm= (Button) findViewById(R.id.stop_alarm);
stop_alarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int min = 1;
int max = 9;
Random r = new Random();
int random_number = r.nextInt(max - min + 1) + min;
Log.e("random number is ", String.valueOf(random_number));
myIntent.putExtra("extra", "no");
sendBroadcast(myIntent);
//setAlarmText("You clicked a " + " canceled");
}
});
}
public void setAlarmText(String alarmText) {
alarmTextView.setText(alarmText);
}
@Override
public void onStart() {
super.onStart();
inst = this;
}
}
add a comment |
up vote
0
down vote
I have found the solution.
After adding this alarm stops:
Intent intent = new Intent(this, RingtonePlayingService.class);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
working Mainactivity.java
public class MainActivity extends AppCompatActivity {
AlarmManager alarmManager;
private PendingIntent pending_intent;
private TimePicker alarmTimePicker;
private TextView alarmTextView;
private AlarmReceiver alarm;
MainActivity inst;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.context = this;
//alarm = new AlarmReceiver();
alarmTextView = (TextView) findViewById(R.id.alarmText);
final Intent myIntent = new Intent(this.context, AlarmReceiver.class);
// Get the alarm manager service
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
// set the alarm to the time that you picked
final Calendar calendar = Calendar.getInstance();
alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
Button start_alarm= (Button) findViewById(R.id.start_alarm);
start_alarm.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onClick(View v) {
calendar.add(Calendar.SECOND, 3);
//setAlarmText("You clicked a button");
final int hour = alarmTimePicker.getCurrentHour();
final int minute = alarmTimePicker.getCurrentMinute();;
Log.e("MyActivity", "In the receiver with " + hour + " and " +
minute);
setAlarmText("You clicked a " + hour + " and " + minute);
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
myIntent.putExtra("extra", "yes");
pending_intent = PendingIntent.getBroadcast(MainActivity.this, 0,
myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pending_intent);
// now you should change the set Alarm text so it says something nice
setAlarmText("Alarm set to " + hour + ":" + minute);
//Toast.makeText(getApplicationContext(), "You set the alarm",
Toast.LENGTH_SHORT).show();
}
});
Intent intent = new Intent(this, RingtonePlayingService.class);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Button stop_alarm= (Button) findViewById(R.id.stop_alarm);
stop_alarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int min = 1;
int max = 9;
Random r = new Random();
int random_number = r.nextInt(max - min + 1) + min;
Log.e("random number is ", String.valueOf(random_number));
myIntent.putExtra("extra", "no");
sendBroadcast(myIntent);
//setAlarmText("You clicked a " + " canceled");
}
});
}
public void setAlarmText(String alarmText) {
alarmTextView.setText(alarmText);
}
@Override
public void onStart() {
super.onStart();
inst = this;
}
}
add a comment |
up vote
0
down vote
up vote
0
down vote
I have found the solution.
After adding this alarm stops:
Intent intent = new Intent(this, RingtonePlayingService.class);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
working Mainactivity.java
public class MainActivity extends AppCompatActivity {
AlarmManager alarmManager;
private PendingIntent pending_intent;
private TimePicker alarmTimePicker;
private TextView alarmTextView;
private AlarmReceiver alarm;
MainActivity inst;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.context = this;
//alarm = new AlarmReceiver();
alarmTextView = (TextView) findViewById(R.id.alarmText);
final Intent myIntent = new Intent(this.context, AlarmReceiver.class);
// Get the alarm manager service
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
// set the alarm to the time that you picked
final Calendar calendar = Calendar.getInstance();
alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
Button start_alarm= (Button) findViewById(R.id.start_alarm);
start_alarm.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onClick(View v) {
calendar.add(Calendar.SECOND, 3);
//setAlarmText("You clicked a button");
final int hour = alarmTimePicker.getCurrentHour();
final int minute = alarmTimePicker.getCurrentMinute();;
Log.e("MyActivity", "In the receiver with " + hour + " and " +
minute);
setAlarmText("You clicked a " + hour + " and " + minute);
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
myIntent.putExtra("extra", "yes");
pending_intent = PendingIntent.getBroadcast(MainActivity.this, 0,
myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pending_intent);
// now you should change the set Alarm text so it says something nice
setAlarmText("Alarm set to " + hour + ":" + minute);
//Toast.makeText(getApplicationContext(), "You set the alarm",
Toast.LENGTH_SHORT).show();
}
});
Intent intent = new Intent(this, RingtonePlayingService.class);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Button stop_alarm= (Button) findViewById(R.id.stop_alarm);
stop_alarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int min = 1;
int max = 9;
Random r = new Random();
int random_number = r.nextInt(max - min + 1) + min;
Log.e("random number is ", String.valueOf(random_number));
myIntent.putExtra("extra", "no");
sendBroadcast(myIntent);
//setAlarmText("You clicked a " + " canceled");
}
});
}
public void setAlarmText(String alarmText) {
alarmTextView.setText(alarmText);
}
@Override
public void onStart() {
super.onStart();
inst = this;
}
}
I have found the solution.
After adding this alarm stops:
Intent intent = new Intent(this, RingtonePlayingService.class);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
working Mainactivity.java
public class MainActivity extends AppCompatActivity {
AlarmManager alarmManager;
private PendingIntent pending_intent;
private TimePicker alarmTimePicker;
private TextView alarmTextView;
private AlarmReceiver alarm;
MainActivity inst;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.context = this;
//alarm = new AlarmReceiver();
alarmTextView = (TextView) findViewById(R.id.alarmText);
final Intent myIntent = new Intent(this.context, AlarmReceiver.class);
// Get the alarm manager service
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
// set the alarm to the time that you picked
final Calendar calendar = Calendar.getInstance();
alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
Button start_alarm= (Button) findViewById(R.id.start_alarm);
start_alarm.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onClick(View v) {
calendar.add(Calendar.SECOND, 3);
//setAlarmText("You clicked a button");
final int hour = alarmTimePicker.getCurrentHour();
final int minute = alarmTimePicker.getCurrentMinute();;
Log.e("MyActivity", "In the receiver with " + hour + " and " +
minute);
setAlarmText("You clicked a " + hour + " and " + minute);
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
myIntent.putExtra("extra", "yes");
pending_intent = PendingIntent.getBroadcast(MainActivity.this, 0,
myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pending_intent);
// now you should change the set Alarm text so it says something nice
setAlarmText("Alarm set to " + hour + ":" + minute);
//Toast.makeText(getApplicationContext(), "You set the alarm",
Toast.LENGTH_SHORT).show();
}
});
Intent intent = new Intent(this, RingtonePlayingService.class);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Button stop_alarm= (Button) findViewById(R.id.stop_alarm);
stop_alarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int min = 1;
int max = 9;
Random r = new Random();
int random_number = r.nextInt(max - min + 1) + min;
Log.e("random number is ", String.valueOf(random_number));
myIntent.putExtra("extra", "no");
sendBroadcast(myIntent);
//setAlarmText("You clicked a " + " canceled");
}
});
}
public void setAlarmText(String alarmText) {
alarmTextView.setText(alarmText);
}
@Override
public void onStart() {
super.onStart();
inst = this;
}
}
edited Nov 11 at 12:53


Touhidul Islam
356111
356111
answered Nov 11 at 9:34


5NIP3R Xd
157
157
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230292%2fhow-to-stop-the-alarm-ringing-in-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What do you mean, "plays forever"? Can you show the code that plays the sound?
– greeble31
Nov 9 at 18:00
It dosen't stop playing the tune, i followed this tutorial link @greeble31
– 5NIP3R Xd
Nov 10 at 8:01