android - requestPermissions() for gps inside a service . Error:"location provider requires ACCESS_FINE_LOCATION permission." -
the problem facing giving gps permissions application through service writing. tried finding example achieve , examples shows writing gpslocation finder in activity, in case, implement in service.
the service code follows:
public class travelservice extends service{ private locationmanager locmanager; private locationlistener loclistener; @override public void oncreate() { super.oncreate(); locmanager = (locationmanager) getsystemservice(location_service); loclistener = new locationlistener() { @override public void onlocationchanged(location location) { locmanager.requestlocationupdates("gps", 5000, 100, loclistener); } @override public void onstatuschanged(string provider, int status, bundle extras) { } @override public void onproviderenabled(string provider) { } @override public void onproviderdisabled(string provider) { } }; if (build.version.sdk_int >= build.version_codes.m) { if (activitycompat.checkselfpermission(this, manifest.permission.access_fine_location) != packagemanager.permission_granted && activitycompat.checkselfpermission(this, manifest.permission.access_coarse_location) != packagemanager.permission_granted) { // todo: consider calling // activitycompat#requestpermissions // here request missing permissions, , overriding // public void onrequestpermissionsresult(int requestcode, string[] permissions, // int[] grantresults) // handle case user grants permission. see documentation // activitycompat#requestpermissions more details. return; } locmanager.requestlocationupdates("gps", 5000, 100, loclistener); toast.maketext(this, "service created", toast.length_long).show(); } } @override public int onstartcommand(intent intent, int flags, int startid) { locmanager.requestlocationupdates("gps", 5000, 100, loclistener); toast.maketext(this,"service started",toast.length_long).show(); return super.onstartcommand(intent, flags, startid); } @override public void ondestroy() { super.ondestroy(); toast.maketext(this,"service destroyed",toast.length_long).show(); } @nullable @override public ibinder onbind(intent intent) { return null; } }
here code in manifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.param.dateandtime"> <uses-permission android:name="android.permission.internet"/> <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"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <!-- entry loginactivity.class --> <activity android:name=".loginactivity" android:label="@string/app_name"></activity> <activity android:name=".finish" android:label="@string/app_name"></activity> <service android:name=".travelservice" android:exported="false"/> </application> </manifest>
here have attached error log, showing error i'm facing. can see error "location provider requires access_fine_location permission." error log:
you cannot request permissions service since service not tied ui. activity when startservice()
.
activitycompat.requestpermissions(this,new string[]{manifest.permission.access_fine_location, manifest.permission.access_coarse_location}, 1001); //any number can used
and implement onrequestpermissionsresult()
callback
@override public void onrequestpermissionsresult(int requestcode, @nonnull string[] permissions, @nonnull int[] grantresults) { super.onrequestpermissionsresult(requestcode, permissions, grantresults); switch (requestcode) { case 1001: { if (grantresults.length > 0 && grantresults[0] == packagemanager.permission_granted && (activitycompat.checkselfpermission(getactivity(), manifest.permission.access_fine_location) == packagemanager.permission_granted || activitycompat.checkselfpermission(getactivity(), manifest.permission.access_coarse_location) == packagemanager.permission_granted)) { //start service here } } } }
Comments
Post a Comment