gpt4 book ai didi

Android AlarmManager 和服务问题

转载 作者:行者123 更新时间:2023-11-29 18:22:14 26 4
gpt4 key购买 nike

我的应用程序中有一些文件,但现在只有 3 个文件是重要的。这是一个带有闹钟和通知的提醒应用程序。我有一个包含复选框及其监听器的 maincode.java 文件。如果用户选中复选框,AlarmManager 会向 AlarmReceiver.java 发送一个 Intent ,它会启动 MyService.java。 MyService java 包含有关播放声音的代码。代码是部分的。我的服务.java:

public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");

player = MediaPlayer.create(this, R.raw.sound);
player.setLooping(false); // Set looping
}

@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
player.stop();
}

@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
player.start();
}

AlarmReceiver.java:

public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");

player = MediaPlayer.create(this, R.raw.sound);
player.setLooping(false); // Set looping

maincode.java重要部分:

    cb1 = (CheckBox) findViewById(R.id.CheckBox01);
cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (cb1.isChecked())
{
if (GlobalVars.getHourOfDay() >= 0)
{
Toast.makeText(maincode.this, "ok", Toast.LENGTH_SHORT).show();
rem1.setText(GlobalVars.getReminder1name());
Intent intent = new Intent(maincode.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(bInsulinReminder.this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, GlobalVars.getHourOfDay());
cal.set(Calendar.MINUTE, GlobalVars.getMinute());
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()+ 3000, 6000, pendingIntent);

}
Toast.makeText(maincode.this, "Checked", Toast.LENGTH_SHORT).show();
} else {
rem1.setText("No reminder set");
Toast.makeText(maincode.this, "Not checked", Toast.LENGTH_SHORT).show();
}
}

});

(rem1 是提醒按钮,其文本取决于用户想要的任何名称)

代码的问题在于,如果我启动闹钟,就无法停止。我知道 MyService.java 中有 player.stop() 命令,但我如何从未选中复选框的 maincode.java 末尾调用它?

最佳答案

不,您不能直接从监听器执行此操作。您可以通过这种方式禁用警报:

Intent intent = new Intent(maincode.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(bInsulinReminder.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
pendingItem.cancel();
alarmManager.cancel(pendingItem);

或者如果(我想)AlarmReceiver 是 BroadcastReceiver 的实现,并且从 onReceive 方法开始你的 MyService,它是 Service 类的实现。

因此,如果您想从 maincode.java 监听器内部停止此警报,您可以通过重新创建您在 AlarmReceiver 中使用的 PendingIntent 并执行 stopService 方法来停止 MyService。

希望对您有所帮助。

关于Android AlarmManager 和服务问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4820044/

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