Looking for some help android programming

Joined
Aug 1, 2013
Messages
387
Reaction score
0
I am trying to create a method whereby whenever a user turn on "enable mock location", the application should alert the user saying that mock location is turn on. This method is to be integrated into a gps application. Of current, the gps application is able to detect if a gps is turn on it is not on, there will be a alert message to notify the user that GPS is turned off. I'm trying to do the same to mock locations as well however my application keep crashing. Here's the code so far.. which part did it go wrong?

Code:
@Override
    protected void onStart() {
        super.onStart();

        // take care of GPS 
        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        gpsEnabled = mLocationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        if (!gpsEnabled || isMockSettingsON(null)) {
            // GPS is missing or turned off - inform the user
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("GPS location is turned off!")
                    .setCancelable(false)
                    .setPositiveButton("Ok",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    finish();
                                }
                            });
            AlertDialog alert = builder.create();
            alert.show();
        } else {
            setup();
        }
        ;
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(gpsEnabled){
            mLocationManager.removeUpdates(listener);
        };
    }

    private void setup() {
        Location gpsLocation = null;
        mLocationManager.removeUpdates(listener);

        // register GPS event receiver
        if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TEN_SECONDS,
                    TEN_METERS, listener);
            gpsLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        };

        // make a first update
        if (gpsLocation != null)
            updateLocation(gpsLocation);
    }

    private void updateLocation(Location location) {
        double alt, latitude, longitude;
        float bear, speed, acc;

        long getTime = location.getTime();

        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy/MM/dd HH:mm:ss");
        String FgetTime = dateFormat.format(getTime);

        // read GPS data and send them to handlers 
        if (location.hasAccuracy()) {
            acc = location.getAccuracy();
        } else {
            acc = 0;
        }
        ;

        if (location.hasAltitude()) {
            alt = location.getAltitude();
        } else {
            alt = 0;
        }
        ;

        if (location.hasBearing()) {
            bear = location.getBearing();
        } else {
            bear = 0;
        }
        ;

        if (location.hasSpeed()) {
            speed = location.getSpeed();
        } else {
            speed = 0;
        }
        ;
        latitude = location.getLatitude();
        longitude = location.getLongitude();
        current_altitude = alt;

        String toMark = getTime + ";" + latitude + ";" + longitude + ";" + alt
                + ";" + acc + ";" + bear + ";" + speed + ";";

        Message.obtain(mHandler, UPDATE_LAT, String.format("%f", latitude))
                .sendToTarget();

        Message.obtain(mHandler, UPDATE_LNG, String.format("%f", longitude))
                .sendToTarget();

        Message.obtain(mHandler, UPDATE_TIME, FgetTime).sendToTarget();

        Message.obtain(mHandler, UPDATE_ACC, acc + " meters").sendToTarget();

        Message.obtain(mHandler, UPDATE_ALT,
                String.format("%.2f", alt) + " meters").sendToTarget();

        Message.obtain(mHandler, UPDATE_MARK, toMark).sendToTarget();

    }

    public static String CurrentDateFormatted() {
        String ret;

        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "HH_mm_ss_yyyy_MM_dd");
        Date date = new Date();
        ret = dateFormat.format(date);

        return ret;
    };

    private boolean WriteToFile(String data) {
        try {
            // Write data to a file to keep tracking
            File extStore = Environment.getExternalStorageDirectory();
            String SD_PATH = extStore.getAbsolutePath();

            File gps_data = new File(SD_PATH + "/" + file_name);

            FileWriter writer = new FileWriter(gps_data, true);
            writer.append(data + "\n");
            writer.flush();
            writer.close();
            Log.d(TAG, "Data saved OK");

        } catch (FileNotFoundException e) {
            Log.d(TAG, "File not found: " + e.getMessage());
            return false;
        } catch (IOException e) {
            Log.d(TAG, "Error accessing file: " + e.getMessage());
            return false;
        }
        return true;
    }

    // the listener to receive GPS events
    private final LocationListener listener = new LocationListener() {

        public void onLocationChanged(Location location) {
            updateLocation(location);
        }

        public void onProviderDisabled(String provider) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };
}
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top