Android Geofencing API 在后台可以正常工作,但不能用于 GPS

Posted

技术标签:

【中文标题】Android Geofencing API 在后台可以正常工作,但不能用于 GPS【英文标题】:Android Geofencing API working fine for wifi in the background but not for GPS 【发布时间】:2018-10-20 07:10:17 【问题描述】:

我正在使用 GeofencingClient Api。当wifi打开并触发地理围栏转换时,我的应用程序工作正常。 但在 GPS 的情况下,它不起作用。 我希望该服务在后台继续正常工作,并在进入和离开地理围栏设置半径时不断触发地理围栏转换。

一些示例代码

我调用 GeofencingClient 的代码

  public static void addGeofences() 
    mGeofenceList = new ArrayList<>();
    populateGeofenceList();

    mGeofencingClient = LocationServices.getGeofencingClient(MyApplicationClass.getAppContext());


    mGeofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent())
            .addOnCompleteListener(new OnCompleteListener<Void>() 
                @Override
                public void onComplete(@NonNull Task<Void> task) 
                    Log.d("Tag", "Test");
                
            );

这个方法被称为addGeofence的代码

public class GpsLocationReceiver extends BroadcastReceiver implements LocationListener 

    LocationManager locationManager ;
    boolean GpsStatus ;
    public Context mContext;
    @Override
    public void onReceive(Context context, Intent intent) 

        MainActivity obj = new MainActivity();

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

        GpsStatus = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        if(GpsStatus == true)
        

            Toast.makeText(context, "GPS Enabled", Toast.LENGTH_LONG).show();
            obj. populateGeofenceList();
            obj.mGeofencingClient = LocationServices.getGeofencingClient(context);
            obj.addGeofences();


        else 

            Toast.makeText(context, "Disabled GPS", Toast.LENGTH_LONG).show();

        
    

    @Override
    public void onLocationChanged(Location location) 

    

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) 

    

    @Override
    public void onProviderEnabled(String s) 

    

    @Override
    public void onProviderDisabled(String s) 

    

以及发生这种转换的代码

@Override
protected void onHandleIntent(Intent intent) 
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);

     Context context;

    if (geofencingEvent.hasError()) 
        String errorMessage = GeofenceErrorMessages.getErrorString(this,
                geofencingEvent.getErrorCode());
        Log.e(TAG, errorMessage);
        return;
    

    // Get the transition type.
    int geofenceTransition = geofencingEvent.getGeofenceTransition();


    //===============================

    if (geofenceTransition == 1) 
        handler.post(new Runnable() 
            @Override
            public void run() 

                attendenInRecord();

            
        );
        Log.i(TAG, "Entered");

    

    else 

        handler.post(new Runnable() 
            @Override
            public void run() 

                attendenOutRecord();

            
        );
        Log.e(TAG, String.valueOf(geofenceTransition));
    
    //===================================

    // Test that the reported transition was of interest.
    if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
            geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) 


        // Get the geofences that were triggered. A single event can trigger multiple geofences.
        List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();

        // Get the transition details as a String.
        String geofenceTransitionDetails = getGeofenceTransitionDetails(geofenceTransition,
                triggeringGeofences);

        // Send notification and log the transition details.
        sendNotification(geofenceTransitionDetails);


        Log.i(TAG, geofenceTransitionDetails);

        if(geofenceTransition==Geofence.GEOFENCE_TRANSITION_ENTER)

            Log.i(TAG, geofenceTransitionDetails);
        

     else 
        // Log the error.
        Log.e(TAG, getString(R.string.geofence_transition_invalid_type, geofenceTransition));

    


/**
 * Gets transition details and returns them as a formatted string.
 *
 * @param geofenceTransition    The ID of the geofence transition.
 * @param triggeringGeofences   The geofence(s) triggered.
 * @return                      The transition details formatted as String.
 */
private String getGeofenceTransitionDetails(
        int geofenceTransition,
        List<Geofence> triggeringGeofences) 

    String geofenceTransitionString = getTransitionString(geofenceTransition);

    // Get the Ids of each geofence that was triggered.
    ArrayList<String> triggeringGeofencesIdsList = new ArrayList<>();
    for (Geofence geofence : triggeringGeofences) 
        triggeringGeofencesIdsList.add(geofence.getRequestId());
    
    String triggeringGeofencesIdsString = TextUtils.join(", ",  triggeringGeofencesIdsList);

    return geofenceTransitionString + ": " + triggeringGeofencesIdsString;


/**
 * Posts a notification in the notification bar when a transition is detected.
 * If the user clicks the notification, control goes to the MainActivity.
 */
private void sendNotification(String notificationDetails) 
    // Get an instance of the Notification manager
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // android O requires a Notification Channel.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 
        CharSequence name = getString(R.string.app_name);
        // Create the channel for the notification
        NotificationChannel mChannel =
                new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_DEFAULT);

        // Set the Notification Channel for the Notification Manager.
        mNotificationManager.createNotificationChannel(mChannel);
    

    // Create an explicit content Intent that starts the main Activity.
    Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);

    // Construct a task stack.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

    // Add the main Activity to the task stack as the parent.
    stackBuilder.addParentStack(MainActivity.class);

    // Push the content Intent onto the stack.
    stackBuilder.addNextIntent(notificationIntent);

    // Get a PendingIntent containing the entire back stack.
    PendingIntent notificationPendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    // Get a notification builder that's compatible with platform versions >= 4
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    // Define the notification settings.
    builder.setSmallIcon(R.drawable.point)
            // In a real app, you may want to use a library like Volley
            // to decode the Bitmap.
            .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                    R.drawable.point))
            .setColor(Color.RED)
            .setContentTitle(notificationDetails)
            .setContentText(getString(R.string.geofence_transition_notification_text))
            .setContentIntent(notificationPendingIntent);

    // Set the Channel ID for Android O.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 
        builder.setChannelId(CHANNEL_ID); // Channel ID
    

    // Dismiss notification once the user touches it.
    builder.setAutoCancel(true);

    // Issue the notification
    mNotificationManager.notify(0, builder.build());


/**
 * Maps geofence transition types to their human-readable equivalents.
 *
 * @param transitionType    A transition type constant defined in Geofence
 * @return                  A String indicating the type of transition
 */
private String getTransitionString(int transitionType) 
    switch (transitionType) 

        case Geofence.GEOFENCE_TRANSITION_ENTER:


            return getString(R.string.geofence_transition_entered);

        case Geofence.GEOFENCE_TRANSITION_EXIT:

            return getString(R.string.geofence_transition_exited);
        default:
            return getString(R.string.unknown_geofence_transition);
    

【问题讨论】:

您是否要求良好的位置? ACCESS_COARSE_LOCATION 【参考方案1】:

尝试使用 GPS 请求精确位置。我发现当使用粗略定位时,gps 围栏不会被触发,但使用 wifi 他们会知道你在某个范围内。

我知道明年谷歌发布 wifiRTT 时,融合位置将极大地帮助这一点达到 1 米。无论哪种方式,您都需要请求包含粗略位置的精细位置。

【讨论】:

感谢 reidisaki 。我已经尝试过fine_location,但在后台服务中,没有wifi它仍然无法工作。我也希望它与 gps 一起使用。 在应用的位置设置中是否设置为高精度?不平衡,还是省电?这是在 android OS 级别的位置设置中 当我在前台时,两个应用程序都运行良好,但在后台 GPS 出现问题。 reidisaki 我将我的设置设置为高精度当我在活动的 onCreate 中调用 addGeofence 方法时,两者都运行良好但是当我在服务中调用它时,它在 GPS 的情况下会出现问题跨度> 只是想确认您正在尝试在服务中添加地理围栏?

以上是关于Android Geofencing API 在后台可以正常工作,但不能用于 GPS的主要内容,如果未能解决你的问题,请参考以下文章

新的 Android 地理围栏 API - 示例代码在位置时不会发出警报/通知

在 Android Studio 后端设置 API 级别

除了 Android API 之外,还有其他用于计算地理围栏违规的 API

带有模拟位置提供程序的 Android 地理围栏

如何在后端 API 中验证 AzureAD accessToken

Android:如何在用户关闭位置设置时获取地理围栏事件?