gpt4 book ai didi

android - AlarmManager 不播放选定的音调

转载 作者:行者123 更新时间:2023-12-05 00:01:50 27 4
gpt4 key购买 nike

我有一个要在其中创建警报的 Android 应用程序。这是一个正常的闹钟,应该在指定的时间叫醒你。与任何警报一样,您还可以通过在创建警报时选择声音文件来指定它将使用哪种警报声音。
闹钟效果很好,它在正确的时间开始,但它总是只播放手机的默认铃声。不是我创建警报时选择的那个。
铃声选择:

ringtoneURI = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
数据库存储:
SQLiteDatabase database=getWritableDatabase();
ContentValues contentValues=new ContentValues();
...
contentValues.put(DBFields.ALARM_TONE, ringtoneURI.toString());
...
database.insert(DBFields.TABLE_OF_ALARMS,null,contentValues);
警报创建:
Intent intent = new Intent(AddAlarm.this, AlarmReceiver.class);
intent.putExtra("AlarmTone", cursor.getString(cursor.getColumnIndex(DBFields.ALARM_TONE)));
intent.putExtra("AlarmId", cursor.getInt(cursor.getColumnIndex(DBFields.ALARM_ID)));
intent.putExtra("AlarmTime", cursor.getString(cursor.getColumnIndex(DBFields.ALARM_TIME)));
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
100,
intent,
FLAG_UPDATE_CURRENT);

alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
报警接收器:
NotificationCompat.Builder builder = new 
NotificationCompat.Builder(context, CHANNEL_ID);
Uri soundUri = Uri.parse(intent.getStringExtra("AlarmTone"));
builder.setSound(soundUri);
我已经使用调试器和正确的 AlarmName 逐步完成了代码。被选中并且是 Intent 的一部分.因此,我假设正确的名称被发送到 AlarmManager而且我不知道问题出在哪里。
如何让闹钟播放正确的音调?

最佳答案

我在 Geeks for Geeks site: 中找到了这个
使用它并调整您的数据库数据:

Calendar calendar = Calendar.getInstance();

// calendar is called to get current time in hour and minute
calendar.set(Calendar.HOUR_OF_DAY, YourHour);
calendar.set(Calendar.MINUTE, YourMinute());

// using intent class AlarmReceiver which inherits
// BroadcastReceiver
Intent intent = new Intent(this, AlarmReceiver.class);

// we call broadcast using pendingIntent
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

time = yourTimeToWait

// Alarm rings continuously until toggle button is turned off
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, 10000, pendingIntent);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (time * 1000), pendingIntent);
然后创建一个名为 AlarmReciever.java 的类。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Vibrator;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

public class AlarmReceiver extends BroadcastReceiver {
@RequiresApi(api = Build.VERSION_CODES.Q)
@Override
// implement onReceive() method
public void onReceive(Context context, Intent intent) {

Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.YOUR_TYPE);
if (alarmUri == null) {
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.YOUR_TYPE);
}

// setting default ringtone
Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);

// play ringtone
ringtone.play();
}
}

简而言之,正在发生的是你的计时器 CalenderalarmManager实例正在调用 AlarmReciever.java这是一个 BroadcastReciever .然后 AlamrReciever 类处理实际播放的计时器声音。

关于android - AlarmManager 不播放选定的音调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72419674/

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