Android:我的广播接收器中的代码重复执行

时间:2014-08-03 07:14:48

标签: android broadcastreceiver phone-state-listener

Hai iam刚开始使用广播接收器进行Android开发...我的应用程序有一个广播接收器和一个类( MyPhoneStateListener.java ),它扩展了 PhoneStateListener 通知来电的电话号码。这里的问题是我在MyPhoneStateListener.java中有一个Log.d(“no”,msg)语句来查看logcat中的传入呼叫号码。当我第一次启动我的应用程序并进行测试时,电话号码会显示一次就好了,当我第二次测试时,电话号码显示三次(语句或类别可能会被执行3次),当我再次测试时,电话号码会显示5次等等...我在下面发布了我的代码,请检查并帮我解决。

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 个答案:

没有答案