توسط
Javad Mahmoodi
سلام
من در برنامه ام از یک کلاسی استفاده می کنم که بصورت اتوماتیک متوجه میشه که GPS الان روشن هست یا خاموش.
تو این کلاس تعریف کردم زمانی که GPS خاموش شد یک دیالوگ در صفحه نشون بده و به محض روشن شدن GPS دیالوگ از بین بره.
زمانی که برنامه را اجرا میکنم در صفحه اول که لاگین (ورود کاربر) هست کاملا درست کار میکنه.
ولی بعد از صفحه لاگین و در صفحه اصلی برنامه وقتی GPS خاموش میشه هنگام نمایش دیالوگ فورس کلوز میده!!!
ممنون میشم راهنمایی بفرمایید.
باتشکر
کلاس GPS :
public class GPSTracker implements LocationListener {
public static double x, y;
private final Context mContext;
public int code = 0;
private Handler handler;
AlertDialog.Builder alertDialog;
AlertDialog.Builder builder;
AlertDialog alert;
boolean isGPSEnabled = false;
boolean canGetLocation = false;
boolean isNetworkEnabled = false;
double latitude;
double longitude;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
Location location;
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
@SuppressLint("MissingPermission")
public Location getLocation() {
try {
locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isGPSEnabled || isNetworkEnabled) {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if (isGPSEnabled && location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
public void stopUsingGPS() {
if (locationManager != null) {
locationManager.removeUpdates(GPSTracker.this);
}
}
public boolean canGetLocation() {
return this.canGetLocation;
}
public void showSettingsAlert() {
builder = new AlertDialog.Builder(mContext);
View view = LayoutInflater.from(mContext).inflate(R.layout.custom_layout, null);
TextView title = view.findViewById(R.id.Alert_Title);
TextView txt_comment_dialog = view.findViewById(R.id.txt_comment_dialog);
ImageButton imageButton = view.findViewById(R.id.Alert_Image);
title.setText("لطفا موقعیت مکانی دستگاه را روشن نمایید.");
txt_comment_dialog.setText("دسترسی به موقعیت شما امکان پذیر نیست.");
imageButton.setImageResource(R.drawable.location_off);
builder.setPositiveButton("تنظیمات", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
builder.setCancelable(false);
builder.setView(view);
alert = builder.create();
alert.show();
code = 1;
}
@Override
public void onLocationChanged(Location location) {
String str = "Latitude: " + location.getLatitude() + " Longitude: " + location.getLongitude();
//Toast.makeText(mContext, str, Toast.LENGTH_LONG).show();
x = location.getLatitude();
y = location.getLongitude();
}
@Override
public void onProviderDisabled(String provider) {
if (code == 0) {
showSettingsAlert();
}
// Toast.makeText(mContext, "مکان یاب خاموش است", Toast.LENGTH_LONG).show();
}
@Override
public void onProviderEnabled(String provider) {
if (code == 1) {
alert.dismiss();
code = 0;
} else {
code = 0;
}
//Toast.makeText(mContext, "مکان یاب روشن است", Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
ارور :
2019-02-23 13:00:02.509 8443-8451/? E/art: Failed sending reply to debugger: Broken pipe
2019-02-23 13:00:03.658 8443-8460/com.uncocoder.course.app.database E/libEGL: load_driver(/system/lib/egl/libGLES_emulation.so): dlopen failed: library "/system/lib/egl/libGLES_emulation.so" not found
2019-02-23 13:01:03.029 8443-8443/com.uncocoder.course.app.database E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.uncocoder.course.app.database, PID: 8443
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@d94f2a4 is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:678)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at android.app.Dialog.show(Dialog.java:316)
at com.uncocoder.course.app.database.GPSTracker.showSettingsAlert(GPSTracker.java:125)
at com.uncocoder.course.app.database.GPSTracker.onProviderDisabled(GPSTracker.java:142)
at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:310)
at android.location.LocationManager$ListenerTransport.-wrap0(LocationManager.java)
at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:242)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
پست های پیشنهاد شده
به گفتگو بپیوندید
هم اکنون می توانید مطلب خود را ارسال نمایید و بعداً ثبت نام کنید. اگر حساب کاربری دارید، برای ارسال با حساب کاربری خود اکنون وارد شوید .