问题描述
在我的应用程序退出我的GPS传感器一直工作或者当我改变我的活动应用程序崩溃根据我的配置(在code以下,当我改变的活动或退出应用程序崩溃)。如何更改我下面的活动,这样,当我退出应用程序GPS传感器停止运行,但是当我移动到它继续运行另一个活动?
When my application is quit my GPS sensor keeps working or when I change activities my application crashes depending on my configuration (in the code below it crashes when I change activities or quit the application). How do I change my below activity so that when I quit the application the GPS sensor stops running but when I move to another activity it keeps running?
下面是我的活动关键部分:
Here is the key parts to my activity:
public class InitialChoice extends Activity {
LocationManager mlocManager;
LocationListener mlocListener;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.initial_screen);
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
@Override
protected void onStop(){
//mlocManager.removeUpdates(mlocListener);
mlocManager.removeGpsStatusListener((Listener) mlocListener);
mlocManager = null;
}
}
如果我用下面的code代替的onStop()的GPS不会停止运行:
If I use the following code instead of onStop() the gps wont stop running:
@Override
public void finish(){
//mlocManager.removeUpdates(mlocListener);
mlocListener = null;
mlocManager = null;
}
LogCat中:
21 12-16:54:58.120:D /的LocationManager(909):requestLocationUpdates:供应商=全球定位系统,监听= com.dummies.android.taskreminder.MyLocationListener@46284458
12-16 21:55:04.740:D / AndroidRuntime(909):关闭VM
12-16 21:55:04.740:W / dalvikvm(909):主题ID = 1:螺纹未捕获的异常(组= 0x400259f8)退出
12-16 21:55:04.750:E / AndroidRuntime(909):致命异常:主要
12-16 21:55:04.750:E / AndroidRuntime(909):了java.lang.RuntimeException:无法停止活动{com.dummies.android.taskreminder / com.dummies.android.taskreminder.activity.InitialChoice}:JAVA。 lang.ClassCastException:com.dummies.android.taskreminder.MyLocationListener
12-16 21:55:04.750:E / AndroidRuntime(909):在android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3632)
12-16 21:55:04.750:E / AndroidRuntime(909):在android.app.ActivityThread.handleStopActivity(ActivityThread.java:3677)
12-16 21:55:04.750:E / AndroidRuntime(909):在android.app.ActivityThread.access $ 2600(ActivityThread.java:135)
12-16 21:55:04.750:E / AndroidRuntime(909):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2153)
12-16 21:55:04.750:E / AndroidRuntime(909):在android.os.Handler.dispatchMessage(Handler.java:99)
12-16 21:55:04.750:E / AndroidRuntime(909):在android.os.Looper.loop(Looper.java:144)
12-16 21:55:04.750:E / AndroidRuntime(909):在android.app.ActivityThread.main(ActivityThread.java:4937)
12-16 21:55:04.750:E / AndroidRuntime(909):在java.lang.reflect.Method.invokeNative(本机方法)
12-16 21:54:58.120: D/LocationManager(909): requestLocationUpdates: provider = gps, listener = com.dummies.android.taskreminder.MyLocationListener@4628445812-16 21:55:04.740: D/AndroidRuntime(909): Shutting down VM12-16 21:55:04.740: W/dalvikvm(909): threadid=1: thread exiting with uncaught exception (group=0x400259f8)12-16 21:55:04.750: E/AndroidRuntime(909): FATAL EXCEPTION: main12-16 21:55:04.750: E/AndroidRuntime(909): java.lang.RuntimeException: Unable to stop activity {com.dummies.android.taskreminder/com.dummies.android.taskreminder.activity.InitialChoice}: java.lang.ClassCastException: com.dummies.android.taskreminder.MyLocationListener12-16 21:55:04.750: E/AndroidRuntime(909): at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3632)12-16 21:55:04.750: E/AndroidRuntime(909): at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3677)12-16 21:55:04.750: E/AndroidRuntime(909): at android.app.ActivityThread.access$2600(ActivityThread.java:135)12-16 21:55:04.750: E/AndroidRuntime(909): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2153)12-16 21:55:04.750: E/AndroidRuntime(909): at android.os.Handler.dispatchMessage(Handler.java:99)12-16 21:55:04.750: E/AndroidRuntime(909): at android.os.Looper.loop(Looper.java:144)12-16 21:55:04.750: E/AndroidRuntime(909): at android.app.ActivityThread.main(ActivityThread.java:4937)12-16 21:55:04.750: E/AndroidRuntime(909): at java.lang.reflect.Method.invokeNative(Native Method)
从这里我新的错误与方法:
New Errors for me with method from here: Stop Location Listener in Android
23 12-16:10:50.295:W / dalvikvm(1272):主题ID = 1:螺纹未捕获的异常(组= 0x400259f8)退出
12-16 23:10:50.305:E / AndroidRuntime(1272):致命异常:主要
12-16 23:10:50.305:E / AndroidRuntime(1272):了java.lang.RuntimeException:无法停止活动{com.dummies.android.taskreminder / com.dummies.android.taskreminder.activity.InitialChoice}:机器人。 app.SuperNotCalledException:活动{com.dummies.android.taskreminder / com.dummies.android.taskreminder.activity.InitialChoice}没有通过调用super.onStop()
12-16 23:10:50.305:E / AndroidRuntime(1272):在android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3632)
12-16 23:10:50.305:E / AndroidRuntime(1272):在android.app.ActivityThread.handleStopActivity(ActivityThread.java:3677)
12-16 23:10:50.295: W/dalvikvm(1272): threadid=1: thread exiting with uncaught exception (group=0x400259f8)12-16 23:10:50.305: E/AndroidRuntime(1272): FATAL EXCEPTION: main12-16 23:10:50.305: E/AndroidRuntime(1272): java.lang.RuntimeException: Unable to stop activity {com.dummies.android.taskreminder/com.dummies.android.taskreminder.activity.InitialChoice}: android.app.SuperNotCalledException: Activity {com.dummies.android.taskreminder/com.dummies.android.taskreminder.activity.InitialChoice} did not call through to super.onStop()12-16 23:10:50.305: E/AndroidRuntime(1272): at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3632)12-16 23:10:50.305: E/AndroidRuntime(1272): at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3677)
推荐答案
现在这似乎运作得很好,但有可能是一个更好的方法(后台线程.....):
For now this seems to work reasonably well but there is probably a better way (background thread.....):
@Override
public void onDestroy(){
mlocManager.removeUpdates(mlocListener);
super.onDestroy();
}
@Override
public void onResume(){
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
super.onResume();
}
}
这篇关于有一些麻烦我的GPS传感器停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!