2010-01-27 27 views
14

Tôi đang cố gắng lấy một đối tượng điện thoại để tôi có thể gọi và hội thảo hai số điện thoại từ trong ứng dụng của mình.Một đối tượng telephony.Phone có thể được khởi tạo qua sdk không?

Tôi đã thử sử dụng static PhoneFactory.makeDefaultPhones((Context)this) nhưng chưa có bất kỳ may mắn nào.

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

Điều này có thể hiển nhiên nhưng vì 'PhoneFactory' không có trong SDK, bạn có thể không muốn sử dụng nó :) –

+1

Khi chạy mã này, tôi nhận được' InvocationTargetException' trên 'getDefaultPhone.invoke (null)'. Tôi cũng đã thử trước nó với 'getDefaultPhone.setAccessible (true)', nhưng điều đó không có hiệu lực. –

+0

@Tyler tôi phải khởi chạy cuộc gọi hội nghị từ ứng dụng của mình. nếu bạn tìm thấy bất kỳ giải pháp .pls trả lời ở đây –

Trả lời

0

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

Đó không phải là có thể từ SDK.

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

Điều đó không có trong SDK. Vui lòng do not go past the bounds of the SDK.

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

Đó là bởi vì bạn đang cố gắng thực hiện việc bạn không cần phải làm từ chuỗi nền.

+4

Tôi nhận ra rằng tôi không được yêu cầu, tuy nhiên để thực hiện cuộc gọi nâng cao quản lý từ bên trong ứng dụng của tôi, không có phương pháp công cộng nào đủ. Tôi muốn gọi hai số, thảo luận và gọi điện thoại khi thích hợp. Bạn có bất kỳ đề xuất nào về cách thực hiện điều đó bên ngoài việc nhận đối tượng Điện thoại nội bộ hay không. – tsmith

0

tôi gọi nó từ Activity.onCreate và nó đã bị rơi một vài dòng sau khi vấn đề của bạn với các lỗi sau:

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

Al nhất chúng tôi có thể trả lời hoặc bỏ qua các cuộc gọi =) hãy để tôi sao chép-dán bài đăng của tôi

OMG !!! CÓ, CHÚNG TÔI CÓ THỂ LÀM ĐƯỢC ĐIỀU ĐÓ!!!
Tôi sẽ tự tử sau 24 giờ điều tra và khám phá nghiêm trọng ... Nhưng tôi đã tìm thấy một giải pháp "mới"!

// "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); 

Những người muốn phát triển phần mềm truy cập điều khiển cuộc gọi của họ điểm bắt đầu này: 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

Có một dự án. và có những ý kiến ​​quan trọng (và các khoản tín dụng).

Tóm lại: sao chép tệp AIDL, thêm quyền vào tệp kê khai, sao chép-dán nguồn để quản lý điện thoại.

Một số thông tin khác cho bạn. Lệnh AT bạn chỉ có thể gửi nếu bạn bắt nguồn từ. Hơn bạn có thể giết chết quá trình hệ thống và gửi lệnh nhưng bạn sẽ cần khởi động lại để cho phép điện thoại nhận và gửi cuộc gọi.

Tôi rất vui! =) Bây giờ Shake2MuteCall của tôi sẽ nhận được bản cập nhật!

+0

Tôi có thể xác nhận rằng dự án 'myLock' hoạt động (bạn có thể kiểm tra nó từ SVN từ http://mylockforandroid.googlecode.com/svn/trunk/phone/), nhưng nó không cung cấp quyền truy cập vào' PhoneFactory'. Có cách nào để sửa đổi cách tiếp cận này để làm như vậy, hoặc ít nhất có được hoạt động 'Call'? –

+0

Lưu ý rằng bạn đang truy cập vào nội bộ của khung công tác Android. Và bạn không có gì đảm bảo như vậy. Nó có thể sẽ không hoạt động trên tất cả các thiết bị và nó có thể đột ngột ngừng hoạt động trên một số thiết bị sau khi cập nhật. –

+0

Liên kết sẽ chuyển sang 404; [ – r1si

11

Có thể được khởi tạo. Nhưng bạn phải vượt qua một vài rào cản:

  • Trong AndroidManifest.xml của bạn thiết

    android:sharedUserId="android.uid.phone"

    trong thẻ <manifest>. Điều này là cần thiết để ngăn không cho SecurityException bị ném khi Intents được bảo vệ được gửi bằng các phương pháp bạn có thể gọi (như android.intent.action.SIM_STATE_CHANGED).

  • Set

    android:process="com.android.phone"

    trong thẻ <application> của bạn. Điều này là bắt buộc để cho phép gọi số getDefaultPhone()/makeDefaultPhone().

  • Để thực hiện tất cả điều này, ứng dụng của bạn phải được ký bằng khóa chữ ký hệ thống.

2

Hy. Tôi đã có thể lấy một ProxyPhone trought lớp này (và một chút phản ánh). Bạn có thể sử dụng PhoneFactory (Reflected) dưới đây:

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; 

} 
} 

Với nó, sử dụng mã:

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

Lưu ý rằng "makeDefaultPhone" cuộc gọi sẽ cập nhật giá trị của các thành viên tĩnh "tĩnh tin Looper sLooper; " và tôi chưa thử nghiệm các hiệu ứng thế chấp.

Với đối tượng "phoneProxy" đã nhận, bạn có thể thực hiện phản chiếu trêu chọc của cuộc gọi PhoneProxy. (Tôi hiện đang triển khai lớp này và có thể đăng bài nếu được xem là hữu ích.

+0

như cách gói trong lớp ReflectedPhone của bạn. mô hình tốt. –

+0

Tôi đã thử trên Galaxy S2 nhưng có một InvocationTargetException trên dòng 'ret = (Object) get.invoke (null, (Object []) null);'. – brianestey

0

Fyi, các lớp nội bộ Điện thoại, CallManager và một số khác được chuyển từ /system/framework/framework.jar sang/system/framework/telephony-common .jar trong Jelly bean

Các vấn đề liên quan