0

I am trying to do some work at incoming call times. I am trying like this

public class callDeductor extends BroadcastReceiver {



       @Override
        public void onReceive(Context context, Intent intent) {                                       
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);                    


            if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
            {
                 Toast.makeText(context, "Phone Is Ringing", Toast.LENGTH_LONG).show();

            }

            if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
            {
                 Toast.makeText(context, "Call Recieved", Toast.LENGTH_LONG).show();
            }

            if (state.equals(TelephonyManager.EXTRA_STATE_IDLE))
            {
                Toast.makeText(context, "Phone Is Idle", Toast.LENGTH_LONG).show();

            }

        }

    }

This is my Manifeast:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.deduct.calldeduct"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="Call Deduct" >
        <receiver android:name="com.deduct.calldeduct.callDeductor" > 
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

I check with mobile and emulator. But it does not show any toast message. Please let me know where I did mistake.

I got reference from this websites. Link 1 Link 2

  • These answers might help to detect incoming call. http://stackoverflow.com/questions/15563921/detecting-an-incoming-call-coming-to-an-android-device http://stackoverflow.com/questions/6610750/how-to-detect-and-manage-incoming-call-android – zzas11 May 21 '15 at 06:39
  • ur code is working like a charm..it works – DJphy May 21 '15 at 07:25
  • Dont delete ur activity, nor the activity xml file and also dont delete the activity that will be auto registered in Manifest when starting a new project. Within the same Main Activity class package have ur CallDetector class as well. Register this receiver in Manifest as u have done. Dont do anything in ur main activity. Then run it, ur activity will launch, make a call u will 100% get a Toast. – DJphy May 21 '15 at 07:31
  • But not working for me :-( . Even I download the example from that site. That is also not working :-( – Vijayadhas Chandrasekaran May 21 '15 at 07:33
  • Once u have run it and its a success, its time to delete ur Main Activity class, and its related layout xml and Also the activity registered in manifest as launcher. The next time u run ur application, there will be no activity but ur receiver will work as expected. The first time of App run to be used as receiver needs activity , i also faced this issue long back.. – DJphy May 21 '15 at 07:36
  • Start a new project and do as i have mentioned..Its working for me, i copy pasted ur code buddy – DJphy May 21 '15 at 07:37

1 Answers1

1

Continuing my conversation with asker as an answer.I Just started a new Android application and worked out like i have said in the comments.. The manifest looks like this :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dj.randomtest"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >


     <receiver android:name=".CallDetector">
        <intent-filter>                
            <action android:name="android.intent.action.PHONE_STATE" />                
        </intent-filter>
    </receiver>

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

The CallDetector class looks like this(ur code):

public class CallDetector extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {


    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);                    


    if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
    {
        Toast.makeText(context, "Phone Is Ringing", Toast.LENGTH_LONG).show();

    }

    if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
    {
        Toast.makeText(context, "Call Recieved", Toast.LENGTH_LONG).show();
    }

    if (state.equals(TelephonyManager.EXTRA_STATE_IDLE))
    {
        Toast.makeText(context, "Phone Is Idle", Toast.LENGTH_LONG).show();

    }

}

}

ScreenShot of the project set up:

DJphy
  • 1,292
  • 1
  • 17
  • 31
  • Notin wrong, jus telling that the askers code is working fine...See my conversation with the asker above.The answer is jus a continuation of the conversation. – DJphy May 21 '15 at 08:18
  • Ok, I see. I'd add a small conclusion here though :) ... only difference I can see is, that you use a relative class path here: `android:name=".CallDetector"` .... and you followed the Java naming convention ;) – Trinimon May 21 '15 at 08:29