Listener is getting executed each time the Activity starts
public class example extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
开发者_高级运维 super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PhoneStateListener myListener = new PhoneStateListener() {
@Override
public void onServiceStateChanged (ServiceState serviceState) {
// Some stuff
}
};
TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
mTelephonyManager.listen(myListener, PhoneStateListener.LISTEN_SERVICE_STATE);
}
Here is the code. Problem is Listener is getting executed each time the Activity starts rather than only on event listen. Please let me know how can I resolved this issue.
Perhaps the network service state indeed is changing quite frequently. This might make it seem that the listener is being called as soon as the Activity starts (which is when the listener is getting attached).
精彩评论