gpt4 book ai didi

java - 怎么设置短信铃声

转载 作者:行者123 更新时间:2023-11-29 08:46:37 24 4
gpt4 key购买 nike

我的 android 应用程序有一个小问题。我的应用程序中有一些 mp3 文件,我希望能够将其设置为默认铃声或通知。应用程序设置调用铃声和警报铃声没有问题,但设置短信铃声不起作用来电铃声:

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();



}
});

最佳答案

解决方案是将原始文件夹中的声音复制到 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);

关于java - 怎么设置短信铃声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24980670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com