Android : Android: The code in my Broadcast Receiver is repeatedly executing

on Sunday, August 3, 2014


Hai iam new to android development working with the broadcast receivers for the first time...My app has a broadcast receiver and a class(MyPhoneStateListener.java) which extends PhoneStateListener to notify the phone number of the incoming call. Here the issue is i have a Log.d("no", msg) statement in MyPhoneStateListener.java to view the incoming call number in logcat. When i launch my app for the first time and test it, the phone number is shown once that is fine, when i test it for second time the phone number is shown three times(the statement or the class may be executed 3 times),and when i test it again the phone number is shown 5 times and so on...i posted my codes below please check it and help me to solve.


BroadReceiver.java



package com.vibrator;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
//import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
//import android.widget.Toast;

public class BroadReceiver extends BroadcastReceiver{



public void onReceive(Context context, Intent intent) {

try {
// TELEPHONY MANAGER class object to register one listner
TelephonyManager tmgr = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);

//Create Listner
MyPhoneStateListener PhoneListener = new MyPhoneStateListener();

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

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


MyPhoneStateListener.java



package com.vibrator;
import android.telephony.PhoneStateListener;
import android.util.Log;

public class MyPhoneStateListener extends PhoneStateListener {

public void onCallStateChanged(int state, String incomingNumber) {

// Log.d("MyPhoneListener",state+" incoming no:"+incomingNumber);
//if the call state is ringing(i.e state=1) then if statements executes
if (state == 1) {

String msg = "New Phone Call Event. Incomming Number : "+incomingNumber;
Log.d("no", msg);

}
}
}


}


Logcat:



08-03 12:24:15.258: D/dalvikvm(5878): VFY: replacing opcode 0x70 at 0x0000
08-03 12:24:15.268: D/dalvikvm(5878): VFY: dead code 0x0003-0007 in Landroid/support/v7/internal/view/menu/ActionMenuView$LayoutParams;.<init> (Landroid/support/v7/internal/view/menu/ActionMenuView$LayoutParams;)V
08-03 12:24:54.618: D/no(5878): New Phone Call Event. Incomming Number : 0123456789
08-03 12:24:54.797: D/onpause(5878): onpause mainactivity
08-03 12:25:14.797: D/no(5878): New Phone Call Event. Incomming Number : 0123456789
08-03 12:25:14.797: D/no(5878): New Phone Call Event. Incomming Number : 0123456789
08-03 12:25:14.857: D/no(5878): New Phone Call Event. Incomming Number : 0123456789
08-03 12:25:15.137: D/onpause(5878): onpause mainactivity
08-03 12:25:21.417: D/dalvikvm(5878): GC_EXPLICIT freed 2421 objects / 175800 bytes in 443ms
08-03 12:25:27.629: D/no(5878): New Phone Call Event. Incomming Number : 0123456789
08-03 12:25:27.629: D/no(5878): New Phone Call Event. Incomming Number : 0123456789
08-03 12:25:27.638: D/no(5878): New Phone Call Event. Incomming Number : 0123456789
08-03 12:25:27.658: D/no(5878): New Phone Call Event. Incomming Number : 0123456789
08-03 12:25:27.738: D/no(5878): New Phone Call Event. Incomming Number : 0123456789
08-03 12:25:28.027: D/onpause(5878): onpause mainactivity
08-03 12:25:36.617: D/no(5878): New Phone Call Event. Incomming Number : 0123456789
08-03 12:25:36.617: D/no(5878): New Phone Call Event. Incomming Number : 0123456789
08-03 12:25:36.627: D/no(5878): New Phone Call Event. Incomming Number : 0123456789
08-03 12:25:36.637: D/no(5878): New Phone Call Event. Incomming Number : 0123456789
08-03 12:25:36.637: D/no(5878): New Phone Call Event. Incomming Number : 0123456789
08-03 12:25:36.647: D/no(5878): New Phone Call Event. Incomming Number : 0123456789
08-03 12:25:36.727: D/no(5878): New Phone Call Event. Incomming Number : 0123456789
08-03 12:25:37.127: D/onpause(5878): onpause mainactivity

0 comments:

Post a Comment