5

I'm trying to catch an incomming call broadcast but it isn't working.

This is my manifest:

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

<uses-sdk android:minSdkVersion="9" />

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

<uses-feature
    android:name="android.hardware.camera"
    android:required="false" />
<uses-feature
    android:name="android.hardware.camera.front"
    android:required="false" />

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name" >
    <receiver android:name=".PhoneStateBroadcastReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>
</application>

This is my broadcast receiver

package com.test.bgPicture;

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

public class PhoneStateBroadcastReceiver extends BroadcastReceiver{

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

        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);
    }
}

And the listener..

package com.test.bgPicture;

import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class CustomPhoneStateListener extends PhoneStateListener {

//private static final String TAG = "PhoneStateChanged";
Context context; //Context to make Toast if required 
public CustomPhoneStateListener(Context context) {
    super();
    this.context = context;
}

@Override
public void onCallStateChanged(int state, String incomingNumber) {
    super.onCallStateChanged(state, incomingNumber);

    switch (state) {
    case TelephonyManager.CALL_STATE_IDLE:
        //when Idle i.e no call
        Toast.makeText(context, "Phone state Idle", Toast.LENGTH_LONG).show();
        break;
    case TelephonyManager.CALL_STATE_OFFHOOK:
        //when Off hook i.e in call
        //Make intent and start your service here
        Toast.makeText(context, "Phone state Off hook", Toast.LENGTH_LONG).show();
        break;
    case TelephonyManager.CALL_STATE_RINGING:
        //when Ringing
        Toast.makeText(context, "Phone state Ringing", Toast.LENGTH_LONG).show();
        break;
    default:
        break;
    }
}
}

But the toasts aren't displayed, any idea's?

EDIT: I just tried it with another phone with android 2.3.6 and it worked there, but on the first device (android 4.1) it didn't work. Why could this be ?

Robby Smet
  • 4,649
  • 8
  • 61
  • 104
  • try to `Log.v("onCallStateChanged","Phone state Idle");` and check logcat because maybe possible context you are using for showing Toast is not available – ρяσѕρєя K Nov 28 '12 at 11:15
  • That didn't help.. I did notice that it works on android 2.3.6 but not on android 4.1, any idea ? – Robby Smet Nov 28 '12 at 12:12

2 Answers2

10

I found the problem, you must manually start an activity from your application before the broadcast receivers starts to work as of android 3.0.

Robby Smet
  • 4,649
  • 8
  • 61
  • 104
  • I read somewhere on the blogs that this is some security feature, so that application that user never activated by starting an activity dont get the broadcasts. – Abhijeet Apsunde May 23 '13 at 05:17
-1

Have you tried to add a broadcast receiver inside a service? A service is meant to stay active even when the application is closed/minimized. Check this link

Community
  • 1
  • 1
Renato Probst
  • 5,914
  • 2
  • 42
  • 45