发布时获取地理位置时应用程序强制关闭

Posted

技术标签:

【中文标题】发布时获取地理位置时应用程序强制关闭【英文标题】:App force close when get geolocation on release 【发布时间】:2019-03-05 02:56:13 【问题描述】:

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

但是当我在发布模式下使用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>

【问题讨论】:

你能发布错误信息吗?如果它在设备上,您可能需要从logcat 获取它。 【参考方案1】:

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

...

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

【讨论】:

以上是关于发布时获取地理位置时应用程序强制关闭的主要内容,如果未能解决你的问题,请参考以下文章

强制关闭 Android 位置权限

每次应用启动时如何强制phonegap询问地理位置?

未获取位置时关闭应用程序的对话框

在设备位置关闭时从 Google Play 服务获取最后一个已知位置

定位服务 GPS 强制关闭

如果设备屏幕关闭,如何获取位置信息?