本文介绍了安卓:当停止GPS监听器强制关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个强制关闭,当我试图阻止我的普惠制监听
这里的code:

  mlocManager.removeUpdates(mlocListener);
mlocManager = NULL;
 

和这样的:

  LocationManager mlocManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener的mlocListener =新MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
(长)的更新,0,mlocListener);
 

请帮帮我,我不明白为什么它强行关闭。

  11月7号至三十零日:43:39.557:D / AndroidRuntime(1071):关闭虚拟机
11月7号至三十○日:43:39.557:W / dalvikvm(1071):主题ID = 1:螺纹退出与未捕获的异常(组= 0x40015560)
11月7号至三十○日:43:39.575:E / AndroidRuntime(1071):致命异常:主要
11月7号至三十○日:43:39.575:E / AndroidRuntime(1071):显示java.lang.NullPointerException
11月7号至三十○日:43:39.575:E / AndroidRuntime(1071):在com.example.anotheractivity.Festivale $ 1.onClick(Festivale.java:109)
11月7号至三十○日:43:39.575:E / AndroidRuntime(1071):在android.view.View.performClick(View.java:2485)
11月7号至三十○日:43:39.575:E / AndroidRuntime(1071):在android.view.View $ PerformClick.run(View.java:9080)
11月7号至三十○日:43:39.575:E / AndroidRuntime(1071):在android.os.Handler.handleCallback(Handler.java:587)
11月7号至三十○日:43:39.575:E / AndroidRuntime(1071):在android.os.Handler.dispatchMessage(Handler.java:92)
11月7号至三十○日:43:39.575:E / AndroidRuntime(1071):在android.os.Looper.loop(Looper.java:123)
11月7号至三十○日:43:39.575:E / AndroidRuntime(1071):在android.app.ActivityThread.main(ActivityThread.java:3683)
11月7号至三十○日:43:39.575:E / AndroidRuntime(1071):在java.lang.reflect.Method.invokeNative(本机方法)
11月7号至三十○日:43:39.575:E / AndroidRuntime(1071):在java.lang.reflect.Method.invoke(Method.java:507)
11月7号至三十○日:43:39.575:E / AndroidRuntime(1071):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
11月7号至三十○日:43:39.575:E / AndroidRuntime(1071):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11月7号至三十○日:43:39.575:E / AndroidRuntime(1071):在dalvik.system.NativeStart.main(本机方法)
11月7号至三十○日:43:41.565:I /过程(1071):发送信号。 PID:1071 SIG:9
 

解决方案

我该做一个服务。这是很容易使用它获得经度/纬度。

  1. 复制/粘贴到你的项目这个类。

包com.sample;    进口com.sample.globalconstant;

 进口android.app.Service;
进口android.content.Context;
进口android.content.Intent;
进口android.location.Location;
进口android.location.LocationManager;
进口android.os.Bundle;
进口android.os.IBinder;
进口android.util.Log;
进口android.widget.Toast;

公共类MyServiceGPS扩展服务
{
    私有静态最后字符串变量=BOOMBOOMTESTGPS;
    私人LocationManager mLocationManager = NULL;
    私有静态最终诠释LOCATION_INTERVAL = 1000;
    私有静态最终浮动LOCATION_DISTANCE = 10F;

    私有类LocationListener的实现android.location.LocationListener {
        地点mLastLocation;
        公共LocationListener的(字符串提供商)
        {
            Log.e(TAG,LocationListener的+供应商);
            mLastLocation =新的位置(供应商);
        }
        公共无效onLocationChanged(位置定位)
        {
            Log.e(TAG,onLocationChanged:+ location.getLatitude()+.....+ location.getLongitude());
            globalconstant.lat = location.getLatitude();
            globalconstant.lon = location.getLongitude();
            Toast.makeText(getApplicationContext(),location.getLatitude()+.....+ location.getLongitude(),1000).show();
            mLastLocation.set(位置);
        }
        公共无效onProviderDisabled(字符串提供商)
        {
            Log.e(TAG,onProviderDisabled:+供应商);
        }
        公共无效onProviderEnabled(字符串提供商)
        {
            Log.e(TAG,onProviderEnabled:+供应商);
        }
        公共无效onStatusChanged(字符串商,INT地位,捆绑演员)
        {
            Log.e(TAG,onStatusChanged:+供应商);
        }
    }
    LocationListener的[] mLocationListeners =新LocationListener的[] {
            新LocationListener的(LocationManager.GPS_PROVIDER)
            新LocationListener的(LocationManager.NETWORK_PROVIDER)
    };
    @覆盖
    公众的IBinder onBind(意向为arg0)
    {
        返回null;
    }
    @覆盖
    公众诠释onStartCommand(意向意图,诠释标志,INT startId)
    {
        Log.e(TAG,onStartCommand);
        super.onStartCommand(意向,标志,startId);
        返回START_STICKY;
    }
    @覆盖
    公共无效的onCreate()
    {
        Log.e(TAG的onCreate);
        initializeLocationManager();
        尝试 {
            mLocationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER,LOCATION_INTERVAL,LOCATION_DISTANCE,
                    mLocationListeners [1]);

        }赶上(java.lang.SecurityException异常前){
            Log.i(TAG,不要求位置更新,忽略,前);
        }赶上(抛出:IllegalArgumentException前){
            Log.d(TAG,网络提供商不存在,+ ex.getMessage());
        }
        尝试 {
            mLocationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER,LOCATION_INTERVAL,LOCATION_DISTANCE,
                    mLocationListeners [0]);
        }赶上(java.lang.SecurityException异常前){
            Log.i(TAG,不要求位置更新,忽略,前);
        }赶上(抛出:IllegalArgumentException前){
            Log.d(TAG,GPS供应商不存在+ ex.getMessage());
        }
    }
    @覆盖
    公共无效的onDestroy()
    {
        Log.e(TAG的onDestroy);
        super.onDestroy();
        如果(mLocationManager!= NULL){
            的for(int i = 0; I< mLocationListeners.length;我++){
                尝试 {
                    mLocationManager.removeUpdates(mLocationListeners [I]);
                }赶上(例外前){
                    Log.i(TAG,无法删除的位置listners,忽视,前);
                }
            }
        }
    }
    私人无效initializeLocationManager(){
        Log.e(TAG,initializeLocationManager);
        如果(mLocationManager == NULL){
            mLocationManager =(LocationManager)getApplicationContext()getSystemService(Context.LOCATION_SERVICE)。
        }
    }
}
 

  1. 在你的活动复制此code时,要启动:

    startService(新意图(这一点,MyServiceGPS.class));

  2. 创建一个类globalconstant:

    公共类globalconstant    {     公共静态双重纬度,经度;    }

    当你想当前的纬度和经度在项目中只写本 globalconstant.lat,globalconstant.lon

  3. 添加使用,许可清单

            

I got a force close when I tried to stop my gsp listener
Here's the code:

mlocManager.removeUpdates(mlocListener);
mlocManager = null;

and this:

LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
(long) update, 0, mlocListener);

Please help me, I don't understand why it force closes.

07-30 11:43:39.557: D/AndroidRuntime(1071): Shutting down VM
07-30 11:43:39.557: W/dalvikvm(1071): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-30 11:43:39.575: E/AndroidRuntime(1071): FATAL EXCEPTION: main
07-30 11:43:39.575: E/AndroidRuntime(1071): java.lang.NullPointerException
07-30 11:43:39.575: E/AndroidRuntime(1071):     at com.example.anotheractivity.Festivale$1.onClick(Festivale.java:109)
07-30 11:43:39.575: E/AndroidRuntime(1071):     at android.view.View.performClick(View.java:2485)
07-30 11:43:39.575: E/AndroidRuntime(1071):     at android.view.View$PerformClick.run(View.java:9080)
07-30 11:43:39.575: E/AndroidRuntime(1071):     at android.os.Handler.handleCallback(Handler.java:587)
07-30 11:43:39.575: E/AndroidRuntime(1071):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-30 11:43:39.575: E/AndroidRuntime(1071):     at android.os.Looper.loop(Looper.java:123)
07-30 11:43:39.575: E/AndroidRuntime(1071):     at android.app.ActivityThread.main(ActivityThread.java:3683)
07-30 11:43:39.575: E/AndroidRuntime(1071):     at java.lang.reflect.Method.invokeNative(Native Method)
07-30 11:43:39.575: E/AndroidRuntime(1071):     at java.lang.reflect.Method.invoke(Method.java:507)
07-30 11:43:39.575: E/AndroidRuntime(1071):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-30 11:43:39.575: E/AndroidRuntime(1071):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-30 11:43:39.575: E/AndroidRuntime(1071):     at dalvik.system.NativeStart.main(Native Method)
07-30 11:43:41.565: I/Process(1071): Sending signal. PID: 1071 SIG: 9
解决方案

I made one service for that. It is easy for get Longitude / Latitude using it.

  1. Copy/paste this class in your project.

package com.sample; import com.sample.globalconstant;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyServiceGPS extends Service
{
    private static final String TAG = "BOOMBOOMTESTGPS";
    private LocationManager mLocationManager = null;
    private static final int LOCATION_INTERVAL = 1000;
    private static final float LOCATION_DISTANCE = 10f;

    private class LocationListener implements android.location.LocationListener{
        Location mLastLocation;
        public LocationListener(String provider)
        {
            Log.e(TAG, "LocationListener " + provider);
            mLastLocation = new Location(provider);
        }
        public void onLocationChanged(Location location)
        {
            Log.e(TAG, "onLocationChanged: " + location.getLatitude() +"....."+ location.getLongitude());
            globalconstant.lat  = location.getLatitude();
            globalconstant.lon  = location.getLongitude();
            Toast.makeText(getApplicationContext(), location.getLatitude() +"....."+ location.getLongitude(), 1000).show();
            mLastLocation.set(location);
        }
        public void onProviderDisabled(String provider)
        {
            Log.e(TAG, "onProviderDisabled: " + provider);
        }
        public void onProviderEnabled(String provider)
        {
            Log.e(TAG, "onProviderEnabled: " + provider);
        }
        public void onStatusChanged(String provider, int status, Bundle extras)
        {
            Log.e(TAG, "onStatusChanged: " + provider);
        }
    }
    LocationListener[] mLocationListeners = new LocationListener[] {
            new LocationListener(LocationManager.GPS_PROVIDER),
            new LocationListener(LocationManager.NETWORK_PROVIDER)
    };
    @Override
    public IBinder onBind(Intent arg0)
    {
        return null;
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        Log.e(TAG, "onStartCommand");
        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }
    @Override
    public void onCreate()
    {
        Log.e(TAG, "onCreate");
        initializeLocationManager();
        try {
            mLocationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                    mLocationListeners[1]);

        } catch (java.lang.SecurityException ex) {
            Log.i(TAG, "fail to request location update, ignore", ex);
        } catch (IllegalArgumentException ex) {
            Log.d(TAG, "network provider does not exist, " + ex.getMessage());
        }
        try {
            mLocationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                    mLocationListeners[0]);
        } catch (java.lang.SecurityException ex) {
            Log.i(TAG, "fail to request location update, ignore", ex);
        } catch (IllegalArgumentException ex) {
            Log.d(TAG, "gps provider does not exist " + ex.getMessage());
        }
    }
    @Override
    public void onDestroy()
    {
        Log.e(TAG, "onDestroy");
        super.onDestroy();
        if (mLocationManager != null) {
            for (int i = 0; i < mLocationListeners.length; i++) {
                try {
                    mLocationManager.removeUpdates(mLocationListeners[i]);
                } catch (Exception ex) {
                    Log.i(TAG, "fail to remove location listners, ignore", ex);
                }
            }
        }
    }
    private void initializeLocationManager() {
        Log.e(TAG, "initializeLocationManager");
        if (mLocationManager == null) {
            mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        }
    }
}
  1. Copy this code in your Activity when you want to start:

    startService(new Intent(this,MyServiceGPS.class));

  2. Create one class globalconstant:

    public class globalconstant { public static double lat, lon; }

    when you want to current latitude and longitude in your project only write this globalconstant.lat ,globalconstant.lon

  3. Add uses-permission in Manifest

这篇关于安卓:当停止GPS监听器强制关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-19 09:24