Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PhoneStateListener从未在广播android中调用_Android_Android Emulator_Broadcastreceiver_Telephonymanager_Phone State Listener - Fatal编程技术网

PhoneStateListener从未在广播android中调用

PhoneStateListener从未在广播android中调用,android,android-emulator,broadcastreceiver,telephonymanager,phone-state-listener,Android,Android Emulator,Broadcastreceiver,Telephonymanager,Phone State Listener,我的广播被调用了,但PhoneStateListener没有被调用。当我在emulator中运行它时,我的代码工作正常,但当我在实际设备上尝试它时,PhoneStateListener从未得到调用,我对这个问题非常恼火 这是我的密码: TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); //TelephonyManager object

我的广播被调用了,但PhoneStateListener没有被调用。当我在emulator中运行它时,我的代码工作正常,但当我在实际设备上尝试它时,PhoneStateListener从未得到调用,我对这个问题非常恼火 这是我的密码:

TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); //TelephonyManager object  
                            CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();  
                            telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); //Register our listener with TelephonyManager 
上面的代码在onReceive()方法中,下面是我扩展PhoneStateListener的类 在onReceive()之外,但在广播类中

public class CustomPhoneStateListener extends PhoneStateListener {  

        private static final String TAG = "CustomPhoneStateListener";  



        @Override  
        public void onCallStateChanged(int state, String phonenumber){  

            if(phonenumber!=null && phonenumber.length()>0) 
                incoming_nr=phonenumber;   
            act=new Call_RecorderActivity();

            switch(state){  
                case TelephonyManager.CALL_STATE_RINGING:  
                        Log.d(TAG, "CALL_STATE_RINGING");  
                        prev_state=state;  


                        break;  


                case TelephonyManager.CALL_STATE_OFFHOOK:  
                Log.d(TAG, "CALL_STATE_OFFHOOK");  
                prev_state=state;  

                break;  


                case TelephonyManager.CALL_STATE_IDLE:  
                     prev_state=state;
                     Log.d(TAG, "CALL_STATE_IDLE==>"+incoming_nr);



                    break;  
                   // Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT
                   // |Intent.FLAG_ACTIVITY_REORDER_TO_FRONT

            } //switch close
        }
}
logcat在来电时显示此信息

01-22 11:25:12.529: I/IncomingCallReceiver(1463): Bundle[mParcelledData.dataSize=168]
01-22 11:25:12.539: I/IncomingCallReceiver(1463): State: RINGING
01-22 11:25:12.539: I/IncomingCallReceiver(1463): Incomng Number: +9184848xxxx2
01-22 11:25:12.779: D/CustomPhoneStateListener(1463): CALL_STATE_IDLE==>+9184848xxxx2
01-22 11:25:16.299: I/IncomingCallReceiver(1463): Bundle[mParcelledData.dataSize=92]
01-22 11:25:16.299: I/IncomingCallReceiver(1463): State: OFFHOOK
01-22 11:25:18.849: I/IncomingCallReceiver(1463): Bundle[mParcelledData.dataSize=88]
01-22 11:25:18.849: I/IncomingCallReceiver(1463): State: IDLE
任何与此相关的建议都将被接受
提前感谢

在调用回调之前,您的服务将得到清理。您不应该真的依赖于
BroadcastReceiver
中分配的任何东西在
onReceive
退出后存在。您应该将
CustomPhoneStateListener
放在
服务
活动
中。然后,您可以使用
Intent
启动活动或服务以进行状态监视

广播接收器
文档

从onReceive()返回后,BroadcastReceiver不再是 活动,并且它的宿主进程只与其他进程一样重要 在其中运行的应用程序组件。这尤其重要 重要的是,如果该进程仅承载 BroadcastReceiver(用户拥有的应用程序的常见情况 从未或最近未与之互动),然后从 系统将考虑它的进程是空的和 积极地杀死它,以便为其他更多的人提供资源 重要过程


它在emulator上工作的原因可能是因为emulator上的进程通常较少,并且出于某些原因,它似乎不太积极地终止进程。

t上述问题的解决方案是:

private final PhoneStateListener phoneStateListener = new PhoneStateListener() {  

    @Override 

    public void onCallStateChanged(int state, String incomingNumber) {  

        String callState = "UNKNOWN";  

        String myNumber = tm.getLine1Number();
        switch (state) {  

        case TelephonyManager.CALL_STATE_IDLE:  

            callState = "IDLE";
            if(Status!=""){
                Toast.makeText(mContext,"Call Ends " + incomingNumber,Toast.LENGTH_LONG).show();
            }
            break;  

        case TelephonyManager.CALL_STATE_RINGING: 
            Status = "RINGING";
            if (incomingNumber.startsWith("00")) {  
                Toast.makeText(mContext,"International Call- " + incomingNumber,Toast.LENGTH_LONG).show();  
                callState = "International - Ringing (" + incomingNumber+ ")";  
            } else {  
                Toast.makeText(mContext, "Local Call - " + incomingNumber, Toast.LENGTH_LONG).show();  
                callState = "Local - Ringing (" + incomingNumber + ")";  
            }  
            break;  
        case TelephonyManager.CALL_STATE_OFFHOOK:  

            try{
                String dialingNumber = mIntent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);  
                if(dialingNumber==null){
                    Status = "Recieve";
                    Toast.makeText(mContext, "Recieve call", Toast.LENGTH_LONG).show();
                }else{
                    Status = "Dialing";
                    if (dialingNumber.startsWith("00")) {  
                        Toast.makeText(mContext,"International - " + dialingNumber,Toast.LENGTH_LONG).show();  
                        callState = "International - Dialing (" + dialingNumber+ ")";  
                    } else {  
                        Toast.makeText(mContext, "Local Call - " + dialingNumber,Toast.LENGTH_LONG).show();  
                        callState = "Local - Dialing (" + dialingNumber + ")";  
                    }  
                }
            }catch(Exception e){}

            break;  

        }  

        Log.i(">>>Broadcast", "onCallStateChanged " + callState);  

        super.onCallStateChanged(state, incomingNumber);  

    }  

}; 

请确保您已在清单中添加了
android.permission.READ\u PHONE\u STATE
权限是的,我已添加该权限显示您的日志和类别您是否在清单中发布了收件人,或者您是在运行时将其注册到
registerReceiver
?@Auto Droid它在仿真器上工作的原因可能是因为仿真器上的进程通常较少,并且出于某种原因,它似乎没有那么激进地终止进程。因此,您的进程仍在emulator的内存中,但不在设备上。这并没有改变这样一个事实,即根据文档,您无法保证该过程将得以保留。因此,您上面编写的代码可能对任何给定的调用都有效,也可能对任何给定的调用都无效。我接受了服务,该服务将为我检测电话状态,但我的服务被调用,但我的侦听器未被调用的问题仍然存在。您在侦听什么广播意图来启动您的服务?我能够将您的代码复制到
服务的
onStartCommand
,它在真实设备上记录了所有状态消息。context.startService(newintent(context,MyService.class))@自动机器人我是说什么触发了你的广播接收器?