ボタンをクリックして座標を取得します手順hereはどのようにユーザーのGPSは、私はこのようなアプリケーションを作りたい
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
double glat = location.getLatitude();
double glong = location.getLongitude();
Toast.makeText(getApplicationContext(), "Your position\n"+glat+"\n"+glong, Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
ボタンのクリックイベントにどのように実装するのですか?ユーザーがボタンをクリックした場合
は、トーストは、位置:)
は私がstartActivityForResult()を使用してそれを実装
UPDATEを示す表示されますが、結果が空またはnullである ここに私のコードです:
これは私がクリックするボタンのコードです
btn_obj_useKor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), MyLocationListener.class);
startActivityForResult(intent, 0);
}
});
public class MyLocationListener extends Activity{
Intent intent;
String output = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
double glat = location.getLatitude();
double glong = location.getLongitude();
output = glat+","+glong;
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
intent.putExtra("returnedData", output);
setResult(RESULT_OK, intent);
finish();
}
}
私、それはWTFを示すボタンをクリックしてください:が、これが私のMyLocationListenerクラスです!結果はnullまたは空であることを意味します。
何が問題なのですか?何をすべきですか?