2010-01-27 102 views
14

我正在嘗試獲取電話對象,以便我可以在我的應用程序中呼叫和會議兩個號碼。Telephony.Phone對象是否可以通過sdk實例化?

我試過使用靜態PhoneFactory.makeDefaultPhones((Context)this),但沒有任何運氣。

String phoneFactoryName = "com.android.internal.telephony.PhoneFactory"; 
String phoneName = "com.android.internal.telephony.Phone"; 
Class phoneFactoryClass = Class.forName(phoneFactoryName); 
Class phoneClass = Class.forName(phoneName); 
Method getDefaultPhone = phoneFactoryClass.getMethod("getDefaultPhone"); 
Object phoneObject = getDefaultPhone.invoke(null); 

Error - Caused by java.lang.RuntimeException: PhoneFactory.getDefaultPhone must be called from Looper thread

+0

這可能很明顯,但由於'PhoneFactory'不在SDK中,所以您可能不想使用它:) – 2010-01-27 02:38:54

+1

運行此代碼時,出現'InvocationTargetException' 'getDefaultPhone.invoke(空)'。我也嘗試過使用'getDefaultPhone.setAccessible(true)'在它之前,但是沒有效果。 – 2011-01-25 13:06:03

+0

@Tyler我必須從我的應用程序發起電話會議。如果你發現任何解決方案.pls在這裏回答 – 2011-08-27 09:16:53

回答

0

I am trying to get a phone object so that I can call and conference two numbers from within my application.

從SDK是不可能的。

I have tried using the static PhoneFactory.makeDefaultPhones((Context)this) but have not had any luck.

這不在SDK中。請do not go past the bounds of the SDK

Error - Caused by java.lang.RuntimeException: PhoneFactory.getDefaultPhone must be called from Looper thread

這是因爲你正在嘗試做的事情 - 你不應該從後臺線程做。

+4

我意識到,我不應該在我的應用程序中進行高級調用管理,但沒有任何公共方法可以滿足要求。我想撥打兩個號碼,召開會議,並在適當的時候掛斷電話。你有沒有建議如何在獲取內部電話對象之外做到這一點。 – tsmith 2010-01-27 17:21:40

0

我從Activity.onCreate叫它,它您的問題,下面的錯誤後墜毀幾行:

Default phones haven't been made yet!

See the Android sources

public static Phone getDefaultPhone() { 
    if (sLooper != Looper.myLooper()) { 
     throw new RuntimeException(
      "PhoneFactory.getDefaultPhone must be called from Looper thread"); 
    } 

    if (!sMadeDefaults) { 
     throw new IllegalStateException("Default phones haven't been made yet!"); 
    } 
    return sProxyPhone; 
} 
4

鋁至少我們可以接聽或忽略來電=)讓我複製粘貼我的帖子

OMG !!!是的,我們可以做到!
經過嚴格的24小時調查和發現後,我纔會自殺......但我發現了一種「新鮮」的解決方案!

// "cheat" with Java reflection to gain access 
// to TelephonyManager's ITelephony getter 
Class c = Class.forName(tm.getClass().getName()); 
Method m = c.getDeclaredMethod("getITelephony"); 
m.setAccessible(true); 
telephonyService = (ITelephony)m.invoke(tm); 

的人誰想要開發自己的呼叫控制軟件訪問這個起點: http://www.google.com/codesearch/p?hl=en#zvQ8rp58BUs/trunk/phone/src/i4nc4mp/myLock/phone/CallPrompt.java&q=itelephony%20package:http://mylockforandroid%5C.googlecode%5C.com&d=0

有一個項目。並有重要的評論(和學分)。

簡而言之:複製AIDL文件,爲清單添加權限,複製粘貼電話管理源。

一些更多的信息給你。 AT命令只有在植根後才能發送。比你可以殺死系統進程併發送命令,但你需要重新啓動以允許你的手機接收和發送呼叫。

我很開心! =)現在我的Shake2MuteCall會得到更新!

+0

我可以確認'myLock'項目的工作原理(可以從http://mylockforandroid.googlecode.com/svn/trunk/phone/的SVN中查到),但是它不提供對'PhoneFactory'的訪問。有沒有辦法修改這種方法,或者至少得到主動的「呼叫」? – 2011-01-25 13:04:31

+0

請注意您正在訪問Android框架的內部。而且你沒有任何保證。它可能不適用於所有設備,更新後可能會突然停止在某些設備上工作。 – 2012-02-20 13:33:16

+0

鏈接轉到404; [ – r1si 2017-02-08 06:53:40

11

是的,它可以被實例化。但是,你必須克服幾個障礙:

  • 在你AndroidManifest.xml設置

    android:sharedUserId="android.uid.phone"

    <manifest>標籤內。當您使用可能調用的方法(如android.intent.action.SIM_STATE_CHANGED)發送受保護的Intents時,需要這樣做以防止引發SecurityException

  • android:process="com.android.phone"

    <application>標籤。這是允許調用getDefaultPhone()/makeDefaultPhone()所必需的。

  • 要做到這一切,您的應用程序必須使用系統簽名密鑰進行簽名。

2

Hy。我能夠檢索到一個代理電話,這個類已經變成了這個類(並且有點反思)。您可以使用下面的(反射)PhoneFactory:

package your.package; 

import java.lang.reflect.Method; 

import android.content.Context; 
import android.util.Log; 

public class ReflectedPhoneFactory { 

public static final String TAG = "PHONE"; 

public static void makeDefaultPhones(Context context) throws IllegalArgumentException { 

    try{ 

     ClassLoader cl = context.getClassLoader(); 
     @SuppressWarnings("rawtypes") 
     Class PhoneFactory = cl.loadClass("com.android.internal.telephony.PhoneFactory"); 

     //Parameters Types 
     @SuppressWarnings("rawtypes") 
     Class[] paramTypes= new Class[1]; 
     paramTypes[0]= Context.class; 

     Method get = PhoneFactory.getMethod("makeDefaultPhone", paramTypes); 

     //Parameters 
     Object[] params= new Object[1]; 
     params[0]= context; 

     get.invoke(null, params); 

    }catch(IllegalArgumentException iAE){ 
     throw iAE; 
    }catch(Exception e){ 
     Log.e(TAG, "makeDefaultPhones", e); 
    } 

} 

public static void makeDefaultPhone(Context context) throws IllegalArgumentException { 

    try{ 

     ClassLoader cl = context.getClassLoader(); 
     @SuppressWarnings("rawtypes") 
     Class PhoneFactory = cl.loadClass("com.android.internal.telephony.PhoneFactory"); 

     //Parameters Types 
     @SuppressWarnings("rawtypes") 
     Class[] paramTypes= new Class[1]; 
     paramTypes[0]= Context.class; 

     Method get = PhoneFactory.getMethod("makeDefaultPhone", paramTypes); 

     //Parameters 
     Object[] params= new Object[1]; 
     params[0]= context; 

     get.invoke(null, params); 

    }catch(IllegalArgumentException iAE){ 
     throw iAE; 
    }catch(Exception e){ 
     Log.e(TAG, "makeDefaultPhone", e); 
    } 

} 

/* 
* This function returns the type of the phone, depending 
* on the network mode. 
* 
* @param network mode 
* @return Phone Type 
*/ 
public static Integer getPhoneType(Context context, int networkMode) throws IllegalArgumentException { 

    Integer ret= -1; 

    try{ 

     ClassLoader cl = context.getClassLoader(); 
     @SuppressWarnings("rawtypes") 
     Class PhoneFactory = cl.loadClass("com.android.internal.telephony.PhoneFactory"); 

     //Parameters Types 
     @SuppressWarnings("rawtypes") 
     Class[] paramTypes= new Class[1]; 
     paramTypes[0]= Integer.class; 

     Method get = PhoneFactory.getMethod("getPhoneType", paramTypes); 

     //Parameters 
     Object[] params= new Object[1]; 
     params[0]= new Integer(networkMode); 

     ret= (Integer) get.invoke(PhoneFactory, params); 

    }catch(IllegalArgumentException iAE){ 
     throw iAE; 
    }catch(Exception e){ 
     ret= -1; 
    } 

    return ret; 

} 

public static Object getDefaultPhone(Context context) throws IllegalArgumentException { 

    Object ret= null; 

    try{ 

     ClassLoader cl = context.getClassLoader(); 
     @SuppressWarnings("rawtypes") 
     Class PhoneFactory = cl.loadClass("com.android.internal.telephony.PhoneFactory"); 

     Method get = PhoneFactory.getMethod("getDefaultPhone", (Class[]) null); 
     ret= (Object)get.invoke(null, (Object[]) null); 

    }catch(IllegalArgumentException iAE){ 
     throw iAE; 
    }catch(Exception e){ 
     Log.e(TAG, "getDefaultPhone", e); 
    } 

    return ret; 

} 

public static Phone getCdmaPhone(Context context) throws IllegalArgumentException { 

    Phone ret= null; 

    try{ 

     ClassLoader cl = context.getClassLoader(); 
     @SuppressWarnings("rawtypes") 
     Class PhoneFactory = cl.loadClass("com.android.internal.telephony.PhoneFactory"); 

     Method get = PhoneFactory.getMethod("getCdmaPhone", (Class[]) null); 
     ret= (Phone)get.invoke(null, (Object[]) null); 

    }catch(IllegalArgumentException iAE){ 
     throw iAE; 
    }catch(Exception e){ 
     // 
    } 

    return ret; 

} 

public static Phone getGsmPhone(Context context) throws IllegalArgumentException { 

    Phone ret= null; 

    try{ 

     ClassLoader cl = context.getClassLoader(); 
     @SuppressWarnings("rawtypes") 
     Class PhoneFactory = cl.loadClass("com.android.internal.telephony.PhoneFactory"); 

     Method get = PhoneFactory.getMethod("getGsmPhone", (Class[]) null); 
     ret= (Phone)get.invoke(null, (Object[]) null); 

    }catch(IllegalArgumentException iAE){ 
     throw iAE; 
    }catch(Exception e){ 
     // 
    } 

    return ret; 

} 
} 

有了它,使用代碼:

 ReflectedPhoneFactory.makeDefaultPhone(yourContext); 
     Object phoneProxy= ReflectedPhoneFactory.getDefaultPhone(yourContext); 

注意「makeDefaultPhone」通話將更新靜態成員「靜態專用尺蠖的價值sLooper;」我還沒有測試過附帶效應。

使用收到的「phoneProxy」對象,您可以使PhoneProxy呼叫進行反射。 (我目前正在實現這個類,並且可能會發布它,如果認爲有用的話。

+0

就像你裹在ReflectedPhone類中一樣。好的模式。 – 2012-02-21 01:10:33

+0

我在Galaxy S2上試過這個,但是在'ret =(Object)get.invoke(null,(Object [])null);'上得到了一個InvocationTargetException異常。 – brianestey 2012-05-11 12:19:34

0

Fyi,內部類Phone,CallManager等等從/system/framework/framework.jar移動到/ system/framework/telephony-common .jar在果凍豆

相關問題