2017-01-12 12 views
2

如何使android應用程序未接電話到指定的號碼。我試過這個,但它會開始一個呼叫,並等待呼叫終止。我想讓電話響一次,然後停下來。請幫幫我。在android中的背景中未接電話

Intent callIntent = new Intent(Intent.ACTION_CALL); 
callIntent.setData(Uri.parse("tel:123456789")); 
startActivity(callIntent); 
+0

我不認爲這是可能的,因爲電話是由你沒有控制的系統,除非生根,你可以掛斷電話開始之前(http://stackoverflow.com/問題/ 599443 /如何掛斷,傳出呼叫在Android),但不在呼叫中間 – WenChao

回答

0

使從Android應用程序未接來電到指定號碼

可以實現它,但不能保證(如果對方接收呼叫瞬間)!做你的計算多少時間需要1個環。開始Thread/Timer,然後調用此方法結束呼叫!

private void mEndCall() { 
     try { 
     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"); 

     telephonyEndCall.invoke(telephonyObject); 

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

    } 
} 
+0

這不會是一個可靠的方式,因爲通話時間差異很大。感謝您的回答。 –