通过Notification解决Android8.0下后台无定位的问题,兼容Android 9、Android 10

本文详细介绍了在Android 8.0及以上版本,由于后台执行限制导致后台无定位数据的问题。解决方案是利用Notification创建前台服务,确保程序在后台时仍能获取定位数据。文章涵盖了NotificationUtil和服务类ServiceNotification的实现,以及在AndroidManifest.xml中的配置,并讨论了Android 8.0的后台服务和广播限制,以及对Android 9和10的兼容性处理。
摘要由CSDN通过智能技术生成

问题描述

在Android 8.0上获取定位数据,在程序切换到后台时,系统无定位数据输出。

$GNGGA,080930.174,2259.0118,N,11322.0587,E,1,24,0.56,39.0,M,-6.0,M,,*5B
$GNGGA,080931.174,2259.0118,N,11322.0587,E,1,24,0.56,38.8,M,-6.0,M,,*53
$GNGGA,080932.175,2259.0119,N,11322.0589,E,1,24,0.56,38.8,M,-6.0,M,,*5E
$GNGGA,080933.174,2259.0120,N,11322.0589,E,1,24,0.56,38.5,M,-6.0,M,,*59
$GNGGA,080934.175,2259.0120,N,11322.0589,E,1,24,0.56,38.4,M,-6.0,M,,*5E
$GNGGA,080949.377,2259.0120,N,11322.0589,E,0,0,,38.4,M,-6.0,M,,*7E
$GNGGA,080950.378,2259.0120,N,11322.0589,E,1,21,0.59,38.3,M,-6.0,M,,*5E
$GNGGA,080951.878,2259.0123,N,11322.0588,E,1,23,0.57,37.3,M,-6.0,M,,*55
$GNGGA,080952.879,2259.0125,N,11322.0588,E,1,23,0.57,36.9,M,-6.0,M,,*5A
$GNGGA,080953.879,2259.0126,N,11322.0589,E,1,24,0.54,36.6,M,-6.0,M,,*52

在9分34秒后开始丢失数据,到9分49秒重新切换程序到前台时恢复。

MainActivity.java

public class MainActivity extends AppCompatActivity {

    /**
     * 位置管理器
     */
    private LocationManager locationManager = null;

    /**
     * 保存NMEA
     */
    private FileOutputStream fos = null;

    private TextView tvProvider = null;
    private TextView tvTime = null;
    private TextView tvLatitude = null;
    private TextView tvLongitude = null;
    private TextView tvAltitude = null;
    private TextView tvBearing = null;
    private TextView tvSpeed = null;
    private TextView tvAccuracy = null;
    private CheckBox cbSaveNmea = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        App.getInstance().setMainActivity(this);

        tvProvider = findViewById(R.id.tv_provider);
        tvTime = findViewById(R.id.tv_time);
        tvLatitude = findViewById(R.id.tv_latitude);
        tvLongitude = findViewById(R.id.tv_longitude);
        tvAltitude = findViewById(R.id.tv_altitude);
        tvBearing = findViewById(R.id.tv_bearing);
        tvSpeed = findViewById(R.id.tv_speed);
        tvAccuracy = findViewById(R.id.tv_accuracy);
        cbSaveNmea = findViewById(R.id.cb_save);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // 注册位置服务,获取系统位置
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
        // 监听NMEA
        locationManager.addNmeaListener(nmeaListener);
    }

    @Override
    protected void onDestroy() {
        if (fos != null) {
            try {
                fos.flush();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            fos = null;
        }
        locationManager.removeNmeaListener(nmeaListener);
        locationManager.removeUpdates(locationListener);
        super.onDestroy();
    }

    /**
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值