【发布时间】:2017-04-16 10:16:20
【问题描述】:
在我的 android 应用程序中,我有一个导航抽屉,其中有几个按钮可以打开具有相关布局的不同片段。
其中一个按钮应该调用理论上应该执行电话拨号的功能。它似乎不起作用。什么都没有发生,导航抽屉刚刚关闭。我已经在清单中实现了这个权限,它应该为了权限而工作:
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
根据导航抽屉中的按钮事件执行代码片段的代码如下所示:
@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;
}
最后但并非最不重要的一点是,执行电话呼叫的功能如下所示;我必须指出,如果我没有检查包管理器是否已授予执行电话呼叫的访问权限,它会引发错误。这就是代码检查是否授予权限的原因:
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();
}
}
【问题讨论】:
-
你的问题到底是什么?更加详细一些。你得到了什么错误?实际发生了什么?
-
@IspasClaudiu 我在第四行文字中是否说过:什么都没有发生,就像根本没有调用任何函数一样。
-
从设置->应用管理器->你的应用中检查实际应用的权限。您可能必须手动授予权限。
-
对于
ACTION_DIAL,您不需要CALL_PHONE权限。
标签: java android phone-call