将mp3铃声设为特定联系人

时间:2013-11-12 10:45:09

标签: android-contacts

我想将mp3歌曲设置为特定联系人。在这里,我点击所选的上下文菜单项,在列表视图中显示联系人。我想将mp3歌曲设置为从listview中选择的联系人项目,但铃声未设置。这是我的代码:     公共类RingtoneActivity扩展了Activity {

Context ctx;
TextView songescape;

ListView listContacts;
Uri queryUri;
String[] projection;
String selection;
// crsorLoader crsorLoader;
Cursor cursor;
ListAdapter adapter;
String contactId;

protected static final int CONTEXTMENU_OPTION1 = 1;
protected static final int CONTEXTMENU_OPTION2 = 2;
protected static final int CONTEXTMENU_OPTION3 = 3;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ringtone);
    ctx = this;
    songescape = (TextView) findViewById(R.id.ringtonetext);
    registerForContextMenu(songescape);

}

public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    // Set title for the context menu
    menu.setHeaderTitle("Set as");

    // Add all the menu options

    menu.add(1, 1, CONTEXTMENU_OPTION1, "Default Ringtone");
    menu.add(1, 2, CONTEXTMENU_OPTION2, "Ringtone to specific Contact");
    menu.add(1, 3, CONTEXTMENU_OPTION3, "Ringtone to Contact Group");
}

public boolean onContextItemSelected(MenuItem item) {

    // Get extra info about list item that was long-pressed
    AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item
            .getMenuInfo();

    // Perform action according to selected item from context menu
    switch (item.getItemId()) {

    case CONTEXTMENU_OPTION1:
        // set as default ringtone
        setRingTone();

        break;

    case CONTEXTMENU_OPTION2:

        // 1:retrieve contacts from phone

        setContentView(R.layout.activity_main);
        listContacts = (ListView) findViewById(R.id.conactlist);

        Uri contactData = ContactsContract.Contacts.CONTENT_URI;
        // contactId = contactData.getLastPathSegment();



        // changes
        // you can retrieve all contacts as a cursor.

        cursor = getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                null, null, null);

        // now we have cusror with contacts and get diffrent value from
        // cusror.

        // final String[] contactid={ContactsContract.Contacts.};

        String[] from = { ContactsContract.Contacts.DISPLAY_NAME };
        int[] to = { android.R.id.text1 };

        // Above code,get all contact name and phone numbers from your phone
        // and sim.

        ListAdapter adapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1, cursor, from, to,
                CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        listContacts.setAdapter(adapter);
        // select contact ie onclick of contact from listview
        listContacts.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                // set mp3 to selected contact
                view.setSelected(true);
                view.setBackgroundColor(0xFF00FF00);

                //Toast.makeText(getApplicationContext(), "", 1000).show();
                // String str1 =
                // cursor.getString(cursor.getColumnIndexOrThrow("_id"));
                String str1 = cursor.getString(cursor
                        .getColumnIndexOrThrow("_id"));
                String str2 = cursor.getString(cursor
                        .getColumnIndexOrThrow("display_name"));
                Uri localUri = Uri.withAppendedPath(
                        ContactsContract.Contacts.CONTENT_URI, str1);
                ContentValues localContentValues = new ContentValues();

                localContentValues.put(
                        ContactsContract.Data.RAW_CONTACT_ID, 3440);
                localContentValues.put(
                        ContactsContract.Data.CUSTOM_RINGTONE, Environment
                                .getExternalStorageDirectory()
                                .getAbsolutePath()
                                + "cocktail.mp3");
                getContentResolver().update(localUri, localContentValues,
                        null, null);
                Toast.makeText(getApplicationContext(), "Saved!", 1000)
                        .show();

            }
        });

        break;

    case CONTEXTMENU_OPTION3:
        // Show message
        Toast.makeText(
                getApplicationContext(),
                "Option 3: ID " + menuInfo.id + ", position "
                        + menuInfo.position, Toast.LENGTH_SHORT).show();
        break;
    }

    return true;
}

public void setRingTone() {
    try {

        String SDCardRoot = (Environment.getExternalStorageDirectory() + "/cocktail.mp3");

        ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, SDCardRoot);
        values.put(MediaStore.MediaColumns.TITLE, "COCKTAIL RINGTONE");
        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
        values.put(MediaStore.MediaColumns.SIZE, 2030);
        values.put(MediaStore.Audio.Media.ARTIST, "");
        values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
        values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
        values.put(MediaStore.Audio.Media.IS_ALARM, true);
        values.put(MediaStore.Audio.Media.IS_MUSIC, false);

        Uri uri = MediaStore.Audio.Media.getContentUriForPath(SDCardRoot);
        getContentResolver().delete(uri,
                MediaStore.MediaColumns.DATA + "=\"" + SDCardRoot + "\"",
                null);
        Uri newUri = getContentResolver().insert(uri, values);

        RingtoneManager.setActualDefaultRingtoneUri(ctx,
                RingtoneManager.TYPE_RINGTONE, newUri);
        Toast.makeText(getApplicationContext(), "Song is set as Ringtone!",
                1000).show();

    } catch (Throwable t) {
        Toast.makeText(getApplicationContext(), "Exception!", 1000).show();
        Log.d("Exception", "catch exception" + t);
    }
}

}

0 个答案:

没有答案