gpt4 book ai didi

java - 从 Android 首选项设置铃声不起作用

转载 作者:行者123 更新时间:2023-12-01 10:10:03 25 4
gpt4 key购买 nike

我有一个提醒应用程序,允许用户从我使用 android 首选项实现的设置选项中设置通知所需的铃声。

铃声对话框与列表一起显示,我选择的铃声已更新,但所选铃声未设置为通知。它始终以设备的默认铃声进行通知。

我引用并尝试了类似问题的答案,但它不起作用

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

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

    @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

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);

最佳答案

第 1 步)从偏好设置中获取铃声。

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);

考虑使用 DEFAULT_NOTIFICATION_URI 而不是 DEFAULT_RINGTONE_URI

第2步)将其设置为通知。

NotificationCompat.Builder b = new NotificationCompat.Builder(context)
.setSound(ringtoneuri)
...

关于java - 从 Android 首选项设置铃声不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36177407/

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