How can I check the current status of the GPS receiver?

前端 未结 17 1770
时光取名叫无心
时光取名叫无心 2020-11-22 08:53

How can I check the current status of the GPS receiver? I already checked the LocationListener onStatusChanged method but somehow it seems that is not working,

17条回答
  •  野性不改
    2020-11-22 09:36

    The GPS icon seems to change its state according to received broadcast intents. You can change its state yourself with the following code samples:

    Notify that the GPS has been enabled:

    Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
    intent.putExtra("enabled", true);
    sendBroadcast(intent);
    

    Notify that the GPS is receiving fixes:

    Intent intent = new Intent("android.location.GPS_FIX_CHANGE");
    intent.putExtra("enabled", true);
    sendBroadcast(intent);
    

    Notify that the GPS is no longer receiving fixes:

    Intent intent = new Intent("android.location.GPS_FIX_CHANGE");
    intent.putExtra("enabled", false);
    sendBroadcast(intent);
    

    Notify that the GPS has been disabled:

    Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
    intent.putExtra("enabled", false);
    sendBroadcast(intent);
    

    Example code to register receiver to the intents:

    // MyReceiver must extend BroadcastReceiver
    MyReceiver receiver = new MyReceiver();
    IntentFilter filter = new IntentFilter("android.location.GPS_ENABLED_CHANGE");
    filter.addAction("android.location.GPS_FIX_CHANGE");
    registerReceiver(receiver, filter);
    

    By receiving these broadcast intents you can notice the changes in GPS status. However, you will be notified only when the state changes. Thus it is not possible to determine the current state using these intents.

提交回复
热议问题