开发者

setting ringtone for a specific application only

I want to be able to set a开发者_Python百科 ringtone which is associated to a particular application only. If the message with a special prefix arrives, my application intercepts it and does whatever it has to do. On message arriving, i want to be able to tell the user that the 'special' message has arrived, through a ringtone( which exclusively plays upon that kind of msg). anyone with a lead, please help.


You can create ringtone preference in your application and use it when any notification comes and play ringtone according to that. below is code for setting ringtone for you application

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 file

<?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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜