gpt4 book ai didi

java - onPause 调用后闹钟继续播放

转载 作者:行者123 更新时间:2023-11-30 00:47:26 26 4
gpt4 key购买 nike

下面是定时器的代码,它在达到 0 时播放声音(定时器工作正常)。问题是即使在调用 MainActivity.java 中的 onPause() 时声音仍然存在。

我在 SimpleIntentService.java 中实现了 onDestroy() 来停止声音,但显然即使在 finish() 中也没有调用它调用 Activity。当应用程序暂停时,我应该如何让声音停止?

这是我的 MainActivity.java

public class MainActivity extends Activity {

private BroadcastReceiver broadcastReceiver;
NumberPicker picker;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
picker = (NumberPicker) findViewById(minutePicker);

Log.i("TurnToTech", "Project Name - SimpleBackgroundService");

picker.setMinValue(0);
picker.setMaxValue(20);

broadcastReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent intent) {
String text = intent.getStringExtra(SimpleIntentService.PARAM_OUT_MSG);
Toast.makeText(getApplicationContext(),
text, Toast.LENGTH_SHORT).show();
}
};
}

Intent msgIntent;

public void startTimer(View view) {
setContentView(R.layout.activity_main);

msgIntent = new Intent(this, SimpleIntentService.class);
msgIntent.putExtra(SimpleIntentService.PARAM_IN_MSG, "Alarm: ");
msgIntent.putExtra("time", picker.getValue());

startService(msgIntent);
}

public void onResume() {
super.onResume();

IntentFilter filter = new IntentFilter(SimpleIntentService.ACTION_RESP);
filter.addCategory(Intent.CATEGORY_DEFAULT);

registerReceiver(broadcastReceiver,filter);
}

public void onPause() {
finish();
unregisterReceiver(broadcastReceiver);
super.onPause();
}
}

SimpleIntentService.java

public class SimpleIntentService extends IntentService {
public static final String PARAM_IN_MSG = "in_msg";
public static final String PARAM_OUT_MSG = "out_msg";
int time;

public static final String ACTION_RESP = "org.turntotech.intent.action.MESSAGE_PROCESSED";

public SimpleIntentService() {
super("SimpleIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
System.out.println("SimpleIntentService Called");
String msg = intent.getStringExtra(PARAM_IN_MSG);
int time = intent.getIntExtra("time", 0);

// Timer implementation
if (time == 0 ){
playSound();
}

while(time > 0){

SystemClock.sleep(5000); // 5 seconds
time -= 5;
String resultTxt = msg + time + " seconds remaining";
Intent broadcastIntent = new Intent();

broadcastIntent.setAction(ACTION_RESP);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
broadcastIntent.putExtra(PARAM_OUT_MSG, resultTxt);
broadcastIntent.putExtra("time", time);

sendBroadcast(broadcastIntent);
if (time == 0) {
playSound();
}
}
}

Uri alert;

public void playSound(){
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), alert);
r.play();
}

public void onDestroy() {
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), alert);
r.stop();

super.onDestroy();
}
}

最佳答案

在您的 IntentService 中,您并没有真正停止 onDestroy 函数中的相同警报。因为每次你得到它的一个新实例。

所以我想建议保留一个Ringtone 的公共(public)静态变量,以便可以从任何地方访问它。在您的 MainActivity 中声明它们。

public static Ringtone r; 
public static Uri alert;

MainActivityonCreate 函数中初始化它们。

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

// ... Other statements

// Initialize ringtone here
initializeRingtone();
}

private void initializeRingtone() {
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
r = RingtoneManager.getRingtone(getApplicationContext(), alert);
}

现在 MainActivityonPause() 函数应该如下所示

public void onPause() {
unregisterReceiver(broadcastReceiver);
r.stop();
super.onPause();
}

如果您想在从后台恢复应用程序然后计时器用完后播放声音,您可以考虑在 MainActivity< 的 onResume 函数中执行类似的操作

public void onResume() {
super.onResume();
registerReceiver(broadcastReceiver);
initializeRingtone(); // Initialize it again.
}

IntentService 中的 playSound() 函数可能如下所示。

public void playSound(){
// Initialize the alert and ringtone again.
MainActivity.alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
MainActivity.r = RingtoneManager.getRingtone(getApplicationContext(), alert);

MainActivity.r.play();
}

public void onDestroy() {
MainActivity.r.stop();
super.onDestroy();
}

希望对您有所帮助!

关于java - onPause 调用后闹钟继续播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41554447/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com