Android 三种拨号方式

Android 三种拨号方式

跳转拨号页面拨打电话

  Uri uri=Uri.parse("tel:"+ number);
  Intent intent =new Intent(Intent.ACTION_DIAL,uri);
  startActivity(intent);

直接拨打电话

      Uri uri=Uri.parse("tel:"+ number);
      Intent intent=new Intent(Intent.ACTION_CALL,uri);
      startActivity(intent);

通过反射拨打

try {
    
    
    // 首先拿到TelephonyManager
     TelephonyManager telMag = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
     Class<TelephonyManager> c = TelephonyManager.class;
     // 再去反射TelephonyManager里面的私有方法 getITelephony 得到 ITelephony对象
     Method mthEndCall = c.getDeclaredMethod("getITelephony", (Class[]) null);
     //允许访问私有方法
     mthEndCall.setAccessible(true);
     final Object obj = mthEndCall.invoke(telMag, (Object[]) null);
     // 再通过ITelephony对象去反射里面的call方法,并传入包名和需要拨打的电话号码
     Method mt = obj.getClass().getMethod("call", new Class[] {
    
     String.class, String.class });
     //允许访问私有方法
     mt.setAccessible(true);
     mt.invoke(obj, new Object[] {
    
     getPackageName() + "", number });}
     catch (Exception e){
    
    
            e.printStackTrace();
    }

猜你喜欢

转载自blog.csdn.net/qq_35892584/article/details/124102073