Search code examples
javaandroidgeolocation

Save network location as a .txt file (Without using GPS)


I am a beginner in android programming. My Graduate project is about tracking a mobile device and i need the code to save the location( Without using GPS) as a text file. Someone suggest me the codes for doing that. It will be a great help for me.


Solution

  • Try this.

    locationManagerNetwork = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Location location2 = locationManagerNetwork
                        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    
         if (location2 != null) {       
                    String message = String
                            .format("Yout location : \n Longitude: %1$s \n Latitude: %2$s",
                                    location2.getLongitude(), location2.getLatitude());
                    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG)
                            .show();
    
    
        //use here file writer if you want to write the coordinates in a text file
                }
    

    for writing sd card

    File sdcard = Environment.getExternalStorageDirectory();
            File f = new File(sdcard, "/yourfile");
    
    if(!f.exsist()){
    f.createNewFile();
    //Use outwriter here, outputstream search how to write into a file in java code 
    }