发布地理定位时,App强制关闭

时间:2019-03-05 02:56:13

标签: android react-native geolocation

我在ACCESS_FINE_LOCATION上添加了AndroidManifest.xml权限 而且在调试模式下运行时我没问题。

但是当我使用react-native run-android --variant=release在发布模式下进行测试时,它可以成功构建,但是当我进入具有Geolocation.watchPosition的屏幕时,即使我已授予权限,应用也会崩溃。

这是我的脚本PosisiMarker.js

import React, {Component} from 'react';
import {Platform, Alert} from 'react-native';
import {Marker, AnimatedRegion} from 'react-native-maps'
import EventEmitter from 'react-native-eventemitter';
import {radius} from '../../../config'
import RNExitApp from 'react-native-exit-app'
import Permissions from 'react-native-permissions'
import Geolocation from 'react-native-geolocation-service';

export default class PosisiMarker extends Component {
  constructor (props) {
    super(props);
    this.state = {
      allowPresence: false,
      coordinate: new AnimatedRegion({
        latitude: 0,
        longitude: 0,
      }),
      alert: false,
    }
  }

  componentDidMount () {
    this.checkPresenceLocation();
  }

  componentWillUnmount () {
    console.log('clearing');
    Geolocation.clearWatch(this.watchId);
    Geolocation.stopObserving();
  }

  checkMocked (position) {
    if (position.mocked && !this.state.alert) {
      this.setState({alert: true});
      Alert.alert(
        'Peringatan!',
        'Anda dicurigai menggunakan aplikasi pemalsu lokasi (FakeGPS). Anda tidak diperkenankan melakukan absence! Aktifitas mencurigakan in akan kami simple!',
        [
          {text: 'Keluar', onPress: () => RNExitApp.exitApp()},
        ],
        {cancelable: false},
      );
    }
  }

  checkPresenceLocation = () => {
    console.log('watching position');
    this.watchId = Geolocation.watchPosition(
      (position) => {
        console.log('position', position);
        this.checkMocked(position)
        const { coordinate } = this.state;
        const newCoordinate = {
          latitude: position.coords.latitude,
          longitude: position.coords.longitude,
        };

        if (Platform.OS === 'android') {
          if (this.marker) {
            this.marker._component.animateMarkerToCoordinate(newCoordinate, 500);
          }
        } else {
          coordinate.timing(newCoordinate).start();
        }
        // this.setState({
        //   latitude: position.coords.latitude,
        //   longitude: position.coords.longitude,
        //   error: null,
        // });

        let latLng = {
          latitude: position.coords.latitude,
          longitude: position.coords.longitude,
        }
        let distance = geolib.getDistance(latLng, {
          latitude: this.props.location.lat,
          longitude: this.props.location.lang,
        })
        if (distance <= radius) {
          if (!this.state.allowPresence) {
            this.setState({allowPresence: true})
            this.emitPresence()
          }
        } else {
          if (this.state.allowPresence) {
            this.setState({allowPresence: false})
            this.emitPresence()
          }
        }
      },
      (error) => this.setState({ error: error.message }),
      { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000, distanceFilter: 10 })
    }

    emitPresence = () => {
      EventEmitter.emit('TOGGLE_PRESENCE', this.state.allowPresence);
    }

    emitCurrentPos = (position) => {
      EventEmitter.emit('CURRENT_POSITION', position);
    }

    render () {
      return (
        <Marker.Animated ref={(marker) => this.marker=marker} coordinate={this.state.coordinate} style={{zIndex: 3}}/>
      )
      // if (this.state.allowPresence) {
      //
      // } else {
      //   return null;
      // }
    }
  }

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="id.go.gorontaloprov.siransija">

  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  <uses-permission android:name="android.permission.CAMERA" />

  <application
    android:name=".MainApplication"
    android:label="@string/app_name"
    android:icon="@mipmap/ic_launcher"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:allowBackup="false"
    android:theme="@style/AppTheme">
    <activity
      android:name=".MainActivity"
      android:label="@string/app_name"
      android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
      android:windowSoftInputMode="adjustResize">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
    <meta-data
      android:name="com.google.android.geo.API_KEY"
      android:value="AIzaSyCzCN6dFqu09uIz1FTpzeOKgbY0nvi4Byk"/>
    </application>

  </manifest>

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题,只需在Proguard中禁用android/app/build.gradle

...

/**
* Run Proguard to shrink the Java bytecode in release builds.
*/
def enableProguardInReleaseBuilds = false