简体   繁体   中英

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. My problem is related to PhoneStateListener which should be called depending on toggle button status:

togglebtn==ON - If ServiceStateChanges or DataConnectionStateChanges -> print a Toast on the screen

togglebtn==OFF - do not check ServiceStateChanges or 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

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:

To unregister a listener, pass the listener object and set the events argument to LISTEN_NONE (0).

You are not passing the listener object -- you are passing null . You need to pass the same PhoneStateListener instance as you used with the original listen() call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM