2011-08-18 3 views
1

끊임없이 (매분마다) GPS 위치를 얻은 후 안드로이드 용 프로그램을 만들어 집에서 내 서버로 보냅니다. 그래서 항상 내 전화가 어디에 있는지 알 수 있습니다.안드로이드에있는 GPS 모니터링 서비스

start.setOnClickListener(new View.OnClickListener() { 
     synchronized public void onClick(View v) { 
      startService(new Intent(GpsTest2.this, GTService.class)); 
     } 
}); 

그런 다음 내 서비스는 다음과 같이 선언한다 :

:이 GTService는 데이터를 검색하는 방법을 가지고

public class GTService extends Service implements LocationListener { 
} 

나는 서비스를 시작하는 시작 버튼이있는 GUI를 생성 의 AndroidManifest.xml에서

public void onLocationChanged(Location location) { 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 
    LocationManager locMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, interval * 1000, minDist, this); 
} 

내가 가진 :

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <service android:name=".GTService"> 
    </service> 

작동하지 않는 것 같습니다 : 데이터가 기록되지 않습니다. 여기서 내가 뭘 잘못하고 있니?

+0

onLocationChanged 메서드에 중단 점을 설정하고 디버깅하여 호출 된 시간을 확인 했습니까? 또한 GPS가 활성화되어 있는지 확인하십시오. – Jack

+0

결코 호출되지 않습니다 :-) 나는 항상 사용할 수있는 네트워크 공급자를 사용하고 있습니다 –

+0

실제 장치 또는 AVD를 사용하고 있습니까? – Jack

답변

1

코드는 당신이이 반드시 당신을 위해 할 것입니다 LocationManager.GPS_PROVIDER

locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2 * 1000, 10, locationListener); 

희망해야한다로 어디 LocationManager.NETWORK_PROVIDER

locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, interval * 1000, minDist, this); 

를 사용하고,이 제외

를 잘 작동하는 것 같군. 감사.

+1

이상하게 작동합니다. gps가 꺼져있을 때도 낮은 해상도로 작동하기 때문에 네트워크 제공 업체를 사용했습니다. –

+0

NETWORK_PROVIDER가 잘못되었다고 생각했는데, 이것은 자신의 매니페스트 (ACCESS_COARSE_LOCATION)에 대한 권한이없는 WiFi를 사용합니다. GPS_PROVIDER은 GPS 공급자이며 ACCESS_FINE_LOCATION이 (가) 필요합니다. – Jack

+0

그래서 문제가 해결 되었습니까? –

0

네트워크 제공 업체의 위치가 정확하지 않습니다. 대신 GPS를 사용하십시오. 당신이 정말로 사용자 네트워크에 원하는 경우에만 그때, 다음은 잘 작동합니다 0

0

로 minDistance의 PARAM을 설정 recomond 것

매니페스트 :

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="17" /> 

<uses-permission android:name="android.permission.LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.smartconcept.locationmonitor.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <service android:name=".GTService" /> 
</application> 

GTService :

import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.widget.Toast; 

public class GTService extends Service implements LocationListener { 
LocationManager locMgr; 
@Override 
public IBinder onBind(Intent arg0) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public void onCreate(){ 
    super.onCreate(); 
    locMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1 * 1000, 0, this); 
} 

@Override 
public void onDestroy(){ 
    super.onDestroy(); 
    locMgr.removeUpdates(this); 
} 

@Override 
public void onLocationChanged(Location loc) { 
    Toast.makeText(getBaseContext(), String.valueOf(loc.getLatitude()) + "\n" + String.valueOf(loc.getLongitude()), Toast.LENGTH_SHORT).show(); 

} 

@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 

} 

} 

MainActivity :

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Button btnStart = (Button)findViewById(R.id.btnStart); 
    btnStart.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      startService(new Intent(MainActivity.this,GTService.class)); 
     } 

    }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

} 

희망이 도움이됩니다.

관련 문제