操纵(接听/拒绝/终止)Android上的来电

时间:2013-07-31 06:02:51

标签: android call

我必须在android上操作传入的调用我得到了这段代码:

public class Metdllamadas extends Activity{

private static final String TAG = "Debug";
Integer Metodo1=0;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_metdllamadas);
    Bundle extras = getIntent().getExtras();
    try
    {
    Metodo1 = Integer.parseInt(extras.getString("Metodo"));

    }catch(NullPointerException npe)
    {
        Metodo1=2;


    }
    Toast.makeText(getApplicationContext(),Metodo1.toString(), Toast.LENGTH_LONG).show();
 TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

try {
    //String serviceManagerName = "android.os.IServiceManager";
    String serviceManagerName = "android.os.ServiceManager";
    String serviceManagerNativeName = "android.os.ServiceManagerNative";
    String telephonyName = "com.android.internal.telephony.ITelephony";

    Class telephonyClass;
    Class telephonyStubClass;
    Class serviceManagerClass;
    Class serviceManagerStubClass;
    Class serviceManagerNativeClass;
    Class serviceManagerNativeStubClass;

    Method telephonyCall;
    Method telephonyEndCall;
    Method telephonyAnswerCall;
    Method getDefault;

    Method[] temps;
    Constructor[] serviceManagerConstructor;

    // Method getService;
    Object telephonyObject;
    Object serviceManagerObject;

    telephonyClass = Class.forName(telephonyName);
    telephonyStubClass = telephonyClass.getClasses()[0];
    serviceManagerClass = Class.forName(serviceManagerName);
    serviceManagerNativeClass = Class.forName(serviceManagerNativeName);

    Method getService = // getDefaults[29];
    serviceManagerClass.getMethod("getService", String.class);

    Method tempInterfaceMethod = serviceManagerNativeClass.getMethod("asInterface", IBinder.class);

    Binder tmpBinder = new Binder();
    tmpBinder.attachInterface(null, "fake");

    serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder);
    IBinder retbinder = (IBinder) getService.invoke(serviceManagerObject, "phone");
    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");

    if (Metodo1==2)
    {
        Toast.makeText(getApplicationContext(), "Terminando Llamada", Toast.LENGTH_LONG).show();
    telephonyEndCall.invoke(telephonyObject);
    }

} catch (Exception e) {
    e.printStackTrace();
    // Log.error(DialerActivity.this,"FATAL ERROR: could not connect to telephony subsystem");
    //Log.error(DialerActivity.this, "Exception object: " + e);
}



finish();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.metdllamadas, menu);
    return true;
}

}

假设活动是从另一个给出参数“Metodo”的活动启动的,如果Metodo1 = 2那么呼叫必须终止(或拒绝),如果Metodo1 = 1我必须接听电话,如果Metodo1 = 3 i必须用扬声器回答。

0 个答案:

没有答案