1. Download our Official Android App: Forums for Android!

Error Adding Geofence on Android 8

Discussion in 'Android Development' started by rohit901, Oct 19, 2018.

  1. rohit901

    rohit901 Lurker
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    4
    Joined:
    Oct 19, 2018

    Oct 19, 2018
    4
    1
    16
    I'm not being able to add Geofences in my android App, A similar question was asked here before: Error adding geofences in Android (status code 1000).
    I have gone to Settings->Location and changed my location mode to High Accuracy as suggested in the above link and Tried adding the Geofences, still it gives me the Error. My Device is Oneplus 3T running on Android 8.0.

    Also each time i change my Location Mode to High accuracy and if i Reboot my Device, surprisingly the location mode changes back again to "Device only" Mode.

    I'm following this tutorial to implement Geofence: Android Location Proximity Alert

    The logcat (stack trace) of the problem is shown:
    Code (Text):
    1. 2018-10-19 17:52:48.737 10711-11144/? I/GeofencerStateMachine: addGeofences called by com.example.rohit.geofencefundo
    2. 2018-10-19 17:52:48.745 10711-11059/? W/GeofencerStateMachine: Ignoring addGeofence because network location is disabled.
    3. 2018-10-19 17:52:48.781 26524-26524/com.example.rohit.geofencefundo W/System.err: com.google.android.gms.common.api.ApiException: 1000:
    4. 2018-10-19 17:52:48.783 26524-26524/com.example.rohit.geofencefundo W/System.err:     at com.google.android.gms.common.internal.ApiExceptionUtil.fromStatus(Unknown Source:4)
    5. 2018-10-19 17:52:48.783 26524-26524/com.example.rohit.geofencefundo W/System.err:     at com.google.android.gms.common.internal.zai.zaf(Unknown Source:2)
    6. 2018-10-19 17:52:48.783 26524-26524/com.example.rohit.geofencefundo W/System.err:     at com.google.android.gms.common.internal.zaj.onComplete(Unknown Source:6)
    7. 2018-10-19 17:52:48.784 26524-26524/com.example.rohit.geofencefundo W/System.err:     at com.google.android.gms.common.api.internal.BasePendingResult.zaa(Unknown Source:172)
    8. 2018-10-19 17:52:48.784 26524-26524/com.example.rohit.geofencefundo W/System.err:     at com.google.android.gms.common.api.internal.BasePendingResult.setResult(Unknown Source:131)
    9. 2018-10-19 17:52:48.785 26524-26524/com.example.rohit.geofencefundo W/System.err:     at com.google.android.gms.common.api.internal.BaseImplementation$ApiMethodImpl.setResult(Unknown Source:36)
    10. 2018-10-19 17:52:48.785 26524-26524/com.example.rohit.geofencefundo W/System.err:     at com.google.android.gms.internal.location.zzba.zza(Unknown Source:22)
    11. 2018-10-19 17:52:48.785 26524-26524/com.example.rohit.geofencefundo W/System.err:     at com.google.android.gms.internal.location.zzan.dispatchTransaction(Unknown Source:41)
    12. 2018-10-19 17:52:48.786 26524-26524/com.example.rohit.geofencefundo W/System.err:     at com.google.android.gms.internal.location.zzb.onTransact(Unknown Source:22)
    13. 2018-10-19 17:52:48.786 26524-26524/com.example.rohit.geofencefundo W/System.err:     at android.os.Binder.execTransact(Binder.java:692)
    My Manifest File:

    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    4.    package="com.example.rohit.geofencefundo">
    5.    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    6.  
    7.    <application
    8.        android:allowBackup="true"
    9.        android:icon="@mipmap/ic_launcher"
    10.        android:label="@string/app_name"
    11.        android:roundIcon="@mipmap/ic_launcher_round"
    12.        android:supportsRtl="true"
    13.        android:theme="@style/AppTheme">
    14.        <meta-data
    15.            android:name="com.google.android.gms.version"
    16.            android:value="@integer/google_play_services_version" />
    17.        <meta-data
    18.            android:name="com.google.android.geo.API_KEY"
    19.            android:value="AIzaSyD0tTCVxdhFVzNdOZ6RJtkBba91xBdSb5A"/>
    20.        <activity android:name=".MainActivity">
    21.            <intent-filter>
    22.                <action android:name="android.intent.action.MAIN" />
    23.  
    24.                <category android:name="android.intent.category.LAUNCHER" />
    25.            </intent-filter>
    26.  
    27.        </activity>
    28.        <service android:name=".LocationAlertIntentService" android:allowBackup="true"></service>
    29.    </application>
    30.  
    31.  
    32.  
    33. </manifest>
    My MainActivity:
    Code (Text):
    1.  
    2. package com.example.rohit.geofencefundo;
    3.  
    4. import android.annotation.SuppressLint;
    5. import android.app.Activity;
    6. import android.app.PendingIntent;
    7. import android.content.Intent;
    8. import android.content.pm.PackageManager;
    9. import android.support.annotation.NonNull;
    10. import android.support.v4.app.ActivityCompat;
    11. import android.support.v4.content.ContextCompat;
    12. import android.support.v7.app.AppCompatActivity;
    13. import android.os.Bundle;
    14. import android.support.v7.widget.Toolbar;
    15. import android.util.Log;
    16. import android.view.Menu;
    17. import android.view.MenuInflater;
    18. import android.view.MenuItem;
    19. import android.view.View;
    20. import android.widget.Button;
    21. import android.widget.Toast;
    22.  
    23. import com.google.android.gms.common.ConnectionResult;
    24. import com.google.android.gms.common.api.GoogleApiClient;
    25. import com.google.android.gms.common.api.ResultCallback;
    26. import com.google.android.gms.common.api.Status;
    27. import com.google.android.gms.location.Geofence;
    28. import com.google.android.gms.location.GeofencingClient;
    29. import com.google.android.gms.location.GeofencingRequest;
    30. import com.google.android.gms.location.LocationServices;
    31. import com.google.android.gms.maps.GoogleMap;
    32. import com.google.android.gms.maps.OnMapReadyCallback;
    33. import com.google.android.gms.maps.SupportMapFragment;
    34. import com.google.android.gms.maps.model.LatLng;
    35. import com.google.android.gms.tasks.OnCompleteListener;
    36. import com.google.android.gms.tasks.OnFailureListener;
    37. import com.google.android.gms.tasks.OnSuccessListener;
    38. import com.google.android.gms.tasks.Task;
    39.  
    40. import java.util.ArrayList;
    41. import java.util.Map;
    42.  
    43. public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
    44.    private static final int LOC_PERM_REQ_CODE = 1;
    45.    //meters
    46.    private static final int GEOFENCE_RADIUS = 2500;
    47.    //in milli seconds
    48.    private static final int GEOFENCE_EXPIRATION = 6000;
    49.    private GoogleMap mMap;
    50.  
    51.  
    52.  
    53.    private GeofencingClient geofencingClient;
    54.  
    55.    @Override
    56.    protected void onCreate(Bundle savedInstanceState) {
    57.        super.onCreate(savedInstanceState);
    58.        setContentView(R.layout.activity_main);
    59.  
    60.  
    61.  
    62.  
    63.       /* geofencingClient = LocationServices.getGeofencingClient(this);
    64.        showCurrentLocationOnMap();*/
    65.  
    66.  
    67.  
    68.        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
    69.                .findFragmentById(R.id.g_map);
    70.        mapFragment.getMapAsync(this);
    71.  
    72.        geofencingClient = LocationServices.getGeofencingClient(this);
    73.  
    74.  
    75.  
    76.  
    77.    }
    78.    @Override
    79.    public void onMapReady(GoogleMap googleMap) {
    80.        mMap = googleMap;
    81.  
    82.        mMap.getUiSettings().setZoomControlsEnabled(true);
    83.        mMap.setMinZoomPreference(15);
    84.  
    85.        showCurrentLocationOnMap();
    86.  
    87.        mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
    88.            @Override
    89.            public void onMapClick(LatLng latLng) {
    90.                addLocationAlert(latLng.latitude, latLng.longitude);
    91.            }
    92.        });
    93.    }
    94.    @SuppressLint("MissingPermission")
    95.    private void showCurrentLocationOnMap() {
    96.        if (isLocationAccessPermitted()) {
    97.            requestLocationAccessPermission();
    98.        } else if (mMap != null) {
    99.            mMap.setMyLocationEnabled(true);
    100.        }
    101.    }
    102.    /*public void addFence(View view){
    103.  
    104.        addLocationAlert(28.360596,75.5863917); //28.360596 75.5863917  budh bhavan
    105.        addLocationAlert( 28.3603219,75.5868534); //ram marg
    106.  
    107.    }*/
    108.  
    109.  
    110.  
    111.  
    112.  
    113.  
    114.    /*@SuppressLint("MissingPermission")
    115.    private void showCurrentLocationOnMap() {
    116.        if (isLocationAccessPermitted()) {
    117.            requestLocationAccessPermission();
    118.        }
    119.    }*/
    120.  
    121.    private boolean isLocationAccessPermitted() {
    122.        if (ContextCompat.checkSelfPermission(this,
    123.                android.Manifest.permission.ACCESS_FINE_LOCATION)
    124.                != PackageManager.PERMISSION_GRANTED) {
    125.            return true;
    126.        } else {
    127.            return false;
    128.        }
    129.    }
    130.  
    131.    private void requestLocationAccessPermission() {
    132.        ActivityCompat.requestPermissions(this,
    133.                new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
    134.                LOC_PERM_REQ_CODE);
    135.    }
    136.  
    137.    @SuppressLint("MissingPermission")
    138.    private void addLocationAlert(double lat, double lng) {
    139.        if (isLocationAccessPermitted()) {
    140.            requestLocationAccessPermission();
    141.        } else {
    142.            String key = "" + lat + "-" + lng;
    143.            Geofence geofence = getGeofence(lat, lng, key);
    144.           /* geofencingClient.addGeofences(getGeofencingRequest(geofence),
    145.                    getGeofencePendingIntent())
    146.                    .addOnCompleteListener(new OnCompleteListener<Void>() {
    147.                        @Override
    148.                        public void onComplete(@NonNull Task<Void> task) {
    149.                            if (task.isSuccessful()) {
    150.                                Toast.makeText(MainActivity.this,
    151.                                        "Location alter has been added",
    152.                                        Toast.LENGTH_SHORT).show();
    153.                            } else {
    154.                                Toast.makeText(MainActivity.this,
    155.                                        "Location alter could not be added",
    156.                                        Toast.LENGTH_SHORT).show();
    157.                            }
    158.                        }
    159.                    });*/
    160.  
    161.  
    162.            geofencingClient.addGeofences(getGeofencingRequest(geofence),
    163.                    getGeofencePendingIntent())
    164.                    .addOnSuccessListener(this, new OnSuccessListener<Void>() {
    165.                        @Override
    166.                        public void onSuccess(Void aVoid) {
    167.                            Toast.makeText(MainActivity.this, "added", Toast.LENGTH_SHORT).show();
    168.  
    169.                        }
    170.                    })
    171.                    .addOnFailureListener(this, new OnFailureListener() {
    172.                        @Override
    173.                        public void onFailure(@NonNull Exception e) {
    174.  
    175.                            Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
    176.                            e.printStackTrace();
    177.  
    178.                        }
    179.                    });
    180.  
    181.  
    182.        }
    183.    }
    184.  
    185.    private void removeLocationAlert() {
    186.        if (isLocationAccessPermitted()) {
    187.            requestLocationAccessPermission();
    188.        } else {
    189.            geofencingClient.removeGeofences(getGeofencePendingIntent())
    190.                    .addOnCompleteListener(new OnCompleteListener<Void>() {
    191.                        @Override
    192.                        public void onComplete(@NonNull Task<Void> task) {
    193.                            if (task.isSuccessful()) {
    194.                                Toast.makeText(MainActivity.this,
    195.                                        "Location alters have been removed",
    196.                                        Toast.LENGTH_SHORT).show();
    197.                            } else {
    198.                                Toast.makeText(MainActivity.this,
    199.                                        "Location alters could not be removed",
    200.                                        Toast.LENGTH_SHORT).show();
    201.                            }
    202.                        }
    203.                    });
    204.        }
    205.    }
    206.  
    207.    @Override
    208.    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
    209.                                           @NonNull int[] grantResults) {
    210.        switch (requestCode) {
    211.            case LOC_PERM_REQ_CODE: {
    212.                if (grantResults.length > 0
    213.                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    214.                    showCurrentLocationOnMap();
    215.                    Toast.makeText(MainActivity.this,
    216.                            "Location access permission granted, you try " +
    217.                                    "add or remove location allerts",
    218.                            Toast.LENGTH_SHORT).show();
    219.                }
    220.                return;
    221.            }
    222.  
    223.        }
    224.    }
    225.  
    226.    private PendingIntent getGeofencePendingIntent() {
    227.        Intent intent = new Intent(this, LocationAlertIntentService.class);
    228.        return PendingIntent.getService(this, 0, intent,
    229.                PendingIntent.FLAG_UPDATE_CURRENT);
    230.    }
    231.  
    232.    private GeofencingRequest getGeofencingRequest(Geofence geofence) {
    233.        GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
    234.  
    235.        builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_DWELL);
    236.        builder.addGeofence(geofence);
    237.        return builder.build();
    238.    }
    239.  
    240.    private Geofence getGeofence(double lat, double lang, String key) {
    241.        return new Geofence.Builder()
    242.                .setRequestId(key)
    243.                .setCircularRegion(lat, lang, GEOFENCE_RADIUS)
    244.                .setExpirationDuration(Geofence.NEVER_EXPIRE)
    245.                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
    246.                        Geofence.GEOFENCE_TRANSITION_DWELL)
    247.                .setLoiteringDelay(10000)
    248.                .build();
    249.    }
    250.  
    251.    @Override
    252.    public boolean onCreateOptionsMenu(Menu menu) {
    253.        MenuInflater inflater = getMenuInflater();
    254.        inflater.inflate(R.menu.main_menu, menu);
    255.        return true;
    256.    }
    257.  
    258.    @Override
    259.    public boolean onOptionsItemSelected(MenuItem item) {
    260.        switch (item.getItemId()) {
    261.            case R.id.remove_loc_alert:
    262.                removeLocationAlert();
    263.                return true;
    264.            default:
    265.                return super.onOptionsItemSelected(item);
    266.  
    267.        }
    268.    }
    269.  
    270.  
    271. }
    The LocationAlertIntentService is the same as shown in the tutorial which I'm referring to.

    My Build.gradle (Module:app)
    Code (Text):
    1. apply plugin: 'com.android.application'
    2.  
    3. android {
    4.    compileSdkVersion 28
    5.    defaultConfig {
    6.        applicationId "com.example.rohit.geofencefundo"
    7.        minSdkVersion 19
    8.        targetSdkVersion 28
    9.        versionCode 1
    10.        versionName "1.0"
    11.        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    12.    }
    13.    buildTypes {
    14.        release {
    15.            minifyEnabled false
    16.            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    17.        }
    18.    }
    19. }
    20.  
    21. dependencies {
    22.    implementation fileTree(dir: 'libs', include: ['*.jar'])
    23.    implementation 'com.android.support:appcompat-v7:28.0.0'
    24.    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    25.    implementation 'com.google.android.gms:play-services-location:16.0.0'
    26.    implementation 'com.google.android.gms:play-services-maps:16.0.0'
    27.    testImplementation 'junit:junit:4.12'
    28.    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    29.    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    30. }
    any help would be really appreciated. Thank you so much.
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,673
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,673
    11,108
    1,988
    Male
    Software developer
    South West of England
    Have you tried this?

     
  3. rohit901

    rohit901 Lurker
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    4
    Joined:
    Oct 19, 2018

    Oct 19, 2018
    4
    1
    16
    Yeah, I run the app on my physical device and not the emulator itself, and yes I have changed the location settings by going into settings - >location and changed the mode to "high accuracy" which uses GPS mobile data and wifi to measure the location, it was at "Device only" before which only uses GPS to get the location and whenever I reboot or restart my device it changes back to "Device mode" again..
     
  4. rohit901

    rohit901 Lurker
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    4
    Joined:
    Oct 19, 2018

    Oct 19, 2018
    4
    1
    16
    umm guys the geofences seems to be adding in Non Oreo Devices, I tried on my friend's mobile which has Android 7 and it is adding on that so it seems to be a problem with my Android Oreo, Any suggestions on how I can fix it?
     
    AaronHirst likes this.
  5. AaronHirst

    AaronHirst Lurker
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Nov 7, 2018

    Nov 7, 2018
    2
    0
    5
    Did you find a solution for this? I'm also trying to add geofences for an app but got the same error on my OnePlus 5t but works on a Samsung S7.
     
  6. rohit901

    rohit901 Lurker
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    4
    Joined:
    Oct 19, 2018

    Oct 19, 2018
    4
    1
    16
    Hey Aaron, Yes i found the workaround for this, it turns out to be the google location sharing dialog which is the main culprit in this, so this is how I fixed it , I went to Location->Mode->, and then changed the location mode to Battery Saver this time and not High accuracy, and I guess I kept clicking on it till I got the Google Location sharing alert dialog pop up which asked me whether I wanted to share the location data with Google to improve accuracy, I clicked on yes and after that I was able to add geofence without problem, so you could also clear the google location cache or google play service cache and also try clearing the cache of Google services framework, the trick here is to make the google alert dialog pop up which asks whether you want to share the location with google or not after that geofence starts working fine
     

Share This Page

Loading...