【问题标题】:How to get notified when the gps in disabled by the user in android?当用户在android中禁用gps时如何获得通知?
【发布时间】:2012-11-23 06:34:28
【问题描述】:

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

任何帮助将不胜感激。

【问题讨论】:

标签: android gps


【解决方案1】:
        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){}
            }
        }
    };

【讨论】:

  • 位置监听器没有被调用
  • 应该叫它请再检查一遍
  • 我正在尝试在 onLocationChanged() 内的 logcat jsu 中打印。但它不打印。请帮帮我
【解决方案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>

现在,每当您的连接丢失时,它都会向用户通知消息。

【讨论】:

    【解决方案3】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      • 1970-01-01
      • 2015-07-25
      • 2020-12-21
      • 2011-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多