gpt4 book ai didi

java - GPS 启用检查后销毁并重新启动应用程序

转载 作者:太空宇宙 更新时间:2023-11-04 13:32:54 24 4
gpt4 key购买 nike

您好,我运行时遇到问题。我的初始屏幕上有一个 GPS 检查功能,在检查 GPS 连接后,如果其状态为空,它会显示一个警报。我想在单击警报框的按钮后销毁并重新启动我的应用程序。请帮忙。这是我的代码

地理位置查找器类

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);
}
}

启动画面类

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();
}

}

最佳答案

在 Positive 按钮的 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);

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

关于java - GPS 启用检查后销毁并重新启动应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31991342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com