我有这种方法来开始通知。但现在我想将它绑定到长按按钮上下文菜单。我该怎么办?
public void ringtone(){
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
答案 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"