糖尿病康复,内容丰富有趣,生活中的好帮手!
糖尿病康复 > android自定义闹钟铃声 如何在android中设置自定义闹钟铃声

android自定义闹钟铃声 如何在android中设置自定义闹钟铃声

时间:2022-06-04 01:51:35

相关推荐

android自定义闹钟铃声 如何在android中设置自定义闹钟铃声

试试这个

添加任何.MP3文件中的原始文件夹位置该文件的名称

public void setAlarm() {

File file = new File(Environment.getExternalStorageDirectory(),

"/Your Directory Name");

if (!file.exists()) {

file.mkdirs();

}

String path = Environment.getExternalStorageDirectory()

.getAbsolutePath() + "/Your Directory Name";

File f = new File(path + "/", filename + ".mp3");

Uri mUri = Uri.parse("android.resource://" + getContext().getPackageName() + "/raw/" + filename);

ContentResolver mCr = getContext().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(f);

int i = fis.read(readData);

while (i != -1) {

fos.write(readData, 0, i);

i = fis.read(readData);

}

fos.close();

} catch (IOException io) {

}

ContentValues values = new ContentValues();

values.put(MediaStore.MediaColumns.DATA, f.getAbsolutePath());

values.put(MediaStore.MediaColumns.TITLE, filename);

values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");

values.put(MediaStore.MediaColumns.SIZE, f.length());

values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);

values.put(MediaStore.Audio.Media.IS_ALARM, true);

Uri uri = MediaStore.Audio.Media.getContentUriForPath(f.getAbsolutePath());

getContext().getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + f.getAbsolutePath() + "\"", null);

Uri newUri = mCr.insert(uri, values);

try {

RingtoneManager.setActualDefaultRingtoneUri(getContext(),

RingtoneManager.TYPE_ALARM, newUri);

Settings.System.putString(mCr, Settings.System.ALARM_ALERT,

newUri.toString());

Toast.makeText(getContext(), "Done", Toast.LENGTH_SHORT).show();

} catch (Throwable t) {

}

}

如果觉得《android自定义闹钟铃声 如何在android中设置自定义闹钟铃声》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。