| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- package eVVM.apk.ui.home;
- import android.Manifest;
- import android.app.PendingIntent;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.location.Criteria;
- import android.location.Location;
- import android.location.LocationManager;
- import android.nfc.NfcAdapter;
- import android.os.Build;
- import android.support.annotation.NonNull;
- import android.support.v4.app.ActivityCompat;
- import android.util.Log;
- import eVVM.apk.helper.GPS.LocationUtils;
- import eVVM.apk.helper.SPUtils;
- import eVVM.apk.mvp.MvpActivity;
- import eVVM.apk.ui.home.uploadChip.UploadChipPresenter;
- public class BaseGPSActivity extends MvpActivity<UploadChipPresenter> {
- private boolean flag;
- /**
- * onCreat->onStart->onResume->onPause->onStop->onDestroy
- * 启动Activity,界面可见时.
- */
- @Override
- protected void onStart() {
- super.onStart();
- //获取最新位置信息
- getBestLocation();
- }
- /**
- * 获得焦点,按钮可以点击
- */
- @Override
- public void onResume() {
- super.onResume();
- initPermission();//针对6.0以上版本做权限适配
- }
- private void initPermission() {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- //检查权限
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
- || ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
- //请求权限
- ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 1);
- } else {
- flag = true;
- }
- } else {
- flag = true;
- }
- }
- /**
- * 权限的结果回调函数
- */
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- if (requestCode == 1) {
- flag = grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED;
- }
- }
- /**
- * 暂停Activity,界面获取焦点,按钮可以点击
- */
- @Override
- public void onPause() {
- super.onPause();
- }
- @Override
- protected int getLayoutId() {
- return 0;
- }
- @Override
- protected int getTitleId() {
- return 0;
- }
- @Override
- protected void initView() {
- }
- @Override
- protected void initData() {
- }
- @Override
- public void onPointerCaptureChanged(boolean hasCapture) {
- }
- @Override
- protected UploadChipPresenter createPresenter() {
- return new UploadChipPresenter();
- }
- @Override
- protected void onStop() {
- super.onStop();
- }
- //定位
- /**
- * 通过GPS获取定位信息
- */
- protected void getGPSLocation() {
- Location gps = LocationUtils.getGPSLocation(this);
- if (gps == null) {
- //设置定位监听,因为GPS定位,第一次进来可能获取不到,通过设置监听,可以在有效的时间范围内获取定位信息
- LocationUtils.addLocationListener(getApplicationContext(), LocationManager.GPS_PROVIDER, new LocationUtils.ILocationListener() {
- @Override
- public void onSuccessLocation(Location location) {
- if (location != null) {
- SPUtils.put("LOCATION",location.getLatitude() + "," + location.getLongitude());
- Log.e("BaseNfcActivity", "gps onSuccessLocation location: lat==" + location.getLatitude() + " lng==" + location.getLongitude());
- } else {
- Log.e("BaseNfcActivity", "gps location is null");
- }
- }
- });
- } else {
- Log.e("BaseNfcActivity", "gps location: lat==" + gps.getLatitude() + " lng==" + gps.getLongitude());
- }
- }
- /**
- * 通过网络等获取定位信息
- */
- protected void getNetworkLocation() {
- Location location = LocationUtils.getNetWorkLocation(this);
- if (location == null) {
- Log.e("BaseNfcActivity", "net location is null");
- } else {
- SPUtils.put("LOCATION",location.getLatitude() + "," + location.getLongitude());
- Log.e("BaseNfcActivity", "network location: lat==" + location.getLatitude() + " lng==" + location.getLongitude());
- }
- }
- /**
- * 采用最好的方式获取定位信息
- */
- protected void getBestLocation() {
- Criteria c = new Criteria();//Criteria类是设置定位的标准信息(系统会根据你的要求,匹配最适合你的定位供应商),一个定位的辅助信息的类
- c.setPowerRequirement(Criteria.POWER_LOW);//设置低耗电
- c.setAltitudeRequired(true);//设置需要海拔
- c.setBearingAccuracy(Criteria.ACCURACY_COARSE);//设置COARSE精度标准
- c.setAccuracy(Criteria.ACCURACY_LOW);//设置低精度
- //... Criteria 还有其他属性,就不一一介绍了
- Location location = LocationUtils.getBestLocation(this, c);
- if (location == null) {
- Log.e("BaseNfcActivity", " best location is null");
- } else {
- SPUtils.put("LOCATION",location.getLatitude() + "," + location.getLongitude());
- Log.e("BaseNfcActivity", "best location: lat==" + location.getLatitude() + " lng==" + location.getLongitude());
- }
- }
- }
|