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

今天遇到这样的问题,在调用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);
    }
}

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

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.编译该应用程序并安装。



评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值