Android: BroadcastReceiver
<p>I am trying to listen to the phone state when contact is calling me but i am getting some error.</p>

<p>ServiceReceiver.java</p>

<pre><code>package org.example.servicereceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;


public class ServiceReceiver extends BroadcastReceiver {

    private static final String TAG = "ServiceReceiver"; 

    @Override
    public void onReceive(Context context, Intent intent) {
        MyPhoneStateListener phoneListener=new MyPhoneStateListener();
        TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        Log.d(TAG, "telephony: " + telephony);

        telephony.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
    }
}
</code></pre>

<p>MyPhoneStateListener.java</p>

<pre><code>package org.example.servicereceiver;

import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class MyPhoneStateListener extends PhoneStateListener {
    public void onCallStateChanged(int state,String incomingNumber){
        switch(state)
        {
           case TelephonyManager.CALL_STATE_IDLE:
                Log.d("DEBUG", "IDLE");
                break;
           case TelephonyManager.CALL_STATE_OFFHOOK:
                Log.d("DEBUG", "OFFHOOK");
                break;
           case TelephonyManager.CALL_STATE_RINGING:
               Log.d("DEBUG", "RINGING");
               break;
        }
    }   

}
</code></pre>

<p>Errors:</p>

<pre><code>04-13 18:25:21.845: ERROR/AndroidRuntime(14861): FATAL EXCEPTION: main
04-13 18:25:21.845: ERROR/AndroidRuntime(14861): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.example.servicereceiver/org.example.servicereceiver.ServiceReceiver}: java.lang.ClassCastException: org.example.servicereceiver.ServiceReceiver
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2709)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at android.app.ActivityThread.access$2300(ActivityThread.java:135)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at android.os.Looper.loop(Looper.java:144)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at android.app.ActivityThread.main(ActivityThread.java:4937)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at java.lang.reflect.Method.invokeNative(Native Method)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at java.lang.reflect.Method.invoke(Method.java:521)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at dalvik.system.NativeStart.main(Native Method)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861): Caused by: java.lang.ClassCastException: org.example.servicereceiver.ServiceReceiver
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at android.app.Instrumentation.newActivity(Instrumentation.java:1036)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2701)
</code></pre>

<p>XML:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="org.example.servicereceiver"
      android:versionCode="1"
      android:versionName="1.0"&gt;
    &lt;application android:icon="@drawable/icon" android:label="@string/app_name"&gt;
        &lt;activity android:name=".ServiceReceiver"
                  android:label="@string/app_name"&gt;
            &lt;intent-filter&gt;
                &lt;action android:name="android.intent.action.MAIN" /&gt;
                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
        &lt;receiver android:name=".ServiceReceiver"&gt;
        &lt;intent-filter&gt;
        &lt;action android:name="android.intent.action.PHONE_STATE"/&gt;
        &lt;/intent-filter&gt;
        &lt;/receiver&gt;

    &lt;/application&gt;
    &lt;uses-sdk android:minSdkVersion="8" /&gt;
    &lt;uses-permission android:name="android.permission.READ_PHONE_STATE"/&gt;

&lt;/manifest&gt; 
</code></pre>
