在gps启用检查后销毁并重新启动应用程序

时间:2015-08-13 14:39:32

标签: java android android-activity gps

嗨,我正在运行一个问题。我在我的启动画面上有一个gps检查功能,在检查我的gps连接后,如果它的状态为空它会显示一个警告。我想在按钮点击警告框后销毁并重启我的应用程序。请帮助。这是我的代码

Geolocationfinder类

package com.driverapp.inis.zuber;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

import java.util.Timer;
import java.util.TimerTask;

public class GeoLocationFinder {
    private static final String TAG = "GeoLocationFinder";
    Timer locationTimer;
    LocationManager locationManager;
    private static final int min_update_time = 3000; // in msec
    private static final int min_distance_for_update = 10; // in meters
    LocationResult locationResult;
    boolean gps_enabled = Boolean.FALSE;
    boolean network_enabled = Boolean.FALSE;
    private AlertDialogManager alert;
    Context ctx;


    public boolean getLocation(Context ctx, LocationResult result) {
        this.ctx = ctx;
        locationResult = result;

        if (locationManager == null) {
            locationManager = (LocationManager) ctx
                    .getSystemService(Context.LOCATION_SERVICE);
        }

        try {
            gps_enabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch (Exception e) {
           /* alert = new AlertDialogManager();
            alert.showAlertDialog(ctx, "Error", "GPS enabled exception:", false);*/
            Log.d(TAG, "GPS enabled exception: " + e.getMessage());
        }

        try {
            network_enabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch (Exception e) {
            Log.d(TAG, "Network enabled exception: " + e.getMessage());
        }

        if (!gps_enabled && !network_enabled) {
            alert = new AlertDialogManager();
            alert.showAlertDialog(ctx, "Error", "Please Check Your GPS Connection and Try Again", false);
            return Boolean.FALSE;
        }

        if (gps_enabled) {
            locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, min_update_time,
                    min_distance_for_update, locationListenerGps);
        }

        if (network_enabled) {
            locationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, min_update_time,
                    min_distance_for_update, locationListenerNetwork);
        }

        locationTimer = new Timer();
        locationTimer.schedule(new GetLastLocation(), 2000);
        return Boolean.TRUE;
    }

    LocationListener locationListenerGps = new LocationListener() {
        public void onLocationChanged(Location location) {
            locationTimer.cancel();
            locationResult.gotLocation(location);
            locationManager.removeUpdates(this);
            locationManager.removeUpdates(locationListenerGps);
        }

        @Override
        public void onProviderDisabled(String provider) {
            Log.d(TAG, "GPS provider disabled" + provider);
        }

        @Override
        public void onProviderEnabled(String provider) {
            Log.d(TAG, "GPS provider enabled" + provider);

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.d(TAG, "GPS status changed");

        }
    };

    LocationListener locationListenerNetwork = new LocationListener() {
        public void onLocationChanged(Location location) {
            locationTimer.cancel();
            locationResult.gotLocation(location);
            locationManager.removeUpdates(this);
            locationManager.removeUpdates(locationListenerNetwork);
        }
        @Override
        public void onProviderDisabled(String provider) {
            Log.d(TAG, "Network provider disabled. " + provider);

        }

        @Override
        public void onProviderEnabled(String provider) {
            Log.d(TAG, "Network provider enabled. " + provider);

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.d(TAG, "Network status changed.");

        }
    };

    private class GetLastLocation extends TimerTask {

        @Override
        public void run() {
            locationManager.removeUpdates(locationListenerGps);
            locationManager.removeUpdates(locationListenerNetwork);
            Location net_loc = null, gps_loc = null;
            if (gps_enabled) {
                gps_loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            }
            if (network_enabled) {
                net_loc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            }
            if (gps_loc != null && net_loc != null) {
                if (gps_loc.getTime() > net_loc.getTime()) {
                    locationResult.gotLocation(gps_loc);
                } else {
                    locationResult.gotLocation(net_loc);
                }
                return;
            }

            if (gps_loc != null) {
                locationResult.gotLocation(gps_loc);
                return;
            }

            if (net_loc != null) {
                locationResult.gotLocation(net_loc);
                return;
            }

            else {
                locationResult.gotLocation(null);
            }

        }

    }

    public static abstract class LocationResult {
        public abstract void gotLocation(Location location);
    }
}

Splashscreen类

package com.driverapp.inis.zuber;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import Connectivity_manager.Internet_CheckingActivity;



public class Splashscreen extends Activity {
    private static final String TAG = "SplashScreenActivity";
    private Location newLocation = null;
    private AlertDialogManager alert;
    private Internet_CheckingActivity chckInternt;
    private  GeoLocationFinder geoLocationFinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splashscreen);
        alert = new AlertDialogManager();
        chckInternt = new Internet_CheckingActivity(this);
    }
    @Override
    protected void onResume() {
        super.onResume();
        //  setupLocation();
        if (chckInternt.isNetworkAvailable() == true) {
            setupLocation();
        }
        else
        {
            alert.showAlertDialog(Splashscreen.this, "Error", "Please Check Your Internet Connection", false);
        }


    }
    /** Method for checking the current lat log values. */
    private void setupLocation() {
        GeoLocationFinder.LocationResult locationResult = new GeoLocationFinder.LocationResult() {

            @Override
            public void gotLocation(Location location) {
                if (location != null) {

                    newLocation = new Location(location);
                    newLocation.set(location);

                    Log.d(TAG,
                            "Got coordinates, congratulations. Longitude = "
                                    + newLocation.getLongitude() + " Latitude = "
                                    + newLocation.getLatitude());
                    Intent i = new Intent(Splashscreen.this, LoginActivity.class);
                    startActivity(i);
                    finish();
                } else{
                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                        @Override
                        public void run() {
                                alert.showAlertDialog(Splashscreen.this, "Check Your GPS", "Restart your Application", false);

                        }
                    });
                }
            }
        };
        geoLocationFinder = new GeoLocationFinder();
        geoLocationFinder.getLocation(this,locationResult);
    }
}

AlertDialogManager类

package com.driverapp.inis.zuber;


import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.ContextThemeWrapper;

public class AlertDialogManager {
    /**
     * Function to display simple Alert Dialog
     * @param context - application context
     * @param title - alert dialog title
     * @param message - alert message
     * @param status - success/failure (used to set icon)
     *               - pass null if you don't want icon
     * */

     public int setOk = 0;
     public void showAlertDialog(Context context, String title, String message,
            Boolean status) {
   //     AlertDialog alertDialog = new AlertDialog.Builder(context).create();
        AlertDialog.Builder  alertDialog= new AlertDialog.Builder(new ContextThemeWrapper(context,R.style.AlertDialogCustom));
        // Setting Dialog Title
        alertDialog.setTitle(title);

        // Setting Dialog Message
        alertDialog.setMessage(message);


        if(status != null)
            // Setting alert dialog icon
            alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);

        // Setting OK Button
        alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        // Showing Alert Message
        alertDialog.setCancelable(Boolean.FALSE);
        alertDialog.show();
    }

    /*Aler used for the delete in sheet details*/

    public void showAlertDialogDelete(final Context context, String title, String message,
                                Boolean status) {
        //     AlertDialog alertDialog = new AlertDialog.Builder(context).create();
        AlertDialog.Builder  alertDialog= new AlertDialog.Builder(new ContextThemeWrapper(context,R.style.AlertDialogCustom));
        // Setting Dialog Title
        alertDialog.setTitle(title);

        // Setting Dialog Message
        alertDialog.setMessage(message);


        if(status != null)
            // Setting alert dialog icon
            alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);

        // Setting OK Button
        alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Intent in = new Intent(context, Splashscreen.class);
                context.startActivity(in);
            }
        });
        // Showing Alert Message
        alertDialog.setCancelable(Boolean.FALSE);
        alertDialog.show();
    }

}

1 个答案:

答案 0 :(得分:0)

在Positive buton的OnClick()方法中添加以下代码:

Intent intent = new Intent(context, Splashscreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

context.startActivity(intent);

它将清除您的应用程序的活动堆栈并启动Splashscreen活动。