I have write code for get GPS data and print it in Toast message. GPS Data printed as Toast message. but not write in CSV file that time the app get closed.
Here is my code,
public class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location loc)
{
if(root1.canWrite())
{
dir1 = new File (root1.getAbsolutePath() + "/TrackingData");
if(!dir1.exists())
{
Toast.makeText(getBaseContext(), "Directory creation", Toast.LENGTH_LONG).show();
dir1.mkdirs();
file1 = new File(dir1, "Data.csv");
}
}
try {
out = new FileOutputStream (file1,append);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
mlongitude = loc.getLongitude();
mlatitude = loc.getLatitude();
// location = loc;
if (mlatitude != 0 && mlongitude !=0)
{
Toast.makeText( getApplicationContext(),"Lat "+mlatitude+" Long "+mlongitude,Toast.LENGTH_SHORT).show();
//Writing in CSV file on device
//String SDcardpath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/TrackData/Data.csv";
strlat = String.valueOf(mlatitude);
strlong = String.valueOf(mlongitude );
String strspeed = String.valueOf(speed);
locationManager.removeUpdates(this);
locationManager.removeUpdates( mlocListener);
//send location to server
if(file1.exists())
{
sb1.append("Time");
sb1.append(",");
sb1.append("Latitude");
sb1.append(",");
sb1.append("Longitude");
sb1.append(",");
sb1.append("Speed");
if (root1.canWrite()){
try {
out.write(sb1.toString().getBytes());
out.write('\n');
} catch (IOException e) {
e.printStackTrace();
}
sb1.delete(0, sb1.length());
}
sb1.append("\""+sdate+"\"");
sb1.append(",");
sb1.append("\""+strlat+"\"");
sb1.append(",");
sb1.append("\""+strlong+"\"");
sb1.append(",");
sb1.append("\""+strspeed+"\"");
if (root1.canWrite()){
try {
out.write(sb1.toString().getBytes());
out.write('\n');
}
catch (IOException e)
{
Toast.makeText(getBaseContext(), "Can't write in file", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
sb1.delete(0, sb1.length());
}
}
else
{
try {
file1.createNewFile();
sb1.append("Time");
sb1.append(",");
sb1.append("Latitude");
sb1.append(",");
sb1.append("Longitude");
sb1.append(",");
sb1.append("Speed");
if (root1.canWrite()){
try {
out.write(sb1.toString().getBytes());
out.write('\n');
} catch (IOException e) {
e.printStackTrace();
}
sb1.delete(0, sb1.length());
}
sb1.append("\""+sdate+"\"");
sb1.append(",");
sb1.append("\""+strlat+"\"");
sb1.append(",");
sb1.append("\""+strlong+"\"");
sb1.append(",");
sb1.append("\""+strspeed+"\"");
if (root1.canWrite()){
try {
out.write(sb1.toString().getBytes());
out.write('\n');
} catch (IOException e) {
e.printStackTrace();
}
sb1.delete(0, sb1.length());
}
} catch (IOException e) {
Toast.makeText(getBaseContext(), "Can;t create new file", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
}
}
I have declared this in AndroidManifest file
Now working fine