@Test @Config(minSdk = O) public void shouldSetServiceState() { ServiceState serviceState = new ServiceState(); serviceState.setState(ServiceState.STATE_OUT_OF_SERVICE); shadowOf(telephonyManager).setServiceState(serviceState); assertEquals(serviceState, telephonyManager.getServiceState()); }
public void onClick(View v) { // check current state first boolean state = isAirplaneMode(); // toggle the state toggleAirplaneMode(state); state = isAirplaneMode(); // toggle the state toggleAirplaneMode(state); ser = new ServiceState(); ser.setState(STATE_IN_SERVICE); }
public class SampleBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if(intent.getAction() != null) { Toast.makeText(context, ""+intent.getAction(), Toast.LENGTH_LONG); Log.d("onReceive","intent.getAction():::"+intent.getAction()); if(intent.getAction().equals(android.net.ConnectivityManager.CONNECTIVITY_ACTION)) { ServiceState _state = new ServiceState(); } } } }
public void onClick(View v) { // check current state first boolean state = isAirplaneMode(); // toggle the state toggleAirplaneMode(state); state = isAirplaneMode(); // toggle the state toggleAirplaneMode(state); ser = new ServiceState(); ser.setState(STATE_IN_SERVICE); // you might want to disable the button here // change state back after 5s new Handler().postDelayed(new Runnable() { @Override public void run() { // TODO actually change the state variable // and toggle back the airplane mode // if you disabled the button, enable it here } }, 5000); }
//declare current state ServiceState myServiceState = new ServiceState(); PhoneStateListener listener = null; // not sure if this is needed really.. // nifty getter public ServiceState getServiceState(){ return myServiceState; } //setup listener (eg. in onCreate) TelephonyManager tm = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE); listener = new PhoneStateListener() { @Override public void onServiceStateChanged(ServiceState serviceState){ myServiceState = serviceState; } }; tm.listen(listener,PhoneStateListener.LISTEN_SERVICE_STATE); // to be called when destroying your context public void unregisterListener(){ // something like.. tm.listen(listener,PhoneStateListener.LISTEN_NONE); } //check service ServiceState pstate = getServiceState(); if(pstate.getState() != ServiceState.STATE_IN_SERVICE) { Log.v(TAG,"service state" +pstate.getState()); Toast.makeText(Myactivity.this, "error string", 2000).show(); return; }
mcc = Integer.parseInt(new ServiceState().getOperatorNumeric().substring(0, 3));