地理围栏应用程序不断停止

时间:2019-02-24 12:58:44

标签: android android-geofence

我正在尝试按照this教程中的步骤在Android Studio中实现一个简单的地理围栏示例。我想要一个当用户输入/离开一些预定义区域时仅显示一些文本的应用程序(您可以在清单文件中看到以下代码)。但是,每次在模拟器上运行此应用程序时,都会收到应用程序名称不断停止的错误。我也在发布Logcat输出。

错误的原因是什么,我该如何解决?

谢谢。

MainActivity类

    public class MainActivity extends Activity {
        private static final long MINIMUM_DISTANCECHANGE_FOR_UPDATE = 1; // in Meters
        private static final long MINIMUM_TIME_BETWEEN_UPDATE = 1000; // in Milliseconds
        private static final long POINT_RADIUS = 100; // in Meters
        private static final long PROX_ALERT_EXPIRATION = -1; // It will never expire
        private static final String PROX_ALERT_INTENT = "com.example.user.proximityalert2.ProximityAlert";
        private LocationManager locationManager;

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

            if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) ==
                    PackageManager.PERMISSION_GRANTED &&
                    ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) ==
                            PackageManager.PERMISSION_GRANTED) {
addProximityAlert();

            } else {
                Toast.makeText(this, "Permission not granted", Toast.LENGTH_LONG).show();
            ActivityCompat.requestPermissions(this,new String[] { Manifest.permission.ACCESS_FINE_LOCATION}, 1);
            addProximityAlert();
            }

            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            //addProximityAlert();

        }

        @SuppressLint("MissingPermission")
        private void addProximityAlert() {

            double latitude = 40.4893;
            double longitude = 50.2260;

            Intent intent = new Intent(PROX_ALERT_INTENT);
            PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
            locationManager.addProximityAlert(
                    latitude, // the latitude of the central point of the alert region
                    longitude, // the longitude of the central point of the alert region
                    POINT_RADIUS, // the radius of the central point of the alert region, in meters
                    PROX_ALERT_EXPIRATION, // time for this proximity alert, in milliseconds, or -1 to indicate no                           expiration
                    proximityIntent // will be used to generate an Intent to fire when entry to or exit from the alert region is detected
            );

            IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
            registerReceiver(new ProximityIntentReceiver(), filter);
            Toast.makeText(getApplicationContext(),"Alert Added",Toast.LENGTH_SHORT).show();
        }
    }

ProximityIntentReceiver类

public class ProximityIntentReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String key = LocationManager.KEY_PROXIMITY_ENTERING;
        Boolean entering = intent.getBooleanExtra(key, false);
        if (entering) {
            //Log.d(getClass().getSimpleName(), "entering");
            Toast.makeText(context, "You are entering XXX", Toast.LENGTH_SHORT).show();

        }else {
            //Log.d(getClass().getSimpleName(), "exiting");
            Toast.makeText(context, "You are exiting XXX", Toast.LENGTH_SHORT).show();
        }
    }
}

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.user.proximityalert2">
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"
        tools:ignore="MockLocation,ProtectedPermissions" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.VIBRATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".ProximityIntentReceiver"/>
    </application>

</manifest>

Logcat输出

2019-02-24 16:38:38.740 8270-8270/com.example.user.proximityalert2 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.user.proximityalert2, PID: 8270
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.proximityalert2/com.example.user.proximityalert2.MainActivity}: java.lang.SecurityException: Geofence usage requires ACCESS_FINE_LOCATION permission
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.SecurityException: Geofence usage requires ACCESS_FINE_LOCATION permission
        at android.os.Parcel.createException(Parcel.java:1942)
        at android.os.Parcel.readException(Parcel.java:1910)
        at android.os.Parcel.readException(Parcel.java:1860)
        at android.location.ILocationManager$Stub$Proxy.requestGeofence(ILocationManager.java:814)
        at android.location.LocationManager.addProximityAlert(LocationManager.java:1114)
        at com.example.user.proximityalert2.MainActivity.addProximityAlert(MainActivity.java:46)
        at com.example.user.proximityalert2.MainActivity.onCreate(MainActivity.java:32)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
     Caused by: android.os.RemoteException: Remote stack trace:
        at com.android.server.LocationManagerService.checkResolutionLevelIsSufficientForGeofenceUse(LocationManagerService.java:1441)
        at com.android.server.LocationManagerService.requestGeofence(LocationManagerService.java:2343)
        at android.location.ILocationManager$Stub.onTransact(ILocationManager.java:120)
        at android.os.Binder.execTransact(Binder.java:731)

0 个答案:

没有答案