为指定联系人设定指定铃声

来源:互联网 发布:怎样卸载苹果软件 编辑:程序博客网 时间:2024/05/16 19:32

针对android4.1代码有效

public void onRingtoneChanged(TextView btnRingtoneContent,boolean isOnclick) {

// TODO Auto-generated method stub
mBtnRingtone = btnRingtoneContent;
if(isOnclick){
doPickRingtone();
}

}

private void doPickRingtone() {
        Log.v("sjb","doPickRingtone.....");
        Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
        // Allow user to pick 'Default'
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
        // Show only ringtones
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);
        // Don't show 'Silent'
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);


        Uri ringtoneUri;
        if (mCustomRingtone != null) {
            ringtoneUri = Uri.parse(mCustomRingtone);
        } else {
            // Otherwise pick default ringtone Uri so that something is selected.
            ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
        }


        // Put checkmark next to the current ringtone for this contact
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, ringtoneUri);


        // Launch!
        startActivityForResult(intent, REQUEST_CODE_PICK_RINGTONE);
    }

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (mStatus == Status.SUB_ACTIVITY) {
            mStatus = Status.EDITING;
        }


        // See if the photo selection handler handles this result.
        if (mCurrentPhotoHandler != null && mCurrentPhotoHandler.handlePhotoActivityResult(
                requestCode, resultCode, data)) {
            return;
        }


        switch (requestCode) {

           case REQUEST_CODE_PICK_RINGTONE: {
                Uri pickedUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
                handleRingtonePicked(pickedUri);
                break;
            }
                // The following lines are provided and maintained by Mediatek
                // Inc.
        }
    }

private void handleRingtonePicked(Uri pickedUri) {
        if (pickedUri == null || RingtoneManager.isDefault(pickedUri)) {
            mCustomRingtone = null;
        } else {
            mCustomRingtone = pickedUri.toString();
        }
        Uri ringtoneContactUri = mLookupUri;
        //if (mContactData != null) {
           // ringtoneContactUri = mContactData.getLookupUri();
        //}
        Intent intent = ContactSaveService.createSetRingtone(
                mContext, ringtoneContactUri, mCustomRingtone);
        mContext.startService(intent);
updateView();
    }

private void updateView() {
       if (mCustomRingtone == null) {
        if(mBtnRingtone != null) 
        mBtnRingtone.setText(getString(R.string.default_ringtone));
       } else {
           Uri ringtoneUri = Uri.parse(mCustomRingtone);
           Ringtone ringtone = RingtoneManager.getRingtone(mContext, ringtoneUri);
           if (ringtone == null) {
               Log.w(TAG, "ringtone's URI doesn't resolve to a Ringtone");
               return;
           }
           mBtnRingtone.setText(ringtone.getTitle(mContext));
       }

   }