如何设置短信铃声

时间:2014-07-27 11:55:11

标签: java android

我的Android应用程序遇到了一个小问题。我在我的应用程序中有一些mp3文件,我希望能够设置为默认铃声或notification.app没有问题设置呼叫铃声和alaram铃声但设置短信铃声不起作用 通话铃声:

public void onClick(View arg0) {
        // TODO Auto-generated method stub
        btn3.setBackgroundResource(R.drawable.ok);
        File localFile1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/bomb/");
        Uri mUri = Uri.parse("android.resource://ir.bazisong/"+R.raw.bababanana);
        ContentResolver mCr = bombsound2.this.getContentResolver();
        AssetFileDescriptor soundFile;
        try {
               soundFile= mCr.openAssetFileDescriptor(mUri, "r");
           } catch (FileNotFoundException e) {
               soundFile=null;   
           }

           try {
              byte[] readData = new byte[1024];
              FileInputStream fis = soundFile.createInputStream();
              FileOutputStream fos = new FileOutputStream(localFile1);
              int i = fis.read(readData);

              while (i != -1) {
                fos.write(readData, 0, i);
                i = fis.read(readData);
              }

              fos.close();
           } catch (IOException io) {
           }


           ContentValues localContentValues = new ContentValues();
           localContentValues.put("_data", localFile1.getAbsolutePath());
           localContentValues.put("title", "bomb");
           localContentValues.put("mime_type", "audio/ogg");
           localContentValues.put("artist", "cssounds ");
           localContentValues.put("is_ringtone", Boolean.valueOf(true));
           localContentValues.put("is_notification", Boolean.valueOf(false));
           localContentValues.put("is_alarm", Boolean.valueOf(false));
           localContentValues.put("is_music", Boolean.valueOf(false));
           Uri localUri1 = MediaStore.Audio.Media.getContentUriForPath(localFile1.getAbsolutePath());
           Uri localUri2 = bombsound2.this.getContentResolver().insert(localUri1, localContentValues);
           RingtoneManager.setActualDefaultRingtoneUri(bombsound2.this.getApplication(), 1, localUri2);
           ((Vibrator)bombsound2.this.getSystemService("vibrator")).vibrate(50L);
           Toast.makeText(bombsound2.this.getApplicationContext(), "زنگ تماس انتخاب شد", 1).show();

    }
});

短信铃声

@Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        btn4.setBackgroundResource(R.drawable.ok);

        File localFile1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/bomb/");
        Uri mUri = Uri.parse("android.resource://ir.bazisong/"+R.raw.bababanana);
        ContentResolver mCr = bombsound2.this.getContentResolver();
        AssetFileDescriptor soundFile;
        try {
               soundFile= mCr.openAssetFileDescriptor(mUri, "r");
           } catch (FileNotFoundException e) {
               soundFile=null;   
           }

           try {
              byte[] readData = new byte[1024];
              FileInputStream fis = soundFile.createInputStream();
              FileOutputStream fos = new FileOutputStream(localFile1);
              int i = fis.read(readData);

              while (i != -1) {
                fos.write(readData, 0, i);
                i = fis.read(readData);
              }

              fos.close();
           } catch (IOException io) {
           }


           ContentValues localContentValues = new ContentValues();
           localContentValues.put("_data", localFile1.getAbsolutePath());
           localContentValues.put("title", "bomb");
           localContentValues.put("mime_type", "audio/ogg");
           localContentValues.put("artist", "cssounds ");
           localContentValues.put("is_ringtone", Boolean.valueOf(false));
           localContentValues.put("is_notification", Boolean.valueOf(true));
           localContentValues.put("is_alarm", Boolean.valueOf(false));
           localContentValues.put("is_music", Boolean.valueOf(false));
           Uri localUri1 = MediaStore.Audio.Media.getContentUriForPath(localFile1.getAbsolutePath());
           Uri localUri2 = bombsound2.this.getContentResolver().insert(localUri1, localContentValues);
           RingtoneManager.setActualDefaultRingtoneUri(bombsound2.this.getApplication(), 1, localUri2);
           ((Vibrator)bombsound2.this.getSystemService("vibrator")).vibrate(50L);
           Toast.makeText(bombsound2.this.getApplicationContext(), "زنگ پیام انتخاب شد", 1).show();



    }
});

1 个答案:

答案 0 :(得分:0)

解决方案是将原始文件夹中的声音复制到SD卡中,然后执行以下操作

 File k = new File(path, filename);

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "TwiAppclip");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");
values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);

Uri uri = MediaStore.Audio.Media.getContentUriForPath(k
   .getAbsolutePath());
 //do a delete here before inserting
 Uri newUri = getApplicationContext().getContentResolver().insert(uri, values);

  RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(),
  RingtoneManager.TYPE_RINGTONE, newUri);
相关问题