Android PhoneStateListener not working inside broadcast receiver

885 views Asked by At

I have written a small program to turn on the camera flash LED in the event of an incoming call. It worked fine for the first time, but the second time the application crashed. And now the application wouldn't work. I even re-installed the app! I'm using TelephonyManager and PhoneStateListener. Can anyone please indicate what's wrong with the program? It's been more than an hour, and I've got no clue! Any help would be appreciated.

Here is the manifest declaration:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.firefly"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="14" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.CAMERA" />

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

    <application android:icon="@drawable/ic_launcher" android:label="Firefly">

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

    </application>

</manifest>

Actual code:

package com.firefly;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class MainActivity extends BroadcastReceiver {

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

        try {
            TelephonyManager tmgr = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);

            // Create Listener
            MyPhoneStateListener PhoneListener = new MyPhoneStateListener(context);

            // Register listener for LISTEN_CALL_STATE
            tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

        } catch (Exception e) {
            Log.e("Phone Receive Error", " " + e);
        }

    }

    private class MyPhoneStateListener extends PhoneStateListener {

        private Context ctx;
        private boolean flashavailable;
        private Camera camera;

        private MyPhoneStateListener(Context ctx) {
            this.ctx=ctx;
        }

        public void onCallStateChanged(int state, String incomingNumber) {

            flashavailable=ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

            if (state == TelephonyManager.CALL_STATE_RINGING) {             
                if (flashavailable){
                    //Turning on the flash LED
                    camera = Camera.open();
                    final Parameters p = camera.getParameters();
                    p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                    camera.setParameters(p);
                    camera.startPreview();
                }
                String msg = "New Phone Call Event. Incomming Number : "+incomingNumber;
                Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show();
            }
        }
    }
}
0

There are 0 answers