我在这里有两个按钮真正简单的活动。当你按下它们时它会发出声音。
当我按住第一个按钮时,它会弹出一个上下文菜单,询问用户是否要将声音保存为铃声或通知。这在第一个按钮上完美运行。
按下时会播放第二个按钮的声音。长按它会调出上下文菜单....但是它将第一个声音文件保存为铃声/通知而不是第二个...
有人能够对第二个上下文菜单无法正常工作的原因有所了解吗?
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,但也没用。有什么建议吗?
答案 0 :(得分:2)
关于你的代码的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;
}
}
你不需要额外的功能12,功能11,功能21,功能22 ...你可以概括它们......但我把它留给你。
答案 1 :(得分:0)
检查评论,我留在下面......
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
long SoundButton11 = info.id; //THIS IS THE UNUSED VARIABLE!
switch (item.getItemId()) {
case MENU_RINGTONE:
if (savering(R.raw.schwing)){ //this should change based on what button was pressed...
// 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)){ //this should change based on what button was pressed...
// 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;
}
我认为这是您想要的行... 之前这样做确保SoundButton1(& 2)
是实例变量,因此可以在Activity的所有功能中访问它们
//UNTESTED CODE!
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
long SoundButton11 = info.id; //THIS IS THE UNUSED VARIABLE!
int resId = (SoundButton11 == SoundButton1.getId() )? R.raw.schwing : R.raw.rolf;
switch (item.getItemId()) {
case MENU_RINGTONE:
if (savering(resId)){ //use resId instead...
// Code if successful
Toast.makeText(this, "Saved as Ringtone", Toast.LENGTH_SHORT).show();
}
else
.....