当Android中的用户禁用GPS时如何收到通知?

2023-11-22

我正在创建一个使用 GPS 的应用程序。第一次在onCreate()我正在检查 GPS 是否启用。如果未启用,那么我会将用户发送到设置菜单以将 GPS 设置为启用。

一旦 GPS 启用,我就开始执行我的工作。但是,如果用户在通知管理器上停止 GPS 滚动,那么我怎么知道这一点?

因为我的方法只在未启用 GPS 时执行一次。之后,当再次禁用它时,它不会被执行。

I just want to know whenever in between the work the GPS is disabled by the user. Just this information is required.

这是我的代码。

class LocationFinderService extends Service{

    boolean gps_enabled;
    LocationManager locationManager;
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        if(!isGpsEnabled())
        {
            startGpsForLocation();
            stopSelf();
        }

    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    public boolean isGpsEnabled()
    {
        gps_enabled = (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)&& locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER));
        Log.i("Log", "Gps:  "+gps_enabled);
        return gps_enabled;
    }

    public void startGpsForLocation()
    {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) ;
        startActivity(intent);
        Toast.makeText(this, "Please start the GPS", Toast.LENGTH_LONG).show();
    }
}

我在 Activity 类的按钮单击上调用此服务。

任何帮助,将不胜感激。


        lmGps.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,gpsLocationListener);
        lmGps.addGpsStatusListener(gpsStatusListener);

gpsLocationListner 是

LocationListener gpsLocationListener=new LocationListener(){
        public void onLocationChanged(Location location){
            dGpsLat=location.getLatitude();
            dGpsLng=location.getLongitude();


        dLat=dGpsLat;
        dLng=dGpsLng;
        try{
        SaveLocation();
    }
        catch (Exception e) {
            sResponse=e.toString();
            return;
        }
    }
    public void onStatusChanged(String provider,int status,Bundle extras){}
    public void onProviderEnabled(String provider){}
    public void onProviderDisabled(String provider){}
};

GPS状态监听器

GpsStatus.Listener gpsStatusListener=new GpsStatus.Listener(){
        @Override
        public void onGpsStatusChanged(int event){
            if(event==GpsStatus.GPS_EVENT_SATELLITE_STATUS){
                try{
                    GpsStatus status=lmGps.getGpsStatus(null);
                    sats=status.getSatellites();
                    Iterator satI=sats.iterator();

                    int count=0;
                    while(satI.hasNext()){
                        GpsSatellite gpssatellite=(GpsSatellite) satI.next();
                        if(gpssatellite.usedInFix()){
                            count++;
                        }
                    }
                    iSalelliteCount=count;
                }
                catch(Exception ex){}
            }
        }
    };
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当Android中的用户禁用GPS时如何收到通知? 的相关文章

随机推荐