Trying to get latitude and longitude. I am trying the service given below. It crashes on the device while trying to start the service.
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.location.Location;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PowerManager;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.model.LatLng;
import com.google.firebase.iid.FirebaseInstanceId;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Turzo on 01-Aug-17.
*/
public class LocationService extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {
private String TAG = "LocationService";
private LocationRequest mLocationRequest;
private GoogleApiClient mGoogleApiClient;
private static final String LOGSERVICE = "#######";
private String tempAddress = "";
private int erroHandling = 0;
//Context context;
private LatLng localLatLng;
PowerManager pm;
PowerManager.WakeLock wl;
private int userStatusSendFlag = 0;
private String locationAuth;
private String locationAppID;
private String locationUser;
//private String lUserAuth;
DataBaseHelper myDbHelper = new DataBaseHelper(null);
Cursor cursor;
private String data_user_id;
@Override
public void onCreate() {
super.onCreate();
Log.d(LOGSERVICE, "onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//Log.i(LOGSERVICE, "onStartCommand");
Toast.makeText(this, "Updating location started!", Toast.LENGTH_SHORT).show();
Log.d(TAG,"----------Location service started!-----------------");
// getUser();
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
locationAppID = refreshedToken;
locationUser = data_user_id;
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
return START_NOT_STICKY;
}
@Override
public void onConnected(Bundle bundle) {
Log.d(LOGSERVICE, "onConnected" + bundle);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Location l = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (l != null) {
//-- start 1
}
}
@Override
public void onConnectionSuspended(int i) {
Log.d(TAG,"----------Location service Connection Suspended!-----------------");
}
@Override
public void onLocationChanged(Location location) {
double lat = location.getLatitude();
double lng = location.getLongitude();
//-- start 2
Toast.makeText(this,String.valueOf(lat)+"/n"+String.valueOf(lng),Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
super.onDestroy();
//Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
// Toast.makeText(this, "Updating location stopped!", Toast.LENGTH_SHORT).show();
Log.d("111111Updating...","---------------------- Service Stopped! ---------------------- ");
stopSelf();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
// Log.i(LOGSERVICE, "onConnectionFailed ");
Toast.makeText(this, "Connection Failed!", Toast.LENGTH_SHORT).show();
}
private void initLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
//--------------------
}
The error is :
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.google.android.gms.common.api.GoogleApiClient.isConnected()' on a null object reference