2013-07-17 4 views
-1

백그라운드를 사용하여 위치를 추적하는 응용 프로그램을 만들어 서버로 보내야합니다. 부팅시 서비스 시작 (android.intent.action.BOOT_COMPLETED)이 있기 때문에 내 프로그램에서 데이터를 전송합니다.GPS를 한 번 켜면 서버에 데이터를 전송하지 않습니다. GPS를 켠 후에도 전송하지 않습니다.

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); 
    } 
} 
} 

그리고 여기가

<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 내 AndroidManifest.xml에있다 0
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); 
    } 
} 
} 

답변

1

여러 위치 리스너를 만드는 중입니다. 필요한 것은 아니지만 전반적으로 GPS가 켜지거나 꺼진 경우 작동하지 않는 이유를 전반적으로 알 수 없습니다.

내 PasteBin 페이지를 여기에서 보면 http://pastebin.com/u/ScottHelme입니다. 필요로하는 모든 코드가 포함 된 접두사 "GPS Tracker"가있는 네 개의 파일이 있습니다.

관련 문제