我正在制作Android应用程序,我希望当我点击列表视图项以查找被点击项目的ID,获取资源并将该文件设置为铃声时。但是我收到此错误:类型中的方法setActualDefaultRingtoneUri(Context, int, Uri)
RingtoneManager
不适用于参数(new AdapterView.OnItemClickListener
)
我该如何解决这个问题?
listv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String selectedName = ((ListView) view.findViewById(R.id.listView1)).getContext()
.toString();
AssetManager assetManager = getAssets();
File file = new File(Environment.getExternalStorageDirectory(),
"/myRingtonFolder/Audio/");
if (!file.exists()) {
file.mkdirs();
}
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/MyRingtones/Audio/";
File out = new File(path + "/", selectedName);
if (!out.exists()) {
try {
copyFile(assetManager, "Ringtone.mp3", out);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, out.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "airh");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.MediaColumns.SIZE, out.length());
values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
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, true);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(out.getAbsolutePath());
ContentResolver mCr = getContentResolver();
Uri newUri = mCr.insert(uri, values);
try {
RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE,
newUri);
Settings.System.putString(mCr, Settings.System.RINGTONE,
newUri.toString());
} catch (Throwable t) {
//TODO Handle exception
}
switch (position) {
case 0:
if (mp != null) {
mp.release();
mp = null;
}
mp = MediaPlayer.create(Context.this,
R.raw.aint);
mp.start();
break;
case 1:
if (mp != null) {
mp.release();
mp = null;
}
mp = MediaPlayer.create(Context.this,
R.raw.airh);
mp.start();
break;
}
}
});
答案 0 :(得分:2)
您位于OnItemClickListener
界面内,因此this
会引用它。
为了避免这种情况,只需在此之前放置您的活动名称,或者向上下文询问getApplicationContext()
像:
RingtoneManager.setActualDefaultRingtoneUri(
MainActivity.this, RingtoneManager.TYPE_RINGTONE, newUri);
或
RingtoneManager.setActualDefaultRingtoneUri(
getApplicationContext(), RingtoneManager.TYPE_RINGTONE, newUri);
对MediaPlayer.create(MainActivity.this, R.raw.aint);