如何在android中用户禁用gps时收到通知?

时间:2012-11-23 06:34:29

标签: android gps

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

任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:2)

您只需要创建一个 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();   
     }
}; }

还注册到manifest.xml

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

现在,只要您的连接丢失,它就会将消息通知给用户。

答案 2 :(得分:1)