我有一个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);
AlarmReceiver:
NotificationCompat.Builder builder = new
NotificationCompat.Builder(context, CHANNEL_ID);
Uri soundUri = Uri.parse(intent.getStringExtra("AlarmTone"));
builder.setSound(soundUri);
我已经选择了debugger的代码,并且选择了正确的 alarmname
,并且是 Intent Intent
的一部分。因此,我认为正确的名称正在发送到 AlarmManager
,我不知道问题可能在哪里。
如何使警报播放正确的音调?
I have an Android app in which I want to create an alarm. It's a normal alarm that should wake you up at a specified time. As with any alarm, you can also specify which alarm sound it will use, by selecting a sound file when creating the alarm.
The alarm works well, it starts at the correct time, but it always plays only phone's default tone. Not the one I selected when creating the alarm.
Ringtone selection:
ringtoneURI = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
Database storing:
SQLiteDatabase database=getWritableDatabase();
ContentValues contentValues=new ContentValues();
...
contentValues.put(DBFields.ALARM_TONE, ringtoneURI.toString());
...
database.insert(DBFields.TABLE_OF_ALARMS,null,contentValues);
Alarm creation:
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);
AlarmReceiver:
NotificationCompat.Builder builder = new
NotificationCompat.Builder(context, CHANNEL_ID);
Uri soundUri = Uri.parse(intent.getStringExtra("AlarmTone"));
builder.setSound(soundUri);
I have stepped through the code with debugger and the correct AlarmName
is selected and is part of the Intent
. Therefore I assume that the correct name is being sent to the AlarmManager
and I have no idea where the problem could be.
How can I make the alarm play the correct tone?
发布评论
评论(1)
我在
使用此内容并调整数据库数据:
然后创建一个名为AlarmReciever.java的类。
简而言之,发生的事情是您的计时器
calender
和alarmmanager
实例正在呼叫alarm> alarm> armar>代码>。然后,Alamrreciever课程实际上播放计时器声音。
I found this in Geeks for Geeks site:
Use this and tweak to your database data:
Then create a class called AlarmReciever.java.
In short, what is happening is your timer
Calender
andalarmManager
instances are calling to theAlarmReciever.java
which is aBroadcastReciever
. Then the AlamrReciever class handles actually playing timer sounds.