android - GPS 状态变化的 Action

标签 android android-intent gps

我正在寻找 GPS 状态更改时提供的 IntentFilter 操作。我找到了这个 filter.addAction("android.location.GPS_ENABLED_CHANGE"); 但它不起作用。我正在寻找与蓝牙相同的东西
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); 但对于 GPS。

最佳答案

这里是例子。

public class Main extends Activity implements LocationListener {

    /* this class implements LocationListener, which listens for both
     * changes in the location of the device and changes in the status
     * of the GPS system.
     * */

    static final String tag = "Main"; // for Log

    LocationManager lm; 
    StringBuilder sb;
    int noOfFixes = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /* the location manager is the most vital part it allows access 
         * to location and GPS status services */
        lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    }

    @Override
    protected void onResume() {
        /*
         * onResume is is always called after onStart, even if the app hasn't been
         * paused
         * 
         * add location listener and request updates every 1000ms or 10m
         */
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, this);
        super.onResume();
    }

    @Override
    protected void onPause() {
        /* GPS, as it turns out, consumes battery like crazy */
        lm.removeUpdates(this);
        super.onPause();
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.v(tag, "Location Changed");

        sb = new StringBuilder(512);

        noOfFixes++;

        /* display some of the data in the TextView */

        sb.append("No. of Fixes: ");
        sb.append(noOfFixes);
        sb.append('\n');
        sb.append('\n');

        sb.append("Londitude: ");
        sb.append(location.getLongitude());
        sb.append('\n');

        sb.append("Latitude: ");
        sb.append(location.getLatitude());
        sb.append('\n');

        sb.append("Altitiude: ");
        sb.append(location.getAltitude());
        sb.append('\n');

        sb.append("Accuracy: ");
        sb.append(location.getAccuracy());
        sb.append('\n');

        sb.append("Timestamp: ");
        sb.append(location.getTime());
        sb.append('\n');
        Log.v(tag, sb.toString());
    }

    @Override
    public void onProviderDisabled(String provider) {
        /* this is called if/when the GPS is disabled in settings */
        Log.v(tag, "Disabled");

        /* bring up the GPS settings */
        Intent intent = new Intent(
                android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.v(tag, "Enabled");
        Toast.makeText(this, "GPS Enabled", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        /* This is called when the GPS status alters */
        switch (status) {
        case LocationProvider.OUT_OF_SERVICE:
            Log.v(tag, "Status Changed: Out of Service");
            Toast.makeText(this, "Status Changed: Out of Service",
                    Toast.LENGTH_SHORT).show();
            break;
        case LocationProvider.TEMPORARILY_UNAVAILABLE:
            Log.v(tag, "Status Changed: Temporarily Unavailable");
            Toast.makeText(this, "Status Changed: Temporarily Unavailable",
                    Toast.LENGTH_SHORT).show();
            break;
        case LocationProvider.AVAILABLE:
            Log.v(tag, "Status Changed: Available");
            Toast.makeText(this, "Status Changed: Available",
                    Toast.LENGTH_SHORT).show();
            break;
        }
    }

    @Override
    protected void onStop() {
        /* may as well just finish since saving the state is not important for this toy app */
        finish();
        super.onStop();
    }
}

关于android - GPS 状态变化的 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9134894/

相关文章:

Android--调用invalidate()但是view不刷新

android - 交错网格布局管理器。如何在第一行有一个项目,其他行有 2 个项目

android - 从电子邮件链接启动 Activity

java - 收到推送通知和未调用 CGM Intent 服务时应用程序崩溃

android - 如何在从另一个 Activity 调用一个 Activity 时延迟一些?

java - Android - requestLocationUpdates 导致标识符预期错误

java - 有没有办法从 DefaultHandler 中停止或退出解析?

android - Kotlin readBytes() 永远不会完成

android - java.lang.SecurityException : Permission Denial: not allowed to send broadcast android. intent.action.MEDIA_MOUNTED 仅在 KitKat 上

php - 如何根据 GPS 位置的接近程度过滤数据库结果?