Reputation: 345
I have tried many solutions such as-
RingtoneManager.stopPreviousRingtone();
But it comes with an error of -
Cannot make a static reference to the non-static method stopPreviousRingtone() from the type RingtoneManager
MyAlarmService.java
package com.example.getbettersoon;
import android.app.Service;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.IBinder;
import android.widget.Toast;
public class MyAlarmService extends Service {
static Ringtone ringtone;
@Override
public void onDestroy() {
super.onDestroy();
//Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
//RingtoneManager.stopPreviousRingtone();
ringtone.stop(); // THIS DOES NOT STOP THE RINGTONE
}
@SuppressWarnings("deprecation")
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
Uri alarm = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
ringtone = RingtoneManager.getRingtone(getApplicationContext(), alarm);
ringtone.play();
}
}
Alarm.java
package com.example.getbettersoon;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.media.Ringtone;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Alarm extends Activity {
String gethours;
int myNum;
private PendingIntent pendingIntent;
public void SetAlarm(View arg0) {
Intent myIntent = new Intent(Alarm.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(Alarm.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10); // test
//TextView tv = (TextView) findViewById(R.id.bthrs);
//tv.setText(myNum);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(Alarm.this, "Start Alarm", Toast.LENGTH_LONG).show();
};
public void OffAlarm(View arg0) {
Intent myIntent = new Intent(Alarm.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(Alarm.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
// Tell the user about what we did.
Toast.makeText(Alarm.this, "Cancel!", Toast.LENGTH_LONG).show();
};
}
When my button is pressed, the ring tone starts after 10 seconds but when I press the stop button the music doesn't stop. Can anyone help me with it?
Upvotes: 0
Views: 573
Reputation: 8415
I assume you are relying on your service being stopped to stop the ringtone playback. The line:
alarmManager.cancel(pendingIntent);
merely removes the service from being deployed in the future by the AlarmManager and does not stop an active service.
To stop a running service you need to use Context.stopService()
For example:
public void OffAlarm(View arg0){
Intent myIntent = new Intent(Alarm.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(Alarm.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
// Stops an existing running service
stopService(myIntent);
// Tell the user about what we did.
Toast.makeText(Alarm.this, "Cancel!", Toast.LENGTH_LONG).show();
}
only then will the onDestroy()
method be called by the android system and your ringtone stopped.
Upvotes: 1