java - Android Studio GSM Signal strength not updating automatically -


below code in can gsm , lte signal strength , gps location. gps location updating automatically (private textview longitude; private textview latitude) gsm (private textview gsmss;) , lte signal strength (private textview ltersrp;) values not. need open , close application new value.

package com.example.sarath.myapptesting;   import android.content.context;   import android.support.v7.app.appcompatactivity;   import android.os.bundle;   import android.telephony.phonestatelistener;   import android.telephony.signalstrength;   import android.telephony.telephonymanager;   import android.widget.textview;     import java.lang.reflect.invocationtargetexception;   import java.lang.reflect.method;   import android.manifest;   import android.content.intent;   import android.content.pm.packagemanager;   import android.location.location;   import android.location.locationlistener;   import android.location.locationmanager;   import android.os.build;   import android.provider.settings;   import android.support.annotation.nonnull;   import android.support.annotation.nullable;   import android.support.v4.app.activitycompat;     public class mainactivity extends appcompatactivity {      private textview gsmss;      private textview ltersrp;      private textview nwtype;      private locationmanager lm;      private locationlistener locationlistener;      private textview longitude;      private textview latitude;      private telephonymanager telephonmanager;      private myphonestatelistener pslistener;         protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          longitude = (textview)findviewbyid(r.id.txt2);         latitude = (textview)findviewbyid(r.id.txt1);         lm = (locationmanager)getsystemservice(context.location_service);         locationlistener = new mylocationlistener();         configure_button();          gsmss = (textview)findviewbyid(r.id.textviewgsmss);         ltersrp = (textview)findviewbyid(r.id.textviewltersrpvalue);         nwtype = (textview)findviewbyid(r.id.textviewnwtypevalue);           try{             pslistener = new myphonestatelistener();             telephonmanager = (telephonymanager) getsystemservice(context.telephony_service);             telephonmanager.listen(pslistener, phonestatelistener.listen_signal_strengths);         }         catch(exception e){             e.printstacktrace();         }     }      class mylocationlistener implements locationlistener{          public void onlocationchanged(location location){             if(location != null){                 longitude.settext(string.valueof(location.getlongitude()));                 latitude.settext(string.valueof(location.getlatitude()));             }         }          public void onstatuschanged(string s, int i, bundle bundle){          }          public void onproviderenabled(string s){          }          public void onproviderdisabled(string s){             intent = new intent(settings.action_location_source_settings);             startactivity(i);         }      }      public void onrequestpermissionsresult(int requestcode, @nonnull string[] permissions, @nonnull int[] grantresults) {         switch (requestcode){             case 10:                 configure_button();                 break;             default:                 break;         }     }      void configure_button(){         // first check permissions         if (activitycompat.checkselfpermission(this, manifest.permission.access_fine_location) != packagemanager.permission_granted && activitycompat.checkselfpermission(this, manifest.permission.access_coarse_location) != packagemanager.permission_granted) {             if (build.version.sdk_int >= build.version_codes.m) {                 requestpermissions(new string[]{manifest.permission.access_coarse_location,manifest.permission.access_fine_location,manifest.permission.internet}                         ,10);             }             return;         }         // code won't execute if permissions not allowed, because in line above there return statement.         lm.requestlocationupdates(locationmanager.gps_provider, 500, 0, locationlistener);     }      public string getnetworkclass(context context) {         int networktype = telephonmanager.getnetworktype();         switch (networktype) {             case telephonymanager.network_type_edge:             case telephonymanager.network_type_gprs:                 nwtype.settext(string.valueof("2g"));                 return "2g";              case telephonymanager.network_type_cdma:             case telephonymanager.network_type_1xrtt:             case telephonymanager.network_type_iden:              case telephonymanager.network_type_umts:             case telephonymanager.network_type_evdo_0:             case telephonymanager.network_type_evdo_a:             case telephonymanager.network_type_hsdpa:             case telephonymanager.network_type_hsupa:             case telephonymanager.network_type_hspa:             case telephonymanager.network_type_evdo_b:             case telephonymanager.network_type_ehrpd:             case telephonymanager.network_type_hspap:                 nwtype.settext(string.valueof("3g"));                 return "3g";             case telephonymanager.network_type_lte:                 nwtype.settext(string.valueof("4g"));                 return "4g";             default:                 return "unknown";         }      }       class myphonestatelistener extends phonestatelistener{          public void onsignalstrengthschanged(android.telephony.signalstrength signalstrength){             super.onsignalstrengthschanged(signalstrength);             getnetworkclass(null);             if (signalstrength.isgsm()) {                 int signalstrength = 0;                 signalstrength = signalstrength.getgsmsignalstrength();                 signalstrength = (2 * signalstrength) - 113;                 gsmss.settext(string.valueof(signalstrength + "dbm"));             }              // reflection code starts here             try {                 method[] methods = android.telephony.signalstrength.class.getmethods();                 (method mthd : methods) {                     if (mthd.getname().equals("getltersrp"))                     {                         ltersrp.settext(string.valueof(mthd.invoke(signalstrength)));                     }                  }             } catch (securityexception e) {                 e.printstacktrace();             } catch (illegalargumentexception e) {                 e.printstacktrace();             } catch (illegalaccessexception e) {                 e.printstacktrace();             } catch (invocationtargetexception e) {                 e.printstacktrace();             }             // reflection code ends here         }     } } 


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -