Explanation of the process for setting Android ringtones

Source: Internet
Author: User

The general idea is:
1. Intercept ringtones
2. Save it to the SD card
3. Get the ringtone Information
4. Write the information and file path to the database to get the ringtone URI
5. Set the obtained URI as the ringtone.
Core Implementation: In
Public class defaultringtonepreference extends ringtonepreference {
Private Static final string tag = "defaultringtonepreference ";
Public defaultringtonepreference (context, attributeset attrs ){
Super (context, attrs );
}
@ Override
Protected void onprepareringtonepickerintent (intent ringtonepickerintent ){
Super. onprepareringtonepickerintent (ringtonepickerintent );
/*
* Since this preference is for choosing the default ringtone, it
* Doesn't make sense to show a 'default' item.
*/
Ringtonepickerintent. putextra (ringtonemanager. extra_ringtone_show_default, false );
}
@ Override
Protected void onsaveringtone (URI ringtoneuri) {// note that this method is the core method for implementing ringtone settings
Ringtonemanager. setactualdefaultringtoneuri (getcontext (), getringtonetype (), ringtoneuri );
}
@ Override
Protected URI onrestoreringtone (){
Return ringtonemanager. getactualdefaultringtoneuri (getcontext (), getringtonetype ());
}
}
Defaultringtonepreference is an alertdialog generated after a single machine, but the defaultringtonepreference does not see any information to construct the dialog. Apparently, the code of ringtonepreference is built on the father's end:
Public class ringtonepreference extends preference implements preferencemanager. onactivityresultlistener {
Private Static final string tag = "ringtonepreference ";
Private int mringtonetype;
Private Boolean mshowdefault;
Private Boolean mshowsilent;
Private int mrequestcode;
Public ringtonepreference (context, attributeset attrs, int defstyle ){
Super (context, attrs, defstyle );
Typedarray A = context. obtainstyledattributes (attrs, Com. Android. Internal. R. styleable. ringtonepreference, defstyle, 0 );
Mringtonetype = A. getint (COM. Android. Internal. R. styleable. ringtonepreference_ringtonetype, ringtonemanager. type_ringtone );
Mshowdefault = A. getboolean (COM. Android. Internal. R. styleable. ringtonepreference_showdefault, true );
Mshowsilent = A. getboolean (COM. Android. Internal. R. styleable. ringtonepreference_showsilent, true );
A. Recycle ();
}
Public ringtonepreference (context, attributeset attrs ){
This (context, attrs, Com. Android. Internal. R. ATTR. ringtonepreferencestyle );
}
Public ringtonepreference (context ){
This (context, null );
}
/**
* Returns the sound type (s) that are shown in the picker.
*
* @ Return the sound type (s) that are shown in the picker.
* @ See # setringtonetype (INT)
*/
Public int getringtonetype (){
Return mringtonetype;
}
/**
* Sets the sound type (s) that are shown in the picker.
*
* @ Param type
* The sound type (s) that are shown in the picker.
* @ See ringtonemanager # extra_ringtone_type
*/
Public void setringtonetype (INT type ){
Mringtonetype = type;
}
/**
* Returns whether to a show an item for the default sound/ringtone.
*
* @ Return whether to show an item for the default sound/ringtone.
*/
Public Boolean getshowdefault (){
Return mshowdefault;
}
/**
* Sets whether to show an item for the default sound/ringtone. The default
* To use will be deduced from the sound type (s) being shown.
*
* @ Param showdefault
* Whether to show the default or not.
* @ See ringtonemanager # extra_ringtone_show_default
*/
Public void setshowdefault (Boolean showdefault ){
Mshowdefault = showdefault;
}
/**
* Returns whether to a show an item for 'silent '.
*
* @ Return whether to show an item for 'silent '.
*/
Public Boolean getshowsilent (){
Return mshowsilent;
}
/**
* Sets whether to show an item for 'silent '.
*
* @ Param showsilent
* Whether to show 'silent '.
* @ See ringtonemanager # extra_ringtone_show_silent
*/
Public void setshowsilent (Boolean showsilent ){
Mshowsilent = showsilent;
}
@ Override
Protected void onclick () {// note that there is a jump to the left processing of the ultringtonepreference standalone event
// Launch the ringtone picker
Intent intent = new intent (ringtonemanager. action_ringtone_picker );
Onprepareringtonepickerintent (intent );
Getpreferencemanager (). getactivity (). startactivityforresult (intent, mrequestcode );
}
/**
* Prepares the intent to launch the ringtone picker. This can be modified
* To adjust the parameters of the ringtone picker.
*
* @ Param ringtonepickerintent
* The ringtone picker intent that can be modified by putting
* Extras.
*/
Protected void onprepareringtonepickerintent (intent ringtonepickerintent ){
Ringtonepickerintent. putextra (ringtonemanager. extra_ringtone_existing_uri, onrestoreringtone ());
Ringtonepickerintent. putextra (ringtonemanager. extra_ringtone_show_default, mshowdefault );
If (mshowdefault ){
Ringtonepickerintent. putextra (ringtonemanager. extra_ringtone_default_uri, ringtonemanager. getdefaulturi (getringtonetype ()));
}
Ringtonepickerintent. putextra (ringtonemanager. extra_ringtone_show_silent, mshowsilent );
Ringtonepickerintent. putextra (ringtonemanager. extra_ringtone_type, mringtonetype );
}
/**
* Called when a ringtone is chosen.
* <P>
* By default, this saves the ringtone URI to the persistent storage as
* String.
*
* @ Param ringtoneuri
* The chosen ringtone's {@ link URI}. Can be null.
*/
Protected void onsaveringtone (URI ringtoneuri ){
Persiststring (ringtoneuri! = NULL? Ringtoneuri. tostring ():"");
}
/**
* Called when the chooser is about to be shown and the current ringtone
* Shoshould be marked. can return NULL to not mark any ringtone.
* <P>
* By default, this restores the previous ringtone URI from the persistent
* Storage.
*
* @ Return the ringtone to be marked as the current ringtone.
*/
Protected URI onrestoreringtone (){
Final string uristring = getpersistedstring (null );
Return! Textutils. isempty (uristring )? Uri. parse (uristring): NULL;
}
@ Override
Protected object ongetdefadefavalue (typedarray A, int index ){
Return A. getstring (INDEX );
}
@ Override
Protected void onsetinitialvalue (Boolean restorepersistedvalue, object defaultvalueobj ){
String defaultvalue = (string) defavaluvalueobj;
/*
* This method is normally to make sure the internal state and UI
* Matches either the persisted value or the default value. Since we
* Don't show the current value in the UI (until the dialog is opened)
* And we don't keep local state, if we are restoring the persisted
* Value we don't need to do anything.
*/
If (restorepersistedvalue ){
Return;
}
// If we are setting to the default value, we shoshould persist it.
If (! Textutils. isempty (defaultvalue )){
Onsaveringtone (URI. parse (defaultvalue ));
}
}
@ Override
Protected void onattachedtohierarchy (preferencemanager ){
Super. onattachedtohierarchy (preferencemanager );
Preferencemanager. registeronactivityresultlistener (this );
Mrequestcode = preferencemanager. getnextrequestcode ();
}
Public Boolean onactivityresult (INT requestcode, int resultcode, intent data) {// note that this method calls back the onsaveringtone method of the subclass.
If (requestcode = mrequestcode ){
If (Data! = NULL ){
Uri uri = data. getparcelableextra (ringtonemanager. extra_ringtone_picked_uri );
If (callchangelistener (Uri! = NULL? Uri. tostring ():"")){
Onsaveringtone (URI );
}
}
Return true;
}
Return false;
}
}
Jump to ringtonepickeractivity. Java
Explanation of the android ringtone setting process 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.