2015-08-13 37 views
1

您好,我正在运行一个问题。我的闪屏上有一个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(); 
    } 

} 
+0

显示'AlertDialogManager'类的代码。 –

+0

已编辑亲切地帮助我出问题@Prera​​kSola – larry

回答

0

添加以下代码在正布敦的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活动。

+0

得到一个错误“无法解决getApplicationContext()” – larry

+0

尝试'this'而不是'getApplicationContext'。 –

+0

“无法解析构造函数”试过后,甚至得到“无法解析开始活动”@Prera​​k Sola – larry

相关问题