简体   繁体   English

为什么在发送LISTEN_NONE之后PhoneStateListener仍然存在

[英]Why PhoneStateListener still alive after sending LISTEN_NONE

I'm a beginner with programming in Android OS that is why I've decided to ask your help. 我是Android OS编程的初学者,所以我决定寻求您的帮助。 My problem is related to PhoneStateListener which should be called depending on toggle button status: 我的问题与PhoneStateListener有关,应根据切换按钮状态调用它:

togglebtn==ON - If ServiceStateChanges or DataConnectionStateChanges -> print a Toast on the screen togglebtn==ON如果ServiceStateChanges或DataConnectionStateChanges->在屏幕上打印Toast

togglebtn==OFF - do not check ServiceStateChanges or DataConnectionStateChanges togglebtn==OFF不检查ServiceStateChanges或DataConnectionStateChanges

I've found that in order to stop listening i should send LISTEN_NONE as parameter to listen method, but it does not work 我发现为了停止监听,我应该发送LISTEN_NONE作为监听方法的参数,但它不起作用

Here's the simplest version of my code: 这是我的代码的最简单的版本:

package network.com.example;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends Activity 
{   
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

        ToggleButton button_on_off=(ToggleButton)findViewById(R.id.toggleButton1);

        button_on_off.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
        {     
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
            {         
                if (isChecked) 
                {             
                    nw_update(tm , "onCRE");           

                    tm.listen(new PhoneStateListener() {
                        @Override
                        public void onServiceStateChanged(ServiceState serviceState) {          
                            nw_update(tm , "onSSC");            
                        }
                        @Override
                        public void onDataConnectionStateChanged(int state) {           
                            nw_update(tm , "onDCC");                         
                        }
                    }, PhoneStateListener.LISTEN_SERVICE_STATE | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);   
                }               
                else 
                {                            
                    tm.listen(null,  PhoneStateListener.LISTEN_NONE | PhoneStateListener.LISTEN_NONE ); 
                }
            } 
        });     // end of button on/off                        
    } // end of onCreate

    private final void nw_update(TelephonyManager tm , String rodzaj) {
        String _rodzaj = rodzaj;    
        Toast.makeText(getApplicationContext(), _rodzaj, Toast.LENGTH_SHORT).show();       
    }    
} // end of main activity

Any ideas? 有任何想法吗? I am looking forward to hearing from you. 我期待着您的回音。

Quoting the documentation for listen() , with added emphasis: 将文档引用listen() ,重点是:

To unregister a listener, pass the listener object and set the events argument to LISTEN_NONE (0). 要取消注册侦听器,请传递该侦听器对象 ,并将events参数设置为LISTEN_NONE(0)。

You are not passing the listener object -- you are passing null . 您没有传递侦听器对象,而是传递null You need to pass the same PhoneStateListener instance as you used with the original listen() call. 您需要传递与原始listen()调用相同的 PhoneStateListener实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在我调用stopService后服务仍然(似乎仍)有效 - Why the Service still ( Seem to Be )alive after I call stopService Android:为什么活动结束后PhoneCallListener仍然存在? - Android : why PhoneCallListener still alive after activity finish? 在 onDestroy 之后 Loader 还活着 - Loader is still alive after onDestroy 为什么在 Android 中替换当前片段后,我的 ViewModel 仍然存在? - Why my ViewModel is still alive after I replaced current fragment in Android? 关闭应用程序后,守护进程线程仍然存在 - Daemon Thread is still alive after closing the app 强制停止后,服务仍然有效 - Service still alive after force stop 删除数据库后房间数据库仍然存在 - Room database is still alive after deleting database android 进程在完成活动后仍然存在 - android process still alive after finishing activities BroadcastReceiver和PhoneStateListener侦听呼叫之间的区别 - Difference between a BroadcastReceiver and a PhoneStateListener to listen to calls 在PhoneStateListener上侦听delphi firemonkey中的OnCellInfoChanged的问题 - Problem on PhoneStateListener to listen to OnCellInfoChanged in delphi firemonkey
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM