-1

我需要创建一个应用程序来使用 background 跟踪位置,然后将数据发送到服务器。我的程序在我重新启动设备时发送数据,因为我在启动完成时启动了服务(android.intent.action.BOOT_COMPLETED)。

当我关闭并打开 GPS 时,我没有将数据发送到服务器。当我重新启动设备时,它只能工作。如果 GPS 关闭,它应该通过移动数据将数据发送到服务器。任何人都可以帮助我吗?

每当我关闭并打开它时,它应该可以在不重新启动设备的情况下工作。我怎样才能?

GPSTracker.java

 public class GPSTracker extends Service {
 private static final String TAG = "BOOMBOOMTESTGPS";

private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 1000 * 60 * 10;
private static final float LOCATION_DISTANCE = 10f;
private class LocationListener implements android.location.LocationListener{
    Location mLastLocation;
    public LocationListener(String provider)
    {
        Log.e(TAG, "LocationListener " + provider);
        mLastLocation = new Location(provider);
    }

    public String currentDate()
    {
        String 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 void onLocationChanged(final Location location) {
        Log.e(TAG, "onLocationChangedc: " + location);

        Thread thread =  new Thread(new Runnable() {
            private Socket socket;

            public void run() {
                try {
                    socket = new Socket("220.247.223.119", 8888);
                    try {
                        OutputStream target = socket.getOutputStream();
                        String sendValue= getIMEINumber()+", "+String.valueOf(location.getLatitude())+", "+String.valueOf(location.getLongitude()+", "+currentDate());
                        target.write(sendValue.getBytes());
                        target.flush();

                        Log.e("SocketConnectionHandler", "S: Send");

                        socket.close();
                        Log.e("SocketConnectionHandler", "S: Closed.");
                    } catch (Exception e) {
                        Log.e("SocketConnectionHandler", "S: Error", e);
                    }

                } catch(Exception e){
                    e.printStackTrace();
                }
            }
        });
        thread.start();

        mLastLocation.set(location);

    }
    @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");
    initializeLocationManager();

    return START_STICKY;
}
@Override
public void onCreate()
{
    Log.e(TAG, "onCreate");
    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);
    }
}
}

这是我的 AndroidManifest.xml

<manifest package="com.geo.locationtracker"
android:versionCode="1"
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
   android:targetSdkVersion="17" />
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
     <service
android:name=".LocationService"
android:icon="@drawable/ic_launcher"
android:label="@string/service_name">
</service> 
    <receiver android:name=".GpsReceiver">  
<intent-filter>  
    <action android:name="android.intent.action.BOOT_COMPLETED" />  
    <action android:name="android.intent.action.PHONE_STATE" />
    <category android:name="android.intent.category.DEFAULT"/>
     <action android:name="android.intent.action.PACKAGE_INSTALL" />
     <action android:name="android.intent.action.PACKAGE_ADDED" />
 </intent-filter>  

这是 GPS Receiver.java

 public class GpsReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
    {

    Intent intent1 = new Intent(context,GpsService.class);

    //context.startService(new Intent(context,GitService.class));
        intent1.setFlags(0x10000000);
        context.startService(intent1);
    }
}
}
4

1 回答 1

1

似乎您正在创建一组多个位置侦听器,我不确定是否需要这些侦听器,但总的来说,我不明白为什么一旦 GPS 关闭/打开它就无法工作。

如果您在此处查看我的 PasteBin 页面http://pastebin.com/u/ScottHelme,我有 4 个前缀为“GPS Tracker”的文件,其中包含执行您需要的所有代码。

于 2013-07-18T20:13:07.853 回答