I have Activity that will send user current Latitude & Longitude to real time database and another user will get this data and view it in the map so the other user can see the first user when he moving,
anyway in the fist user app the user will be send to Google map for the navigation , now when the user move to Google map I can't get the Latitude & Longitude because my app is not running and I guess it's in background ,
I searched and found out the I need to use service as they work even in background so I would like to know how to use it for my code below
In first user activity where the user will send Latitude & Longitude and I want this code to run as service
based in the giving ans , its semse to work fine where i can see the location icon is on even when my app is close , anyway i still have problem when i try to add these value to firebase database , below is my code
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.maps.model.LatLng;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.HashMap;
import my.penta.xxx.xxx.activity.User;
import my.penta.xxx.xxx.helper.SQLiteHandler;
public class MyServiceGPS extends Service {
private static final String TAG = "GPSTEST";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 1000;
private static final float LOCATION_DISTANCE = 10f;
FirebaseDatabase database;
DatabaseReference myRef;
private SQLiteHandler db;
String SupDriver = "SupDriver",Driverr="provider",tags,Organization="Organization",Individual="Individual",maindriverid;
String uid,email;
Double latitudeLocation;
Double longitudeLocation;
private class LocationListener implements android.location.LocationListener
{
Location mLastLocation;
public LocationListener(String provider)
{
Log.e(TAG, "LocationListener " + provider);
mLastLocation = new Location(provider);
}
@Override
public void onLocationChanged(Location location)
{
Log.e(TAG, "onLocationChanged: " + location);
mLastLocation.set(location);
latitudeLocation = location.getLatitude();
longitudeLocation = location.getLongitude();
LatLng p1 = new LatLng(latitudeLocation, longitudeLocation);
if (tags.equals(SupDriver))
{
String MainProvideridtosend = maindriverid;
String SupProvideridtosend = uid;
String Status = "3";
String TypeOfxxx = "0";
writeNewUser(MainProvideridtosend, Double.toString(latitudeLocation), Double.toString(longitudeLocation), Status, TypeOfxxx,SupProvideridtosend);
}else {
}
}
@Override
public void onProviderDisabled(String provider)
{
Log.e(TAG, "onProviderDisabled: " + provider);
}
@Override
public void onProviderEnabled(String provider)
{
Log.e(TAG, "onProviderEnabled: " + provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.e(TAG, "onStatusChanged: " + provider);
}
}
LocationListener[] mLocationListeners = new LocationListener[] {
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER)
};
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public void onCreate()
{
Log.e(TAG, "onCreate");
//chek if sup Driver or Driver
db = new SQLiteHandler(getApplicationContext());
HashMap<String, String> usertag = db.getUserTag();
tags = usertag.get("tag");
db = new SQLiteHandler(getApplicationContext());
HashMap<String, String> user = db.getProviderDetails();
String name = user.get("name");
email = user.get("email");
String number = user.get("number");
maindriverid = user.get("service"); // << supdriver is mainuserid, Driver is AcountType
uid = user.get("uid");
initializeLocationManager();
try {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[1]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "network provider does not exist, " + ex.getMessage());
}
try {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[0]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}
}
@Override
public void onDestroy()
{
Log.e(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
try {
mLocationManager.removeUpdates(mLocationListeners[i]);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listners, ignore", ex);
}
}
}
}
private void initializeLocationManager() {
Log.e(TAG, "initializeLocationManager");
if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}
private void writeNewUser(String uidl, String lat, String longit,String statuss,String typeOfxxx,String SupProviderid) {
User user = new User(uidl, lat,longit,statuss,typeOfxxx,SupProviderid);
database = FirebaseDatabase.getInstance();
myRef = database.getReference("xxx");
String emailencod = email.replace(".", ",");
myRef.child(emailencod).setValue(user);
}
}
when i start the service this way
Intent serviceIntent = new Intent(Pop_SupDriverOrder.this, MyServiceGPS.class);
startService(serviceIntent);
//open google map for direction
double lat = Double.parseDouble(latitudeLocationv);
double lng = Double.parseDouble(longitudeLocationv);
String format = "geo:0,0?q=" + lat + "," + lng + "( Location title)";
Uri uri = Uri.parse(format);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
the app will stopped it say myappname has stopped close app
the error i got
03-20 15:08:26.526 22132-22132/my.penta.dumpster.dumpster I/InputMethodManager: [IMM] startInputInner - mService.startInputOrWindowGainedFocus
03-20 15:08:26.535 22132-22132/my.penta.dumpster.dumpster D/InputTransport: Input channel constructed: fd=87
03-20 15:08:26.535 22132-22132/my.penta.dumpster.dumpster D/InputTransport: Input channel destroyed: fd=85
03-20 15:08:26.930 22132-25047/my.penta.dumpster.dumpster W/DynamiteModule: Local module descriptor class for com.google.android.gms.googlecertificates not found.
03-20 15:08:26.936 22132-25047/my.penta.dumpster.dumpster I/DynamiteModule: Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:4
03-20 15:08:26.945 22132-25047/my.penta.dumpster.dumpster I/DynamiteModule: Selected remote version of com.google.android.gms.googlecertificates, version >= 4
03-20 15:08:26.952 22132-25047/my.penta.dumpster.dumpster W/System: ClassLoader referenced unknown path: /data/user_de/0/com.google.android.gms/app_chimera/m/0000004e/n/arm64-v8a
03-20 15:08:26.997 22132-22137/my.penta.dumpster.dumpster I/art: Do partial code cache collection, code=56KB, data=61KB
03-20 15:08:26.997 22132-22137/my.penta.dumpster.dumpster I/art: After code cache collection, code=49KB, data=57KB
03-20 15:08:26.997 22132-22137/my.penta.dumpster.dumpster I/art: Increasing code cache capacity to 256KB
03-20 15:08:27.341 22132-22132/my.penta.dumpster.dumpster D/ViewRootImpl@1840a3f[Pop_SupDriverOrder]: ViewPostImeInputStage processPointer 0
03-20 15:08:27.448 22132-22132/my.penta.dumpster.dumpster D/ViewRootImpl@1840a3f[Pop_SupDriverOrder]: ViewPostImeInputStage processPointer 1
03-20 15:08:27.495 22132-24979/my.penta.dumpster.dumpster V/FA: Recording user engagement, ms: 1056
03-20 15:08:27.497 22132-24979/my.penta.dumpster.dumpster V/FA: Activity paused, time: 303917426
03-20 15:08:27.502 22132-24979/my.penta.dumpster.dumpster D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=1056, firebase_screen_class(_sc)=Pop_SupDriverOrder, firebase_screen_id(_si)=-526983696703345070}]
03-20 15:08:27.548 22132-22132/my.penta.dumpster.dumpster D/ViewRootImpl@1840a3f[Pop_SupDriverOrder]: MSG_WINDOW_FOCUS_CHANGED 0
03-20 15:08:27.630 22132-22132/my.penta.dumpster.dumpster D/ViewRootImpl@9a2cef7[Provider_Navigation]: mHardwareRenderer.destroy()#1
03-20 15:08:27.734 22132-22132/my.penta.dumpster.dumpster D/ViewRootImpl@9a2cef7[Provider_Navigation]: Relayout returned: oldFrame=[0,0][1080,2220] newFrame=[0,0][1080,2220] result=0x5 surface={isValid=false 0} surfaceGenerationChanged=true
03-20 15:08:27.750 22132-22132/my.penta.dumpster.dumpster D/ViewRootImpl@9a2cef7[Provider_Navigation]: mHardwareRenderer.destroy()#1
03-20 15:08:27.786 22132-22132/my.penta.dumpster.dumpster D/ViewRootImpl@9a2cef7[Provider_Navigation]: Relayout returned: oldFrame=[0,0][1080,2220] newFrame=[0,0][1080,2220] result=0x1 surface={isValid=false 0} surfaceGenerationChanged=false
03-20 15:08:27.825 22132-22132/my.penta.dumpster.dumpster D/ViewRootImpl@1840a3f[Pop_SupDriverOrder]: mHardwareRenderer.destroy()#1
03-20 15:08:27.857 22132-22132/my.penta.dumpster.dumpster D/ViewRootImpl@1840a3f[Pop_SupDriverOrder]: Relayout returned: oldFrame=[27,268][1052,1879] newFrame=[27,268][1052,1879] result=0x5 surface={isValid=false 0} surfaceGenerationChanged=true
03-20 15:08:28.522 22132-22132/my.penta.dumpster.dumpster D/InputTransport: Input channel destroyed: fd=87
03-20 15:08:32.518 22132-24979/my.penta.dumpster.dumpster V/FA: Inactivity, disconnecting from the service
03-20 15:08:39.161 22132-24988/my.penta.dumpster.dumpster I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
03-20 15:08:39.161 22132-24988/my.penta.dumpster.dumpster I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
03-20 15:08:42.265 22132-22204/my.penta.dumpster.dumpster W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.