我正在使用下一个代码:
public void PhoneCallEnd(Context context) {
try {
// Get the boring old TelephonyManager
TelephonyManager telephonyManager =
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
// Get the getITelephony() method
Class classTelephony = Class.forName(telephonyManager.getClass().getName());
Method methodGetITelephony = classTelephony.getDeclaredMethod("getITelephony");
// Ignore that the method is supposed to be private
methodGetITelephony.setAccessible(true);
// Invoke getITelephony() to get the ITelephony interface
Object telephonyInterface = methodGetITelephony.invoke(telephonyManager);
// Get the endCall method from ITelephony
Class telephonyInterfaceClass = Class.forName(telephonyInterface.getClass().getName());
Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("endCall");
// Invoke endCall()
methodEndCall.invoke(telephonyInterface);
} catch (Exception ex) { // Many things can go wrong with reflection calls
String error=ex.toString();
Toast.makeText(context, "error: "+ error , Toast.LENGTH_LONG).show();
txtHome.setText("error: "+ error);//cambio el contenido del TextView
}
}
问题是我得到了下一个错误" java.lang.reflect.invocationTargetException"
行中的aplication崩溃" methodEndCall.invoke(telephonyInterface);"
你能否告诉我拒绝当前通话的正确方法?
答案 0 :(得分:0)
您需要在清单中定义权限
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
对于大于或等于23的sdk版本,您需要运行时权限。在您的活动中,您可以获得此类
的运行时权限public static final int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this,
Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_PHONE_STATE)) {
// Show an explanation for read phone state
} else
if(ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CALL_PHONE)){
// Show an explanation for call phone
}else {
// No explanation needed
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_PHONE_STATE,Manifest.permission.CALL_PHONE},
MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
// result of the request.
}
}else{
//permission already available
//do the required task here
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[]
grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_PHONE_STATE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission granted
// do the required task here
} else {
// permission denied
}
return;
}
}
}
答案 1 :(得分:0)
@shiriyas
我修正了错误。
感谢您的帮助。
问题在于我正在检查onCreate方法中的perms。
当我在PhoneCallEnd方法中检查perms时问题已解决
抱歉,这是我的第一个应用程序,你可以看到我没有很好地使用这些工具=(