android - Getting error in LocationManager -
i have been trying current address without mentioning latitude , longitude implicitly inside code using locationmanager using gps
iam getting 1 error in locationmanager line in below program:
package com.example.admin.getcurrentlocation; import java.io.ioexception; import java.util.list; import java.util.locale; import android.app.activity; import android.app.alertdialog; import android.content.contentresolver; import android.content.context; import android.content.dialoginterface; import android.content.intent; import android.content.pm.activityinfo; import android.location.address; import android.location.geocoder; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle; import android.provider.settings; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.progressbar; import android.widget.toast; public class mainactivity extends activity implements onclicklistener { private locationmanager locationmangaer=null; private locationlistener locationlistener=null; private button btngetlocation = null; private edittext editlocation = null; private progressbar pb =null; private static final string tag = "debug"; private boolean flag = false; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //if want lock screen portrait mode setrequestedorientation(activityinfo .screen_orientation_portrait); pb = (progressbar) findviewbyid(r.id.progressbar1); pb.setvisibility(view.invisible); editlocation = (edittext) findviewbyid(r.id.edittextlocation); btngetlocation = (button) findviewbyid(r.id.btnlocation); btngetlocation.setonclicklistener(this); locationmangaer = (locationmanager) getsystemservice(context.location_service); } @override public void onclick(view v) { flag = displaygpsstatus(); if (flag) { log.v(tag, "onclick"); editlocation.settext("please!! move device to"+ " see changes in coordinates."+"\nwait.."); pb.setvisibility(view.visible); locationlistener = new mylocationlistener(); <--------------error in line-------------> locationmangaer.requestlocationupdates(locationmanager.gps_provider, 5000, 10,locationlistener); } else { alertbox("gps status!!", "your gps is: off"); } } /*----method check gps enable or disable ----- */ private boolean displaygpsstatus() { contentresolver contentresolver = getbasecontext() .getcontentresolver(); boolean gpsstatus = settings.secure .islocationproviderenabled(contentresolver, locationmanager.gps_provider); if (gpsstatus) { return true; } else { return false; } } /*----------method create alertbox ------------- */ protected void alertbox(string title, string mymessage) { alertdialog.builder builder = new alertdialog.builder(this); builder.setmessage("your device's gps disable") .setcancelable(false) .settitle("** gps status **") .setpositivebutton("gps on", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { // finish current activity // alertboxadvance.this.finish(); intent myintent = new intent( settings.action_security_settings); startactivity(myintent); dialog.cancel(); } }) .setnegativebutton("cancel", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { // cancel dialog box dialog.cancel(); } }); alertdialog alert = builder.create(); alert.show(); } /*----------listener class coordinates ------------- */ private class mylocationlistener implements locationlistener { @override public void onlocationchanged(location loc) { editlocation.settext(""); pb.setvisibility(view.invisible); toast.maketext(getbasecontext(),"location changed : lat: " + loc.getlatitude()+ " lng: " + loc.getlongitude(), toast.length_short).show(); string longitude = "longitude: " +loc.getlongitude(); log.v(tag, longitude); string latitude = "latitude: " +loc.getlatitude(); log.v(tag, latitude); /*----------to city-name coordinates ------------- */ string cityname=null; geocoder gcd = new geocoder(getbasecontext(), locale.getdefault()); list<address> addresses; try { addresses = gcd.getfromlocation(loc.getlatitude(), loc .getlongitude(), 1); if (addresses.size() > 0) system.out.println(addresses.get(0).getlocality()); cityname=addresses.get(0).getlocality(); } catch (ioexception e) { e.printstacktrace(); } string s = longitude+"\n"+latitude + "\n\nmy currrent city is: "+cityname; editlocation.settext(s); } @override public void onproviderdisabled(string provider) { // todo auto-generated method stub } @override public void onproviderenabled(string provider) { // todo auto-generated method stub } @override public void onstatuschanged(string provider, int status, bundle extras) { // todo auto-generated method stub } } }
here logcat.
process: com.example.admin.getcurrentlocation, pid: 3006 java.lang.securityexception: "gps" location provider requires access_fine_location permission. @ android.os.parcel.readexception(parcel.java:1599) @ android.os.parcel.readexception(parcel.java:1552) @ android.location.ilocationmanager$stub$proxy.requestlocationupdates(ilocationmanager.java:606) @ android.location.locationmanager.requestlocationupdates(locationmanager.java:885) @ android.location.locationmanager.requestlocationupdates(locationmanager.java:469) @ com.example.admin.getcurrentlocation.mainactivity.onclick(mainactivity.java:77) @ android.view.view.performclick(view.java:5269) @ android.view.view$performclick.run(view.java:21556) @ android.os.handler.handlecallback(handler.java:815) @ android.os.handler.dispatchmessage(handler.java:104) @ android.os.looper.loop(looper.java:207) @ android.app.activitythread.main(activitythread.java:5769) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:789) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:679) manifest file: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.admin.getcurrentlocation"> <uses-permission android:name="android.permission.access_fine_location"/> <uses-permission android:name="android.permission.access_coarse_location" /> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsrtl="true" android:theme="@style/apptheme"> <activity android:name=".mainactivity" android:label="@string/app_name" android:theme="@style/apptheme.noactionbar"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
looking missing add below permission in manifest.
as gps location provider requires permission.
<uses-permission android:name="android.permission.access_fine_location" /> <uses-permission android:name="android.permission.access_coarse_location" />
Comments
Post a Comment