我已經嘗試了這麼多的解決方案原樣關RingtoneManager歌Android
RingtoneManager.stopPreviousRingtone();
但它自帶了一個錯誤 -
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();
};
}
當按下我的按鈕,鈴聲10秒後啓動,但是當我按下停止按鈕音樂不會停止。任何人都可以幫助我嗎?
儘量減少代碼到與您的問題相關的部分,以鼓勵人們通過它來幫助您。 – LionC 2015-02-07 18:59:43