今天遇到这样的问题,在调用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 应用工程,我的测试应用代码如下:
package com.android.qrdtest; import android.app.Activity; import android.os.Bundle; import android.util.Log; import com.android.internal.telephony.Phone; import com.android.internal.telephony.PhoneFactory; public class QRDTestActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.i("QRDTestActivity", " test test = "); Phone phone = PhoneFactory.getDefaultPhone(); String text= phone.getActiveApn(); Log.i("QRDTestActivity", " text = "+ text); } }
LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE_TAGS := optional LOCAL_SRC_FILES := $(call all-java-files-under, src) LOCAL_PACKAGE_NAME := Qrdtest LOCAL_CERTIFICATE := platform include $(BUILD_PACKAGE) # Build the test package include $(call all-makefiles-under,$(LOCAL_PATH))
3. 修改AndroidManifest.xml 文件,具体修改如下,注意android:sharedUserId="android.uid.phone" 和android:process="com.android.phone"部分。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.qrdtest" android:sharedUserId="android.uid.phone" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/ic_launcher" android:process="com.android.phone" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".QRDTestActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
4.编译该应用程序并安装。