0

Androidフォンから経度と緯度の値をキャプチャしようとしています。外部デバイスに値を表示できますが、エミュレータに表示できず、場所が見つかりません。値をに保存するための適切なアプローチを教えてください。テキストファイルもお願いします。これが私のコードです。アセットフォルダにテキストファイルを作成しました。

public class MainActivity extends Activity implements LocationListener{

private final static String STORETEXT="storetext.txt";


LocationManager locationManager ;
String provider;
String value1;
String value2;



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

    // Getting LocationManager object
    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);        

    // Creating an empty criteria object
    Criteria criteria = new Criteria();

    // Getting the name of the provider that meets the criteria
    provider = locationManager.getBestProvider(criteria, false);


    if(provider!=null && !provider.equals("")){

        // Get the location from the given provider 
        Location location = locationManager.getLastKnownLocation(provider);


        locationManager.requestLocationUpdates(provider, 20000, 1, this);


        if(location!=null)
            onLocationChanged(location);
        else
            Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show();

    }else{
        Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public void onLocationChanged(Location location) {
    // Getting reference to TextView tv_longitude
    TextView tvLongitude = (TextView)findViewById(R.id.tv_longitude);

    // Getting reference to TextView tv_latitude
    TextView tvLatitude = (TextView)findViewById(R.id.tv_latitude);

    // Setting Current Longitude
    tvLongitude.setText("Longitude:" + location.getLongitude());

    // Setting Current Latitude
    tvLatitude.setText("Latitude:" + location.getLatitude() );

    value1 = tvLongitude.getText().toString();
    value2 = tvLatitude.getText().toString();

    saveClicked();




}

private void saveClicked() {



    try{
        OutputStreamWriter out=
                new OutputStreamWriter(openFileOutput(STORETEXT, 0));
        out.write(value1);
        out.write(value2);
        out.close();

        Toast
    .makeText(this, "The contents are saved in the file.", Toast.LENGTH_LONG)
    .show();

    }
    catch(Throwable t){
        Toast.makeText(this, "Exception: "+ t.toString(), Toast.LENGTH_LONG)
        .show();

    }
}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub      
}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub      
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub      
}
}
4

1 に答える 1

2

エミュレーターでは、ジオロケーションは機能しません。テストするために、ジオフィックスを注入することができます

activity.getApplicationContext().getCacheDir()このデータを保存するためにアセットを使用しないでくださいgetFilesDir()

次に、データがローカルに保存されたら、RESTサービスを使用してこのデータをサーバーにアップロードし、データベースに記録することに集中する必要があります。

于 2012-10-13T09:26:45.620 に答える