Android Programming Tutorials

(Romina) #1
Asking Permission to Place a Call

@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(getApplication())
.inflate(R.menu.option_detail, menu);

return(super.onCreateOptionsMenu(menu));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()==R.id.call) {
String toDial="tel:"+phone.getText().toString();

if (toDial.length()> 4 ) {
startActivity(new Intent(Intent.ACTION_DIAL,
Uri.parse(toDial)));
}
}

return(super.onOptionsItemSelected(item));
}

Note that you will need to add a number of imports (Intent, Menu,


MenuInflater, MenuItem, and Uri) to get this to compile cleanly.


In the new code, we check to see if there is a phone number. If so, we wrap


the phone number in a tel: Uri, then put that in an ACTION_DIAL Intent and


start an activity on that Intent. This puts the phone number in the dialer.


If you rebuild and reinstall the application and try out the new menu choice


on some restaurant with a phone number, you will see the Dialer appear:


345
Free download pdf