バックグラウンドを使用してユーザーの位置を追跡し、ソケット経由でサーバーに送信する必要があります。私が行ったアクティビティを使用して正常に動作していますが、サービスを使用するとできませんでした。誰でも私を助けてもらえますか?
public class GpsService extends Service implement LocationListener{
static final String TAG ="MobileTracking";
public Context mycontext;
public boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// Declaring a Location Manager
protected LocationManager locationManager;
LocationListener locationListener;
boolean canGetLocation = false;
public Location location; // location
public double latitude; // latitude
public double longitude; // longitude
public String datetext;//date and time
TelephonyManager telephoneManager;
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; // 1 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
public Location getLocation()
{
try
{
locationManager = (LocationManager) mycontext.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled)
{
// no network provider is enabled
}
else
{
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled)
{
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null)
{
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled)
{
if (location == null)
{
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null)
{
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
return location;
}
//Getting user’s current location (Latitude and Longitude)
//Get the Longitude
public double gettheLongitude()
{
if (location != null)
{
longitude = location.getLongitude();
}
return longitude;
}
// Get the Latitude
public double gettheLatitude()
{
if(location != null)
{
latitude = location.getLatitude();
}
return latitude;
}
public boolean canGetLocation()
{
return this.canGetLocation;
}
// method to get date time
public String currentDate()
{
datetext = (String)DateFormat.format("yyyy-MM-dd hh:mm:ss", (new Date()).getTime());
return datetext;
}
// method to get the IMEI Number by using the Context that you passed to your class
public String getIMEINumber(){
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// get IMEI
String imei = tm.getDeviceId();
return imei;
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onLocationChanged(Location arg0) {
super.onCreate();
Log.d(TAG, "OnCreated");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "OnStarted");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}