0

I'm doing an Android application that is enable differently abled people is alow to dial a pre-defined number for help by shaking the phone. I did a customization when one shake calling a specific number. But, I don't get any idea:

How I turn call off by shaking in the same direction? How does double shake go another call and so on..

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;

    }

Thanks in advance

Franklin Hirata
  • 290
  • 2
  • 12
anshad
  • 824
  • 8
  • 19

1 Answers1

0

i have tried some tricks to dispatch the end call clicks, but none worked.

however, based on this link and this link , the next code will cause the phone call to hang, though i treat this as a hack :

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

EDIT: This code doesn't work on all devices and Android versions (Example Nexus 5x with Android 8.1). The only safe solution is to use official API, which is sadly available from Android P. I wrote about this here.

android developer
  • 114,585
  • 152
  • 739
  • 1,270