[英]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.