[英]Setting Ringtone from android preferences is not working
I am having an reminder application that will allow the user to set the required ringtone for notification from settings option which I implemnted using android preferences. 我有一个提醒应用程序,该应用程序将允许用户设置所需的铃声,以便通过设置选项(我使用android偏好设置实现)来进行通知。
The ringtone dialog is displayed with the list and the ringtone I select is updated but the selected ringtone is not set to the notification. 将显示带有列表的铃声对话框,并且我选择的铃声会更新,但是所选铃声未设置为通知。 It always notifies with the default ringtone of the device. 它始终以设备的默认铃声进行通知。
I referred and tried the answers for similar questions but its not working 我引用并尝试了类似问题的答案,但没有用
Preferences.xml Preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Settings">
<RingtonePreference
android:name="RingtonePreference"
android:summary="Select a ringtone"
android:title="Ringtones"
android:key="@string/ringtonePref"
android:defaultValue="content://settings/system/notification_sound"
/>
</PreferenceCategory>
</PreferenceScreen>
Prefrence.java Prefrence.java
public class Preferences extends PreferenceActivity implements OnPreferenceChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
String ringtonePreference;
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
ringtonePreference = prefs.getString("ringtonePref",
"DEFAULT_RINGTONE_URI");
}
@Override
protected void onResume() {
super.onResume();
RingtonePreference pref = (RingtonePreference) findPreference(getString(R.string.ringtonePref));
pref.setOnPreferenceChangeListener(this);
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// TODO Auto-generated method stub
updateRingtoneSummary((RingtonePreference) preference, Uri.parse((String) newValue));
return true;
}
private void updateRingtoneSummary(RingtonePreference preference, Uri ringtoneuri) {
// TODO Auto-generated method stub
Ringtone ringtone = RingtoneManager.getRingtone(this, ringtoneuri);
if (ringtone != null)
preference.setSummary(ringtone.getTitle(this));
else
preference.setSummary("Silent");
}
}
MainActivity.java MainActivity.java
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_settings:
Intent intent = new Intent(this, Preferences.class);
startActivity(intent);
return true;
case R.id.menu_about:
Intent i = new Intent(this, About.class);
startActivity(i);
}
return super.onMenuItemSelected(featureId, item);
}
NotificationService.java NotificationService.java
NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, ReminderEditActivity.class);
notificationIntent.putExtra(RemindersDbAdapter.KEY_ROWID, rowId);
PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
Notification note=new Notification(R.drawable.ic_icon, title, System.currentTimeMillis());
note.setLatestEventInfo(this, title, "You have a task to be done!", pi);
note.flags |= Notification.FLAG_AUTO_CANCEL;
int id = (int)((long)rowId);
mgr.notify(id, note);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
// DO NOT ever call getBaseContext() unless you know what you're doing. "this" or "getApplicationContext() is perfect. Google the difference.
String ringtonePreference = prefs.getString("ringtonePref", "DEFAULT_RINGTONE_URI");
// The key of preference was "@string/ringtonePref" which is useless since you're hardcoding the string here anyway.
Uri ringtoneuri = Uri.parse(ringtonePreference);
Consider using DEFAULT_NOTIFICATION_URI
instead of DEFAULT_RINGTONE_URI
. 考虑使用DEFAULT_NOTIFICATION_URI
而不是DEFAULT_RINGTONE_URI
。
NotificationCompat.Builder b = new NotificationCompat.Builder(context)
.setSound(ringtoneuri)
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.