我必须在 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 我必须接听带扬声器。