android - How to register a geofence on phone reboot? -


i know have use boot_completed , broadcast receiver. need sample registering geofence broadcast receiver.i created geofence service calss , tried start broadcastreceiver did not work.

public class bootcompletereceiver extends broadcastreceiver  {   @override public void onreceive(context context, intent intent) {     //or whatever action receiver accepts     if(intent.getaction().equals(intent.action_boot_completed)){         toast.maketext(context , "app rebbot received" , toast.length_long).show();         intent serviceintent = new intent(context , geofenceobserversationservice.class);         context.startservice(serviceintent);        //  geofenceobserversationservice.getinstant().addgeofences();      } }} 

here service class

here manifest file.

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cctspl.geofenceex"> <uses-permission android:name="android.permission.access_fine_location" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="com.google.android.providers.gsf.permission.read_gservices" /> <uses-permission android:name="android.permission.receive_boot_completed"></uses-permission>  <application     android:allowbackup="true"     android:name=".myapplication"     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>     <service         android:name=".geofencetransitionsintentservice"         android:exported="true" />     <service android:name=".geofenceobserversationservice"/>     <receiver android:name=".bootcompletereceiver"         android:exported="true">         <intent-filter>             <action android:name="android.intent.action.boot_completed"/>         </intent-filter>     </receiver>     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"></meta-data>     <meta-data android:name="com.google.android.maps.v2.api_key" android:value="aizasydcbhjgbc7otfdrwkjlnag9aqu4_-3ivaw" /> </application> 

public class geofenceobserversationservice extends service implements googleapiclient.connectioncallbacks , googleapiclient.onconnectionfailedlistener , resultcallback<status> { protected static final string tag = "geofenceobserversationservice"; protected googleapiclient mgoogleapiclient; protected arraylist<geofence> mgeofencelist; private boolean mgeofencesadded; private sharedpreferences msharedpreferences; private static geofenceobserversationservice minstant; public static geofenceobserversationservice getinstant(){     return minstant; } @override public void oncreate() {     super.oncreate();     minstant = this;     mgeofencelist = new arraylist<geofence>();     msharedpreferences = getsharedpreferences(constants.shared_preferences_name , mode_private);     mgeofencesadded = msharedpreferences.getboolean(constants.geofences_added_key , false);     populategeofencelist();     buildgoogleapiclient();     addgeofences(); } public static  final hashmap<string ,latlng> bay_area_landmarks = new hashmap<string,latlng>(); static {     bay_area_landmarks.put("rajakilpakkam" , new latlng(12.915450, 80.150437));      bay_area_landmarks.put("hanse garden" , new latlng(12.911120, 80.156161));  }  @nullable @override public ibinder onbind(intent intent) {     return null; }  protected  void buildgoogleapiclient() {          mgoogleapiclient = new googleapiclient.builder(this)                 .addconnectioncallbacks(this)                 .addonconnectionfailedlistener(this)                 .addapi(locationservices.api)                 .build();               }  @override public void ondestroy() {     mgoogleapiclient.disconnect();     super.ondestroy(); }  private geofencingrequest getgeofencingrequest() {     geofencingrequest.builder builder = new geofencingrequest.builder();     builder.setinitialtrigger(geofencingrequest.initial_trigger_enter);     builder.addgeofences(mgeofencelist);     return builder.build(); } public void populategeofencelist() {     (map.entry<string, latlng> entry : bay_area_landmarks.entryset()) {         mgeofencelist.add(new geofence.builder()                 // set request id of geofence. string identify                 // geofence.                 .setrequestid(entry.getkey())                 // set circular region of geofence.                 .setcircularregion(                         entry.getvalue().latitude,                         entry.getvalue().longitude,                         100                 )                  // set expiration duration of geofence. geofence gets automatically                 // removed after period of time.                 .setexpirationduration(geofence.never_expire)                  // set transition types of interest. alerts generated these                 // transition.                 .settransitiontypes(geofence.geofence_transition_enter | geofence.geofence_transition_exit)                  .build()         );      } }  public void addgeofences() {     if (!mgoogleapiclient.isconnected()) {         toast.maketext(this, getstring(r.string.not_connected), toast.length_short).show();         return;     }       if(!mgeofencelist.isempty()){         try {             locationservices.geofencingapi.addgeofences(mgoogleapiclient, getgeofencingrequest(), getgeofencependingintent()).setresultcallback(this);         } catch (securityexception securityexception) {             securityexception.printstacktrace();         }     }  } public void removegeofences() {     if (!mgoogleapiclient.isconnected()) {         toast.maketext(this, getstring(r.string.not_connected), toast.length_short).show();         return;     }     try {         locationservices.geofencingapi.removegeofences(mgoogleapiclient,getgeofencependingintent()).setresultcallback(this);     } catch (securityexception securityexception) {         securityexception.printstacktrace();     } } private pendingintent getgeofencependingintent() {     intent intent = new intent(this, geofencetransitionsintentservice.class);     return pendingintent.getservice(this, 0, intent, pendingintent.flag_update_current); }   @override public int onstartcommand(intent intent, int flags, int startid) {     return start_sticky; }  @override public void onconnected(@nullable bundle bundle) {  }  @override public void onconnectionsuspended(int i) {  }  @override public void onconnectionfailed(@nonnull connectionresult connectionresult) {  }  @override public void onresult(@nonnull status status) {     if (status.issuccess()) {         mgeofencesadded = !mgeofencesadded;         sharedpreferences.editor editor = msharedpreferences.edit();         editor.putboolean(constants.geofences_added_key, mgeofencesadded);         editor.apply();     } else {         string errormessage = geofenceerrormessages.geterrorstring(this,status.getstatuscode());         log.i("geofence", errormessage);     }}} 

thanks in advance.

my logcat

07-28 19:14:09.133 20254-20254/com.cctspl.geofenceex e/androidruntime: fatal exception: main                                                                    process: com.cctspl.geofenceex, pid: 20254                                                                    java.lang.runtimeexception: unable create service com.cctspl.geofenceex.geofenceobserversationservice: java.lang.illegalstateexception: googleapiclient not connected yet.                                                                        @ android.app.activitythread.handlecreateservice(activitythread.java:2596)                                                                        @ android.app.activitythread.access$1800(activitythread.java:144)                                                                        @ android.app.activitythread$h.handlemessage(activitythread.java:1292)                                                                        @ android.os.handler.dispatchmessage(handler.java:102)                                                                        @ android.os.looper.loop(looper.java:136)                                                                        @ android.app.activitythread.main(activitythread.java:5154)                                                                        @ java.lang.reflect.method.invokenative(native method)                                                                        @ java.lang.reflect.method.invoke(method.java:515)                                                                        @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:780)                                                                        @ com.android.internal.os.zygoteinit.main(zygoteinit.java:596)                                                                        @ dalvik.system.nativestart.main(native method)                                                                     caused by: java.lang.illegalstateexception: googleapiclient not connected yet.                                                                        @ com.google.android.gms.internal.zzod.zzd(unknown source)                                                                        @ com.google.android.gms.internal.zzoh.zzd(unknown source)                                                                        @ com.google.android.gms.internal.zzof.zzd(unknown source)                                                                        @ com.google.android.gms.location.internal.zzf.addgeofences(unknown source)                                                                        @ com.cctspl.geofenceex.geofenceobserversationservice.addgeofences(geofenceobserversationservice.java:130)                                                                        @ com.cctspl.geofenceex.geofenceobserversationservice.oncreate(geofenceobserversationservice.java:50)                                                                        @ android.app.activitythread.handlecreateservice(activitythread.java:2586)                                                                        @ android.app.activitythread.access$1800(activitythread.java:144)                                                                         @ android.app.activitythread$h.handlemessage(activitythread.java:1292)                                                                         @ android.os.handler.dispatchmessage(handler.java:102)                                                                         @ android.os.looper.loop(looper.java:136)                                                                         @ android.app.activitythread.main(activitythread.java:5154)                                                                         @ java.lang.reflect.method.invokenative(native method)                                                                         @ java.lang.reflect.method.invoke(method.java:515)                                                                         @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:780)                                                                         @ com.android.internal.os.zygoteinit.main(zygoteinit.java:596)                                                                         @ dalvik.system.nativestart.main(native method)  

when call startservice(), service doesn't start. telling android want service started. android start service when wants to.

you need call startservice(). noticed in oncreate() of service establish geo fences. don't need again.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -