我正在尝试将位置管理器形成的latitude
和longitude
写入文件,但写操作由于某种原因不起作用。以下是我一直在尝试的代码。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
File absPath = getFilesDir();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
String filename = "myfile";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write((location.getLatitude()+","+location.getLongitude()).getBytes());
outputStream.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
locationManager.requestLocationUpdates(NETWORK_PROVIDER, 0, 0, locationListener);
}
}
感谢任何帮助。谢谢。