How to structure code when need to enable GPS
-
13-11-2019 - |
Question
My code works fine if the GPS is enabled, but when it needs to be enabled it force crashes. Something to do with trying to get the location when the GPS is not enabled. I try to open up Settings for the user to enable GPS but it just continues trying to record GPS (hence force-crashing).
I don't know when and where to call Main.
First StartupSettings
is called:
public class StartupSettings extends Activity{
boolean hasGps = false;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.startup_dialog);
checkGPS();
}
public void checkGPS()
{
LocationHandler2 lh = new LocationHandler2();
hasGps = lh.enableGPS(this);
if (hasGps == true)
{
TextView tv = (TextView) findViewById(R.id.checkGPSConnection);
tv.setTextColor(Color.GREEN);
}
}
The enableGPS()
method called by the StartUpSettings() Activity
above:
public class LocationHandler2{
LocationManager mlocManager;
public boolean enableGPS(final StartupSettings main)
{
mlocManager = (LocationManager)main.getSystemService(Context.LOCATION_SERVICE);
if(!mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
//Ask the user to enable GPS
AlertDialog.Builder builder = new AlertDialog.Builder(main);
builder.setTitle("Location Manager");
builder.setMessage("Would you like to enable GPS?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Launch settings, allowing user to make a change
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
main.startActivity(i);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//No location service, no Activity
main.finish();
}
});
builder.create().show();
}
else
{
return true;
}
return true;
}
I want to then call Main
which starts up an AsyncTask
called StartProcess()
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pages);
StartProcess sProcess = new StartProcess();
sProcess.execute(this);
}
The StartProcess AsyncTask
:
public class StartProcess extends AsyncTask<Main, Void, Void>
{
@Override
protected Void doInBackground(Main... params) {
LocationHandler2 lh = new LocationHandler2();
try {
lh.getLocationStartEnd(params[0],0);
}
catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
}
Logcat:
02-26 17:33:31.264: E/AndroidRuntime(10560): FATAL EXCEPTION: AsyncTask #1
02-26 17:33:31.264: E/AndroidRuntime(10560): java.lang.RuntimeException: An error occured while executing doInBackground()
02-26 17:33:31.264: E/AndroidRuntime(10560): at android.os.AsyncTask$3.done(AsyncTask.java:200)
02-26 17:33:31.264: E/AndroidRuntime(10560): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
02-26 17:33:31.264: E/AndroidRuntime(10560): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
02-26 17:33:31.264: E/AndroidRuntime(10560): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
02-26 17:33:31.264: E/AndroidRuntime(10560): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-26 17:33:31.264: E/AndroidRuntime(10560): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
02-26 17:33:31.264: E/AndroidRuntime(10560): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
02-26 17:33:31.264: E/AndroidRuntime(10560): at java.lang.Thread.run(Thread.java:1096)
02-26 17:33:31.264: E/AndroidRuntime(10560): Caused by: java.lang.IllegalArgumentException: provider==null
02-26 17:33:31.264: E/AndroidRuntime(10560): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:653)
02-26 17:33:31.264: E/AndroidRuntime(10560): at mfc.generalguixapi8.LocationHandler2.getLocationStartEnd(LocationHandler2.java:120)
02-26 17:33:31.264: E/AndroidRuntime(10560): at mfc.generalguixapi8.StartProcess.doInBackground(StartProcess.java:13)
02-26 17:33:31.264: E/AndroidRuntime(10560): at mfc.generalguixapi8.StartProcess.doInBackground(StartProcess.java:1)
02-26 17:33:31.264: E/AndroidRuntime(10560): at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-26 17:33:31.264: E/AndroidRuntime(10560): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-26 17:33:31.264: E/AndroidRuntime(10560): ... 4 more
02-26 17:34:37.694: E/global(10620): Deprecated Thread methods are not supported.
02-26 17:34:37.694: E/global(10620): java.lang.UnsupportedOperationException
02-26 17:34:37.694: E/global(10620): at java.lang.VMThread.stop(VMThread.java:85)
02-26 17:34:37.694: E/global(10620): at java.lang.Thread.stop(Thread.java:1379)
02-26 17:34:37.694: E/global(10620): at java.lang.Thread.stop(Thread.java:1344)
02-26 17:34:37.694: E/global(10620): at mfc.generalguixapi8.SplashScreen$1.run(SplashScreen.java:36)
02-26 17:36:18.134: E/global(10713): Deprecated Thread methods are not supported.
02-26 17:36:18.134: E/global(10713): java.lang.UnsupportedOperationException
02-26 17:36:18.134: E/global(10713): at java.lang.VMThread.stop(VMThread.java:85)
02-26 17:36:18.134: E/global(10713): at java.lang.Thread.stop(Thread.java:1379)
02-26 17:36:18.134: E/global(10713): at java.lang.Thread.stop(Thread.java:1344)
02-26 17:36:18.134: E/global(10713): at mfc.generalguixapi8.SplashScreen$1.run(SplashScreen.java:36)
Solution 2
I ended up moving the enableGPS()
method to my StartupSettings
activity, and then used startActivityForResult()
to start Main
when the user enables the GPS.
Not sure if that's ideal but solved!
OTHER TIPS
Yow lady, I dont know whats your problem, but for GPS I did this tutorials http://thenewboston.org/list.php?cat=6 and it works fine, no crashing. For tutorials I do not know which exactly, they should be a little over 100. You must just find tutorials for GPS, this is 30 min of tutorials to do. I hope this is it?