android - Weather is not showing when run on real device -
i use http://api.openweathermap.org/data/2.5/weather show weather working in emulator when send location in using location setting not working on real device. not show error message in log.
can 1 tell me doing wrong?
i add permissions in androidmanifest.xml
also.
mainactivity.java
public class mainactivity extends appcompatactivity implements locationlistener { textview txtcity, txtlastupdate, txtdescription, txthumidity, txttime, txtcelsius; imageview imageview; locationmanager locationmanager; string provider; static double lat, lng; openweathermap openweathermap = new openweathermap(); int my_permission = 0; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //control txtcity = (textview) findviewbyid(r.id.txtcity); txtlastupdate = (textview) findviewbyid(r.id.txtlastupdate); txtdescription = (textview) findviewbyid(r.id.txtdescription); txthumidity = (textview) findviewbyid(r.id.txthumidity); txttime = (textview) findviewbyid(r.id.txttime); txtcelsius = (textview) findviewbyid(r.id.txtcelsius); imageview = (imageview) findviewbyid(r.id.imageview); //get coordinates locationmanager = (locationmanager) getsystemservice(context.location_service); provider = locationmanager.getbestprovider(new criteria(), false); if (activitycompat.checkselfpermission(this, manifest.permission.access_fine_location) != packagemanager.permission_granted && activitycompat.checkselfpermission(this, manifest.permission.access_coarse_location) != packagemanager.permission_granted) { activitycompat.requestpermissions(mainactivity.this, new string[]{ manifest.permission.internet, manifest.permission.access_coarse_location, manifest.permission.access_fine_location, manifest.permission.access_network_state, manifest.permission.system_alert_window, manifest.permission.write_external_storage }, my_permission); } location location = locationmanager.getlastknownlocation(provider); if (location == null) log.e("tag","no location"); } @override protected void onpause() { super.onpause(); if (activitycompat.checkselfpermission(this, manifest.permission.access_fine_location) != packagemanager.permission_granted && activitycompat.checkselfpermission(this, manifest.permission.access_coarse_location) != packagemanager.permission_granted) { activitycompat.requestpermissions(mainactivity.this, new string[]{ manifest.permission.internet, manifest.permission.access_coarse_location, manifest.permission.access_fine_location, manifest.permission.access_network_state, manifest.permission.system_alert_window, manifest.permission.write_external_storage }, my_permission); } locationmanager.removeupdates(this); } @override protected void onresume() { super.onresume(); if (activitycompat.checkselfpermission(this, manifest.permission.access_fine_location) != packagemanager.permission_granted && activitycompat.checkselfpermission(this, manifest.permission.access_coarse_location) != packagemanager.permission_granted) { activitycompat.requestpermissions(mainactivity.this, new string[]{ manifest.permission.internet, manifest.permission.access_coarse_location, manifest.permission.access_fine_location, manifest.permission.access_network_state, manifest.permission.system_alert_window, manifest.permission.write_external_storage }, my_permission); } locationmanager.requestlocationupdates(provider, 400, 1, this); } @override public void onlocationchanged(location location) { lat = location.getlatitude(); lng = location.getlongitude(); new getweather().execute(common.apirequest(string.valueof(lat),string.valueof(lng))); } @override public void onstatuschanged(string provider, int status, bundle extras) { } @override public void onproviderenabled(string provider) { } @override public void onproviderdisabled(string provider) { } private class getweather extends asynctask<string,void,string>{ progressdialog pd = new progressdialog(mainactivity.this); @override protected void onpreexecute() { super.onpreexecute(); pd.settitle("please wait..."); pd.show(); } @override protected string doinbackground(string... params) { string stream = null; string urlstring = params[0]; helper http = new helper(); stream = http.gethttpdata(urlstring); return stream; } @override protected void onpostexecute(string s) { super.onpostexecute(s); if(s.contains("error: not found city")){ pd.dismiss(); return; } gson gson = new gson(); type mtype = new typetoken<openweathermap>(){}.gettype(); openweathermap = gson.fromjson(s,mtype); pd.dismiss(); txtcity.settext(string.format("%s,%s",openweathermap.getname(),openweathermap.getsys().getcountry())); txtlastupdate.settext(string.format("last updated: %s", common.getdatenow())); txtdescription.settext(string.format("%s",openweathermap.getweather().get(0).getdescription())); txthumidity.settext(string.format("%d%%",openweathermap.getmain().gethumidity())); txttime.settext(string.format("%s/%s",common.unixtimestamptodatetime(openweathermap.getsys().getsunrise()),common.unixtimestamptodatetime(openweathermap.getsys().getsunset()))); txtcelsius.settext(string.format("%.2f °c",openweathermap.getmain().gettemp())); picasso.with(mainactivity.this) .load(common.getimage(openweathermap.getweather().get(0).geticon())) .into(imageview); } } }
common.java
public class common { public static string api_key = "mykey"; public static string api_link = "http://api.openweathermap.org/data/2.5/weather"; @nonnull public static string apirequest(string lat, string lng){ stringbuilder sb = new stringbuilder(api_link); sb.append(string.format("?lat=%s&lon=%s&appid=%s&units=metric",lat,lng,api_key)); return sb.tostring(); } public static string unixtimestamptodatetime(double unixtimestamp){ dateformat dateformat = new simpledateformat("hh:mm"); date date = new date(); date.settime((long)unixtimestamp*1000); return dateformat.format(date); } public static string getimage(string icon){ return string.format("http://openweathermap.org/img/w/%s.png",icon); } public static string getdatenow(){ dateformat dateformat = new simpledateformat("dd mmmm yyyy hh:mm"); date date = new date(); return dateformat.format(date); }
}
the method getlastknownlocation
location info when last time, it's may null.
you can reference other question keywords: getlastknownlocation return null
these question you, may be.
getlastknownlocation return null after re-install apk file via eclipse
Comments
Post a Comment