【问题标题】:android correct way to end call in the version 6.0android在6.0版本中结束通话的正确方法
【发布时间】:2017-09-12 18:00:19
【问题描述】:

我正在使用下一个代码:

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”

“methodEndCall.invoke(telephonyInterface);”行中的应用程序崩溃

你能告诉我拒绝当前通话的正确方法吗?

【问题讨论】:

  • 你能显示你的堆栈跟踪吗?
  • 我怎样才能看到它?
  • 复制 java.lang.reflect.invocationTargetException 的整个错误日志
  • 错误是java.lang.SecurityException:用户10123和当前进程都没有android.permission.CALL_PHONE。

标签: android phone-call


【解决方案1】:

@shiriyas

我修正了错误。

谢谢你帮助我。

问题是我在 onCreate 方法中检查 perms。

当我在 PhoneCallEnd 方法中检查 perms 时,问题已解决

抱歉,这是我的第一个应用程序,如您所见,我没有很好地使用这些工具 =(

【讨论】:

    【解决方案2】:

    您需要在清单中定义权限

    <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;
        }
    
      }
    }
    

    【讨论】:

    • 查看更新答案以了解 Marshmallow 及更高版本的运行时权限
    • 在运行时查看两个权限的更新答案
    猜你喜欢
    • 2012-05-19
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    相关资源
    最近更新 更多