我正在尝试通过我的应用拨打电话。但是,每次崩溃时,logcat上都不会显示错误。我在清单中获得许可,并在运行时检查它。
public void call(String number){
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+number));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(callIntent);
}
答案 0 :(得分:0)
别忘了在清单中添加相关权限:
<uses-permission android:name="android.permission.CALL_PHONE" />
意图本身仅仅是描述某些内容的对象。它什么也没做。
public void call(String number){
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + number));
context.startActivity(intent);
}
答案 1 :(得分:0)
您确定上下文不为null吗?你应该做这样的事情。 在通话活动中进行这些更改
private static final int REQUEST_CALL_PHONE_PERMISSION = 100;
if( isCallPhonePermissionGranted() ){
call("<Number>");
} else {
call("<Number>");
}
private void requestCallPermission(){ 最终的String []权限=新的String [] {Manifest.permission.CALL_PHONE}; ActivityCompat.requestPermissions(this,权限,REQUEST_CALL_PHONE_PERMISSION); }
private boolean isCallPhonePermissionGranted() {
return ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
== PackageManager.PERMISSION_GRANTED;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults) {
if (requestCode != REQUEST_CALL_PHONE_PERMISSION) {
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
return;
}
if (grantResults.length != 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
call("<Number>");
return;
}
}
public void call(String number){
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+number));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(callIntent);
}
最后将此权限添加到Android Manifest.xml
<uses-permission android:name="android.permission.CALL_PHONE" />