Android长按按钮上下文菜单

时间:2014-03-25 21:25:41

标签: android button menu android-context ringtone

我有这种方法来开始通知。但现在我想将它绑定到长按按钮上下文菜单。我该怎么办?

public void ringtone(){
    try {
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
        r.play();
     } catch (Exception e) {
         e.printStackTrace();

     }

1 个答案:

答案 0 :(得分:0)

@Override  
public boolean onCreateOptionsMenu(Menu menu) {  
    // Inflate the menu; this adds items to the action bar if it is present.  
    getMenuInflater().inflate(R.menu.main, menu);//Menu Resource, Menu  
    return true;  
}  
@Override  
public boolean onOptionsItemSelected(MenuItem item) {  
    switch (item.getItemId()) {  
        case R.id.List1:  
             //List1 is a button in menu.xml
             //it is the button you want to bind the method to
             //call the function ringtone() fom here
             ringtone();
          default:  
            return super.onOptionsItemSelected(item);  
    }  
}  

假设您已经创建了一个ContextMenu,并且在其中有一个带有

的按钮
android:id="@+id/List1"
相关问题