我希望能够设置仅与特定应用程序关联的铃声。如果带有特殊前缀的消息到达,我的应用程序会拦截它并执行它必须执行的操作。 在消息到达时,我希望能够通过铃声(专门播放该类消息)告诉用户“特殊”消息已到达。 任何有领导的人,请帮忙。
答案 0 :(得分:2)
您可以在应用程序中创建铃声首选项,并在收到任何通知时使用它并根据该铃声播放铃声。以下是为您的应用程序设置铃声的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
Uri defaultRingtoneUri = RingtoneManager.getDefaultUri(ringTonePref.getRingtoneType());
String defaultRingtone = defaultRingtoneUri.toString();
String selectedRingtone = ringTonePref.getPreferenceManager().getSharedPreferences()
.getString(NOTIFICATION_RINGTONE,defaultRingtone);
if (defaultRingtone.equals(selectedRingtone)) {
ringtoneUri = defaultRingtoneUri;
} else {
ringtoneUri = (TextUtils.isEmpty(selectedRingtone)) ? null : Uri.parse(selectedRingtone);
}
updateRingtoneSummary(ringTonePref, ringtoneUri);
final Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
Uri defaultRingtoneUri = RingtoneManager.getDefaultUri(ringTonePref.getRingtoneType());
String defaultRingtone = defaultRingtoneUri.toString();
String selectedRingtone = ringTonePref.getPreferenceManager().getSharedPreferences()
.getString(NOTIFICATION_RINGTONE,defaultRingtone);
if (defaultRingtone.equals(selectedRingtone)) {
ringtoneUri = defaultRingtoneUri;
} else {
ringtoneUri = (TextUtils.isEmpty(selectedRingtone)) ? null : Uri.parse(selectedRingtone);
}
updateRingtoneSummary(ringTonePref, ringtoneUri);
super.handleMessage(message);
}
};
ringTonePref.setOnPreferenceChangeListener(new OnPreferenceChangeListener (){
public boolean onPreferenceChange(Preference arg0, Object arg1) {
handler.sendEmptyMessage(UPDATE_RINGTONE_SUMMARY);
return true;
}
});
}
private void updateRingtoneSummary(RingtonePreference ringtonePreference, final Uri ringtoneUri) {
// Set the summary
if (ringtoneUri == null) {
// The ringtone picker returns null for "silent" ringtone
ringtonePreference.setSummary(R.string.silent);
} else {
Ringtone ringtone = RingtoneManager.getRingtone(this, ringtoneUri);
if (ringtone != null) {
ringtonePreference.setSummary(ringtone.getTitle(this));
}
}
}
preference.xml文件
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/notificationsettings">
<RingtonePreference
android:key="pref_key_ringtone"
android:title="@string/title_list_preference"
android:dialogTitle="@string/title_list_preference"
android:defaultValue="content://settings/system/notification_sound"
android:ringtoneType="notification" />
<CheckBoxPreference
android:key="pref_key_vibrate"
android:title="@string/vibrate"
android:defaultValue="true"/>
</PreferenceCategory>
</PreferenceScreen>