现在的位置: 首页 > 综合 > 正文

PhoneFactory.getDefaultPhone()引发的问题解决

2018年04月21日 ⁄ 综合 ⁄ 共 4151字 ⁄ 字号 评论关闭

今天遇到这样的问题,在调用PhoneFactory.getDefaultPhone()出现如下的错误:


PhoneFactory.getDefaultPhone must be called from Looper thread

E/AndroidRuntime( 2014):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1745)

E/AndroidRuntime( 2014):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1761)

E/AndroidRuntime( 2014):     at android.app.ActivityThread.access$1500(ActivityThread.java:120)

E/AndroidRuntime( 2014):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:934)

E/AndroidRuntime( 2014):     at android.os.Handler.dispatchMessage(Handler.java:99)

E/AndroidRuntime( 2014):     at android.os.Looper.loop(Looper.java:130)

E/AndroidRuntime( 2014):     at android.app.ActivityThread.main(ActivityThread.java:3781)

E/AndroidRuntime( 2014):     at java.lang.reflect.Method.invokeNative(Native Method)

E/AndroidRuntime( 2014):     at java.lang.reflect.Method.invoke(Method.java:507)

E/AndroidRuntime( 2014):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:921)

E/AndroidRuntime( 2014):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

E/AndroidRuntime( 2014):     at dalvik.system.NativeStart.main(Native Method)

E/AndroidRuntime( 2014): Caused by: java.lang.RuntimeException: PhoneFactory.getDefaultPhone must be called from Looper thread

E/AndroidRuntime( 2014):     at com.android.internal.telephony.PhoneFactory.getDefaultPhone(PhoneFactory.java:211)

E/AndroidRuntime( 2014):     at com.android.qrdtest.QRDTestActivity.onCreate(QRDTestActivity.java:17)

E/AndroidRuntime( 2014):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

E/AndroidRuntime( 2014):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1709)

E/AndroidRuntime( 2014):     ... 11 more

W/ActivityManager(  218):   Force finishing activity com.android.qrdtest/.QRDTestActivity


解决方法如下:

1.使用eclipse 新建一个android 应用工程,我的测试应用代码如下:

[java] view
plain
copy

  1. package com.android.qrdtest;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6.   
  7. import com.android.internal.telephony.Phone;  
  8. import com.android.internal.telephony.PhoneFactory;  
  9.   
  10. public class QRDTestActivity extends Activity {  
  11.     /** Called when the activity is first created. */  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         Log.i("QRDTestActivity"" test test  = ");  
  17.         Phone phone = PhoneFactory.getDefaultPhone();  
  18.         String text= phone.getActiveApn();  
  19.         Log.i("QRDTestActivity"" text = "+ text);  
  20.     }  
  21. }  


2,将新建的应用工程代码移入到packages/apps/,并在该目录中新建android.mk文件,注意LOCAL_CERTIFICATE :=
platform
部分,具体内容如下:

[java] view
plain
copy

  1. LOCAL_PATH:= $(call my-dir)  
  2.   
  3. include $(CLEAR_VARS)  
  4.   
  5. LOCAL_MODULE_TAGS := optional  
  6.   
  7. LOCAL_SRC_FILES := $(call all-java-files-under, src)  
  8.   
  9. LOCAL_PACKAGE_NAME := Qrdtest  
  10. LOCAL_CERTIFICATE := platform  
  11.   
  12. include $(BUILD_PACKAGE)  
  13.   
  14. # Build the test package  
  15. include $(call all-makefiles-under,$(LOCAL_PATH))  



3. 修改AndroidManifest.xml 文件,具体修改如下,注意android:sharedUserId="android.uid.phone" 和android:process="com.android.phone"部分。

[java] view
plain
copy

  1.   
[html] view
plain
copy

  1. <pre name="code" class="java"><?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.android.qrdtest"  
  4.       
  5.     android:sharedUserId="android.uid.phone"  
  6.     android:versionCode="1"  
  7.     android:versionName="1.0" >  
  8.   
  9.     <uses-sdk android:minSdkVersion="10" />  
  10.   
  11.     <application  
  12.     android:icon="@drawable/ic_launcher"  
  13.     android:process="com.android.phone"  
  14.         android:label="@string/app_name" >  
  15.       
  16.         <activity  
  17.             android:label="@string/app_name"  
  18.             android:name=".QRDTestActivity" >  
  19.             <intent-filter >  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.   
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26.   
  27. </manifest></pre><br>  
  28. <br>  
  29. <pre></pre>  
  30. 4.编译该应用程序并安装。  
  31. <p></p>  
  32. <p></p>  
  33. <p></p>  
  34. <p></p>  
  35. <p></p>  
  36. <p></p>  
  37. <p></p>  
  38. <p></p>  
  39. <p></p>  
  40. <pre></pre>  
  41. <pre></pre>  

抱歉!评论已关闭.