RingtoneManager类型的方法setActualDefaultRingtoneUri(Context,int,Uri)不适用于参数OnItemClickListener [英] The method setActualDefaultRingtoneUri(Context, int, Uri) in the type RingtoneManager is not applicable for the arguments OnItemClickListener
问题描述
我正在制作android应用程序,我想在列表视图项上单击以找到被单击项的ID,获取资源并将该文件设置为铃声.但是我遇到此错误:类型为new AdapterView.OnItemClickListener
)
I am making android application and I want when I click on list view item to find ID of clicked item, get resources and set that file as ringtone.However I get this error : The method setActualDefaultRingtoneUri(Context, int, Uri)
in the type RingtoneManager
is not applicable for the arguments (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;
}
}
});
推荐答案
您位于OnItemClickListener
界面内,因此this
将引用它.
You are within the OnItemClickListener
interface so this
will referrence to it.
为避免这种情况,只需将活动名称"放在此之前,或向getApplicationContext()询问上下文
To avoid this just place the Name of your Activity before this or ask getApplicationContext() for context
赞:
RingtoneManager.setActualDefaultRingtoneUri(
MainActivity.this, RingtoneManager.TYPE_RINGTONE, newUri);
或
RingtoneManager.setActualDefaultRingtoneUri(
getApplicationContext(), RingtoneManager.TYPE_RINGTONE, newUri);
对MediaPlayer.create(MainActivity.this, R.raw.aint);
这篇关于RingtoneManager类型的方法setActualDefaultRingtoneUri(Context,int,Uri)不适用于参数OnItemClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!