我有活动,会向用户发送当前的Latitude&经度到实时数据库和另一个用户将获取此数据并在地图中查看,以便其他用户在移动时可以看到第一个用户,
无论如何,在第一个用户应用程序中,用户将被发送到Google地图进行导航,现在当用户移动到Google地图时,我无法获得Latitude&经度,因为我的应用程序没有运行,我猜它是在后台,
我搜索并发现我需要使用服务,因为他们甚至在后台工作,所以我想知道如何将它用于我的代码
在第一个用户活动中,用户将发送Latitude&经度,我希望此代码作为服务运行
基于给出ans,它的semse工作正常我可以看到位置图标即使我的应用程序关闭,无论如何我仍然有问题当我尝试将这些值添加到firebase数据库时,下面是我的代码
O(1)
当我以这种方式启动服务时
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);
}
}
//打开谷歌地图方向
Intent serviceIntent = new Intent(Pop_SupDriverOrder.this, MyServiceGPS.class);
startService(serviceIntent);
应用程序将停止它说myappname已停止关闭应用程序
我得到的错误
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);