【问题标题】:how to end the call by shaking the phone如何通过摇晃手机结束通话
【发布时间】:2013-03-24 16:27:29
【问题描述】:

我正在做一个 Android 应用程序,它允许不同能力的人通过摇动电话拨打预定义的号码寻求帮助。当摇晃呼叫特定号码时,我进行了自定义。但是,我不明白:

如何通过向同一方向摇晃来关闭通话? 双摇怎么去另一个电话等等..

public void onSensorChanged(SensorEvent event) {

    if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER || event.values.length < 2)
          return;

    currentTime = System.currentTimeMillis();

    if ((currentTime - lastUpdate) > 100) {
        long diffTime = (currentTime - lastUpdate);
        lastUpdate = currentTime;

        current_x = event.values[DATA_X];
        current_y = event.values[DATA_Y];
        current_z = event.values[DATA_Z];

        currenForce = Math.abs(current_x+current_y+current_z - last_x - last_y - last_z) / diffTime * 10000;

        if (currenForce > FORCE_THRESHOLD && inCall ==0) {


            if (sp.contains("number") && sp.getString("number","").length() > 5 ){

            // Device has been shaken now go on and do something
            // you could now inform the parent activity ...
            String phoneNumber = sp.getString("Number", "1800");
            android.net.Uri uri = android.net.Uri.fromParts("tel", phoneNumber, null);


            try{
            inCall =1;  
            android.content.Intent callIntent = new android.content.Intent(android.content.Intent.ACTION_CALL, uri);
            callIntent.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(callIntent);
            }catch(Exception e){
                inCall =0;
                e.printStackTrace();
            }

            }else{
                Toast.makeText(this, "Please Configure number first", Toast.LENGTH_LONG);
            }



        }else{

             try{   
                 Toast.makeText(getBaseContext(), "Trying to End Call", Toast.LENGTH_LONG);
                 // Send Key For End Call
                 new KeyEvent(KeyEvent.ACTION_DOWN,
                       KeyEvent.KEYCODE_ENDCALL );

                 inCall =0;
             }catch(Exception e){

                 e.printStackTrace();
             }

        }

        last_x = current_x;
        last_y = current_y;
        last_z = current_z;

    }

提前致谢

【问题讨论】:

    标签: android handshaking


    【解决方案1】:

    我尝试了一些技巧来发送结束通话点击,但没有奏效。

    然而,基于 this linkthis link ,下一个代码将导致电话挂起,虽然我认为这是一个黑客:

    try {
    final String serviceManagerName = "android.os.ServiceManager";
    final String serviceManagerNativeName = "android.os.ServiceManagerNative";
    final String telephonyName = "com.android.internal.telephony.ITelephony";
    
    Class<?> telephonyClass;
    Class<?> telephonyStubClass;
    Class<?> serviceManagerClass;
    Class<?> serviceManagerNativeClass;
    Method telephonyEndCall;
    Object telephonyObject;
    Object serviceManagerObject;
    
    telephonyClass = Class.forName(telephonyName);
    telephonyStubClass = telephonyClass.getClasses()[0];
    serviceManagerClass = Class.forName(serviceManagerName);
    serviceManagerNativeClass = Class.forName(serviceManagerNativeName);
    
    final Method getService = serviceManagerClass.getMethod("getService", String.class);
    
    final Method tempInterfaceMethod = serviceManagerNativeClass.getMethod("asInterface", IBinder.class);
    
    final Binder tmpBinder = new Binder();
    tmpBinder.attachInterface(null, "fake");
    
    serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder);
    final IBinder retbinder = (IBinder) getService.invoke(serviceManagerObject, "phone");
    final Method serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder.class);
    
    telephonyObject = serviceMethod.invoke(null, retbinder);
    // telephonyCall = telephonyClass.getMethod("call", String.class);
    telephonyEndCall = telephonyClass.getMethod("endCall");
    // telephonyAnswerCall = telephonyClass.getMethod("answerRingingCall");
    
    telephonyEndCall.invoke(telephonyObject);
    
    } catch (final Exception e) {
    e.printStackTrace();
    }
    

    编辑:此代码不适用于所有设备和 Android 版本(例如 Nexus 5x 和 Android 8.1)。唯一安全的解决方案是使用官方 API,遗憾的是 Android P 提供了该 API。我写过这个 here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多