2014-01-29 1 views
0

나는 안드로이드 앱을 개발 중이다. 서비스로 위치 업데이트를 자주 받는다.
내 코드는 내가 서비스를 시작하는 스위치를 사용하고서비스에서 위치 업데이트를 자주 얻는 것

import android.app.Activity; 
import android.content.Intent; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.provider.Settings; 
import android.view.Menu; 
import android.view.View; 
import android.widget.CompoundButton; 
import android.widget.Switch; 

public class MainActivity extends Activity implements CompoundButton.OnCheckedChangeListener{ 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    View view = this.getWindow().getDecorView(); 
    LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE); 
    boolean enabled = service 
     .isProviderEnabled(LocationManager.GPS_PROVIDER); 

    // check if enabled and if not send user to the GSP settings 
    // Better solution would be to display a dialog and suggesting to 
    // go to the settings 
    if (!enabled) { 
     Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
     startActivity(intent); 
    } 
    Switch s = (Switch) findViewById(R.id.switch1); 
    if(s != null) { 
     s.setOnCheckedChangeListener(this); 
    } 


} 

public void onResume(View v) { 

} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 
@Override 
public void onCheckedChanged(CompoundButton buttonview, boolean isChecked) { 
    // TODO Auto-generated method stub 
    if(isChecked) { 
     Intent intent = new Intent(this, GetGeoLocation.class); 
     startService(intent); 
    } 

    if(!isChecked) { 
     stopService(new Intent(MainActivity.this,GetGeoLocation.class)); 
    } 
} 

} 

GetGeoLocation.java

import android.annotation.SuppressLint; 
import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.util.Log; 
import android.widget.Toast; 

public class GetGeoLocation extends Service implements LocationListener{ 


protected LocationManager locationManager; 
protected LocationListener locationListener; 
@Override 
public IBinder onBind(Intent arg0) { 
    // TODO Auto-generated method stub 
    return null; 
} 

public void onCreate() { 
    Toast.makeText(this, "The new Service was Created", Toast.LENGTH_SHORT).show(); 
} 
public int onStart() { 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 

    Criteria cri = new Criteria(); 
    String bestProvider = locationManager.getBestProvider(cri, true); 

    Location loc = locationManager.getLastKnownLocation(bestProvider); 
    double latMy = loc.getLatitude(); 
    double lngMy = loc.getLongitude(); 
    Toast.makeText(this, " " +latMy+" "+lngMy, Toast.LENGTH_SHORT).show(); 
    return START_STICKY; 
} 

public void onDestroy() { 
    Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show(); 

} 

@SuppressLint("ShowToast") 
@Override 
public void onLocationChanged(Location loc) { 
    // TODO Auto-generated method stub 
    Toast.makeText(this,"Location Changed", Toast.LENGTH_SHORT); 
    Toast.makeText(this, " " +loc.getLatitude()+" "+loc.getLongitude(), Toast.LENGTH_SHORT).show(); 
    System.out.println("Location"+loc.getLatitude()+" " +loc.getLongitude()); 
} 

@SuppressLint("ShowToast") 
@Override 
public void onProviderDisabled(String arg0) { 
    // TODO Auto-generated method stub 
    Toast.makeText(this,"ProviderDisabled", Toast.LENGTH_SHORT); 
} 

@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.java에게 있습니다. 스위치를 확인하여 서비스를 시작하면 서비스가 현재 위치를 얻고 위치를 자주 호출합니다. 나는 내 코드에서 무엇이 잘못되었는지 알지 못한다. 위치를 얻는 지 알지 못한다.
누군가 나를 도와주세요. 텍스트를 한 후에

내 로그 캣이 토스트를 표시하려면 경고 나 오류

01-29 20:29:34.996: I/SurfaceTextureClient(5512): [STC::queueBuffer] (this:0x5b79b008) fps:0.14, dur:7126.33, max:7126.33, min:7126.33 
01-29 20:29:34.997: I/SurfaceTextureClient(5512): [STC::queueBuffer] this:0x5b79b008, api:1, last queue time elapsed:7126.33 
01-29 20:29:35.268: D/skia(5512): Flag is not 10 
+0

답변으로 인해 문제가 해결되면 수락 해주세요. 그렇지 않은 경우 자세한 정보 또는 솔루션을 제공해주십시오. 또는 더 이상 필요하지 않은 경우이 질문을 삭제하십시오. – wtsang02

답변

0

을 보여 나던, 당신은 쇼()를 호출해야합니다. 당신이 다른 오류가있는 경우

@SuppressLint("ShowToast") 

가, 로그 캣을 게시하시기 바랍니다 :

Toast.makeText(...).show(); 

또한 모든 행을 제거하십시오.

+0

내 logcat을 업데이트했습니다. 그것은 어떤 오류나 경고를 보여주지 못한다. – ImMathan

0

코드에 어떤 문제가 있는지 잘 모르겠습니다. 이전 위치 제공 업체를 사용하고있는 것으로 확인되었습니다. 먼저 새로운 Google Play 위치 서비스를 사용하는 것이 좋습니다. 사용하기가 훨씬 쉽고 정확합니다.

새로운 Google Play 위치 서비스를 사용하며 백그라운드에서 작동하는 샘플이 있습니다. MIT는 라이센스되어 있고 사용 준비가되어 있습니다.

https://github.com/nickfox/GpsTracker/tree/master/phoneClients/android

관련 문제