5

我正在创建一个使用 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 类的按钮单击时调用此服务。

任何帮助,将不胜感激。

4

3 回答 3

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

gpsLocationListner is

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){}
};

gpsStatusListener

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){}
            }
        }
    };
于 2012-11-23T07:03:13.643 回答
2

You just need to Create one BroadcastReceiver

public class InternetConnectionBroadcast extends BroadcastReceiver 
 {
    private Handler handler = new Handler();
    public static String state;
    private Context netcontext;
    public void onReceive(Context context, Intent intent) 
    {
     netcontext = context;
     NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

     if(null != info)
     {
             state = getNetworkStateString(info.getState());
             Log.i("----------Network State",state);
     }
}

private String getNetworkStateString(NetworkInfo.State state)
{
    String stateString = "Unknown";
    switch(state)
    {
            case CONNECTED:         stateString = "Connected";      break;

            case CONNECTING:        stateString = "Connecting";     break;

            case DISCONNECTED:      

                stateString = "Disconnected";  
                handler.removeCallbacks(sendUpdatesToUI);
                handler.post(sendUpdatesToUI);

                break;

            case DISCONNECTING:     stateString = "Disconnecting";  break;

            case SUSPENDED:         stateString = "Suspended";      break;

            default:                stateString = "Unknown";        break;
    }
    return stateString;
}


private Runnable sendUpdatesToUI = new Runnable() 
{
     public void run() 
     {
             Toast.makeText(netcontext,"Please Check Your Internet Connection",Toast.LENGTH_SHORT).show();   
     }
}; }

Also Register into manifest.xml

<receiver android:name="com.example.utility.InternetConnectionBroadcast">
     <intent-filter>
     <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 
     </intent-filter>
     </receiver>

Now whenever your connection will lost, it will notify message to the user.

于 2012-11-23T06:49:35.647 回答
1

尝试使用 GPSStatus.Listener:

http://developer.android.com/reference/android/location/GpsStatus.Listener.html

于 2012-11-23T06:44:50.553 回答