我这里有两个按钮的真正简单的 Activity 。当您按下它们时,它会发出声音。
当我按住第一个按钮时,它会弹出一个上下文菜单,询问用户是否要将声音保存为铃声或通知。这对第一个按钮非常有效。
第二个按钮的声音在按下时播放。当长按它会弹出上下文菜单....但它会将第一个声音文件保存为铃声/通知而不是第二个...
有人可以解释为什么第二个上下文菜单无法正常工作吗?
package com.my.app;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
public class One extends Activity implements OnClickListener{
private SoundManager mSoundManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.one);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.blah);
mSoundManager.addSound(2, R.raw.rofl);
//BUTTONS PLAY SOUND WHEN PRESSED
View SoundButton1 = findViewById(R.id.Sound1);
SoundButton1.setOnClickListener(this);
View SoundButton2 = findViewById(R.id.Sound2);
SoundButton2.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.Sound1:
mSoundManager.playSound(1);
break;
case R.id.Sound2:
mSoundManager.playSound(2);
break;
}
//WHEN LONG PRESSED BUTTONS BRING UP CONTEXT MENU FOR SAVE AS RINGTONE OR NOTIFICATION
Button SoundButton11 = (Button) findViewById(R.id.Sound1);
registerForContextMenu(SoundButton11);
Button SoundButton22 = (Button) findViewById(R.id.Sound2);
registerForContextMenu(SoundButton22);
}
//CONTEXT MENU FOR BUTTON 1
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Save as...");
menu.add(0, v.getId(), 0, "Ringtone");
menu.add(0, v.getId(), 0, "Notification");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Ringtone"){function1(item.getItemId());}
else if(item.getTitle()=="Notification"){function2(item.getItemId());}
else {return false;}
return true;
}
public void function1(int id){
if (savering(R.raw.blah)){
// Code if successful
Toast.makeText(this, "Saved as Ringtone", Toast.LENGTH_SHORT).show();
}
else
{
// Code if unsuccessful
Toast.makeText(this, "Failed - Check your SDCard", Toast.LENGTH_SHORT).show();
}
}
public void function2(int id){
if (savenot(R.raw.blah)){
// Code if successful
Toast.makeText(this, "Saved as Notification", Toast.LENGTH_SHORT).show();
}
else
{
// Code if unsuccessful
Toast.makeText(this, "Failed - Check your SDCard", Toast.LENGTH_SHORT).show();
}
//CONTEXT MENU FOR BUTTON 2
}
public void onCreateContextMenu1(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Save as...");
menu.add(0, v.getId(), 0, "Ringtone");
menu.add(0, v.getId(), 0, "Notification");
}
public boolean onContextItemSelected1(MenuItem item) {
if(item.getTitle()=="Ringtone"){function11(item.getItemId());}
else if(item.getTitle()=="Notification"){function21(item.getItemId());}
else {return false;}
return true;
}
public void function11(int id){
if (savering(R.raw.rofl)){
// Code if successful
Toast.makeText(this, "Saved as Ringtone", Toast.LENGTH_SHORT).show();
}
else
{
// Code if unsuccessful
Toast.makeText(this, "Failed - Check your SDCard", Toast.LENGTH_SHORT).show();
}
}
public void function21(int id){
if (savenot(R.raw.rofl)){
// Code if successful
Toast.makeText(this, "Saved as Notification", Toast.LENGTH_SHORT).show();
}
else
{
// Code if unsuccessful
Toast.makeText(this, "Failed - Check your SDCard", Toast.LENGTH_SHORT).show();
}
}
public boolean savering(int ressound){
byte[] buffer=null;
InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
int size=0;
try {
size = fIn.available();
buffer = new byte[size];
fIn.read(buffer);
fIn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
}
String path="/sdcard/media/audio/ringtones/";
String filename="HahaSound"+".ogg";
boolean exists = (new File(path)).exists();
if (!exists){new File(path).mkdirs();}
FileOutputStream save;
try {
save = new FileOutputStream(path+filename);
save.write(buffer);
save.flush();
save.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
}
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));
File k = new File(path, filename);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "HahaSound");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
//Insert it into the database
this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);
return true;
}
public boolean savenot(int ressound){
byte[] buffer=null;
InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
int size=0;
try {
size = fIn.available();
buffer = new byte[size];
fIn.read(buffer);
fIn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
}
String path="/sdcard/media/audio/notifications/";
String filename="HahaSound"+".ogg";
boolean exists = (new File(path)).exists();
if (!exists){new File(path).mkdirs();}
FileOutputStream save;
try {
save = new FileOutputStream(path+filename);
save.write(buffer);
save.flush();
save.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
}
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));
File k = new File(path, filename);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "HahaSoundSound");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
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, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
//Insert it into the database
this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);
return true;
}
}
自 STOLE 回复后更新-
//BUTTON 1
View SoundButton1 = findViewById(R.id.Sound1);
SoundButton1.setOnClickListener(this);
View SoundButton2 = findViewById(R.id.Sound2);
SoundButton2.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.Sound1:
mSoundManager.playSound(1);
break;
case R.id.Sound2:
mSoundManager.playSound(2);
break;
}
Button SoundButton11 = (Button) findViewById(R.id.Sound1);
registerForContextMenu(SoundButton11);
Button SoundButton22 = (Button) findViewById(R.id.Sound2);
registerForContextMenu(SoundButton22);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Save as...");
menu.add(0, MENU_RINGTONE, 0, "Ringtone");
menu.add(0, MENU_NOTIFICATION, 0, "Notification");
}
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
long SoundButton11 = info.id;
switch (item.getItemId()) {
case MENU_RINGTONE:
if (savering(R.raw.schwing)){
// Code if successful
Toast.makeText(this, "Saved as Ringtone", Toast.LENGTH_SHORT).show();
}
else
{
// Code if unsuccessful
Toast.makeText(this, "Failed - Check your SDCard", Toast.LENGTH_SHORT).show();
}
break;
case MENU_NOTIFICATION:
if (savenot(R.raw.schwing)){
// Code if successful
Toast.makeText(this, "Saved as Notification", Toast.LENGTH_SHORT).show();
}
else
{
// Code if unsuccessful
Toast.makeText(this, "Failed - Check your SDCard", Toast.LENGTH_SHORT).show();
}
break;
}
return false;
}
这是自阅读您上次回复后得到的。在我尝试移动到下一个按钮之前,我只是想让它在第一个按钮上工作。但是使用您的代码,我在 SoundButton11 下收到警告“从未读取局部变量 SoundButton11” 我很困惑,因为我有...
Button SoundButton11 = (Button) findViewById(R.id.Sound1);
registerForContextMenu(SoundButton11);
我也试过 Sound1 也没有用。有什么建议吗?
最佳答案
2 关于您的代码的事情....
1) 没有名为 onCreateContextMenu1
的函数,所以您没有覆盖它...所以第一个 onCreateContextMenu
被调用。
2)
menu.add(0, v.getId(), 0, "Ringtone");
menu.add(0, v.getId(), 0, "Notification");
您正在为两个菜单项(上下文)分配相同的 ID……理想情况下,它们必须不同。你还会如何识别它们。我很惊讶它实际上为你工作,使用标题。
这是你应该做的...
final int MENU_RINGTONE = 0;
final int MENU_NOTIFICATION = 1;
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, MENU_RINGTONE, 0, "Ringtone");
menu.add(0, MENU_NOTIFICATION, 0, "Notification");
}
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
long buttonId = info.id;
switch (item.getItemId()) {
case MENU_RINGTONE:
function1(buttonId);
break;
case MENU_NOTIFICATION:
function2(buttonId);
break;
}
}
您不需要额外的 function12、funtion11、function21、function22...您可以概括它们...但我将其留给您。
关于java - Android:使用多个上下文菜单时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3315269/