In my android app, I have a navigation drawer with a few buttons that open different fragments with associated layouts.
One of the buttons is supposed to call a function which in theory should perform a phone dial. It just doesn't seem to work. Nothing happens, and the navigation drawer just closes. I already implemented this permission in the manifest, which should do the work for the sake of permissions:
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
My code that executes pieces of code based on the button events in the navigation drawer looks like this:
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
android.app.FragmentManager fragmentmanager = getFragmentManager();
if (id == R.id.nav_forside) {
fragmentmanager.beginTransaction()
.replace(R.id.content_frame
, new MainFragment())
.commit();
} else if (id == R.id.nav_matif) {
fragmentmanager.beginTransaction()
.replace(R.id.content_frame
, new MatifFragment())
.commit();
} else if (id == R.id.nav_om) {
fragmentmanager.beginTransaction()
.replace(R.id.content_frame
, new OmFragment())
.commit();
} else if (id == R.id.nav_rk) {
fragmentmanager.beginTransaction()
.replace(R.id.content_frame
, new RkFragment())
.commit();
} else if (id == R.id.nav_bd) {
fragmentmanager.beginTransaction()
.replace(R.id.content_frame
, new BdFragment())
.commit();
} else if (id == R.id.nav_rf) {
fragmentmanager.beginTransaction()
.replace(R.id.content_frame
, new RfFragment())
.commit();
} else if (id == R.id.nav_kontakt_os) {
fragmentmanager.beginTransaction()
.replace(R.id.content_frame
, new KontaktOsFragment())
.commit();
} else if (id == R.id.nav_call_number) {
callNumber();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
And last but not least, the function the performs the phone call looks like this; I must point out that it threw an error if I didn't check if the package manager had granted access to perform the phone call. That is why the code checks if permission is granted:
public void callNumber () {
//Tries to call the phone number, and catches any errors that might be present
try {
//Checking if the the permission to call a phone is granted
if (ActivityCompat.checkSelfPermission(MainActivity.this.getApplicationContext(), Manifest.permission.CALL_PHONE) ==
PackageManager.PERMISSION_GRANTED) {
Intent i = new Intent(Intent.ACTION_DIAL);
String p = "tel:12345678";
i.setData(Uri.parse(p));
startActivity(i);
}
} catch (Exception exLog) {
//Catches error that prevents phone from calling, and expresses itself in form of a alertdialog
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Opkaldet kunne ikke gennemføres");
alertDialog.setMessage(exLog.toString());
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
}