PhoneStateListener - 应用程序意外停止

时间:2012-06-07 18:38:30

标签: android events call phone-state-listener

这是我的代码:

private PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);
        switch (state) {
        case TelephonyManager.CALL_STATE_OFFHOOK:
            break;
        case TelephonyManager.CALL_STATE_RINGING:
            break;
        case TelephonyManager.CALL_STATE_IDLE:
            break;
        }
    }
};

OnCreate事件:

mTelephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        mTelephonyManager.listen(mPhoneStateListener,
                PhoneStateListener.LISTEN_CALL_STATE);

AndroidManifest.xml:

  <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
        <receiver android:name=".ServiceReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>

修改

错误日志:

06-07 20:00:35.203: W/dalvikvm(1807): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
06-07 20:00:36.253: E/AndroidRuntime(1807): FATAL EXCEPTION: main
06-07 20:00:36.253: E/AndroidRuntime(1807): java.lang.RuntimeException: Unable to instantiate receiver company.group.ServiceReceiver: java.lang.ClassNotFoundException: company.group.ServiceReceiver
06-07 20:00:36.253: E/AndroidRuntime(1807):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2100)
06-07 20:00:36.253: E/AndroidRuntime(1807):     at android.app.ActivityThread.access$1500(ActivityThread.java:123)
06-07 20:00:36.253: E/AndroidRuntime(1807):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1197)
06-07 20:00:36.253: E/AndroidRuntime(1807):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-07 20:00:36.253: E/AndroidRuntime(1807):     at android.os.Looper.loop(Looper.java:137)
06-07 20:00:36.253: E/AndroidRuntime(1807):     at android.app.ActivityThread.main(ActivityThread.java:4424)
06-07 20:00:36.253: E/AndroidRuntime(1807):     at java.lang.reflect.Method.invokeNative(Native Method)
06-07 20:00:36.253: E/AndroidRuntime(1807):     at java.lang.reflect.Method.invoke(Method.java:511)
06-07 20:00:36.253: E/AndroidRuntime(1807):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-07 20:00:36.253: E/AndroidRuntime(1807):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-07 20:00:36.253: E/AndroidRuntime(1807):     at dalvik.system.NativeStart.main(Native Method)
06-07 20:00:36.253: E/AndroidRuntime(1807): Caused by: java.lang.ClassNotFoundException: company.group.ServiceReceiver
06-07 20:00:36.253: E/AndroidRuntime(1807):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
06-07 20:00:36.253: E/AndroidRuntime(1807):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
06-07 20:00:36.253: E/AndroidRuntime(1807):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
06-07 20:00:36.253: E/AndroidRuntime(1807):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2095)

问题:当我接到电话时,我的应用程序会意外停止。这段代码有什么问题?

1 个答案:

答案 0 :(得分:0)

您使用的是服务还是活动?

如果你在一个Activity中做这件事,试着在onCreate()中做两件事(用于PhoneStateListener和TelephonyManager的crate内部类)。