Мой код работает нормально, если включен GPS, но когда его нужно включить, он принудительно падает. Что-то связанное с попыткой получить местоположение, когда GPS не включен. Я пытаюсь открыть настройки для пользователя, чтобы включить GPS, но он просто продолжает пытаться записать GPS (отсюда принудительный сбой).
Я не знаю, когда и где позвонить Майну.
Первый StartupSettings
называется:
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);
}
}
Метод enableGPS()
, вызываемый StartUpSettings() Activity
выше:
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;
}
Я хочу затем позвонить Main
, который запускает AsyncTask
с именем 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);
}
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)