Android:当应用处于前台时,地理围栏工作正常,但在后台时不起作用。
创始人
2024-10-13 11:00:51
0

要解决这个问题,需要确保在应用处于后台时,地理围栏仍然能够正常工作。以下是一种可能的解决方法:

  1. 在AndroidManifest.xml文件中,确保已声明必要的权限和服务:


    

    
        
    


  1. 创建一个名为GeofenceTransitionsIntentService的服务类,该类将处理地理围栏的转换事件:
public class GeofenceTransitionsIntentService extends IntentService {

    private static final String TAG = "GeofenceTransitionsIS";

    public GeofenceTransitionsIntentService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
        if (geofencingEvent.hasError()) {
            Log.e(TAG, "GeofencingEvent error: " + geofencingEvent.getErrorCode());
            return;
        }

        int geofenceTransition = geofencingEvent.getGeofenceTransition();
        if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
                geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
            List triggeringGeofences = geofencingEvent.getTriggeringGeofences();
            // 处理地理围栏转换事件
            // ...
        }
    }
}
  1. 在需要使用地理围栏的活动或服务中,注册和管理地理围栏:
public class MainActivity extends AppCompatActivity implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        ResultCallback {

    private GoogleApiClient googleApiClient;
    private PendingIntent geofencePendingIntent;
    private GeofencingRequest geofencingRequest;
    private Geofence geofence;

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

        // 初始化GoogleApiClient
        googleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();

        // 创建Geofence对象
        geofence = new Geofence.Builder()
                .setRequestId("myGeofence")
                .setCircularRegion(37.422, -122.084, 100) // 设置地理围栏的坐标和半径
                .setExpirationDuration(Geofence.NEVER_EXPIRE)
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
                .build();

        // 创建GeofencingRequest对象
        geofencingRequest = new GeofencingRequest.Builder()
                .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
                .addGeofence(geofence)
                .build();
    }

    @Override
    protected void onStart() {
        super.onStart();
        googleApiClient.connect();
    }

    @Override
    protected void onStop() {
        super.onStop();
        googleApiClient.disconnect();
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        // 请求地理围栏的权限
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    1);
        } else {
            // 添加地理围栏
            addGeofence();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1 && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // 添加地理围栏
            addGeofence();
        }
    }

    private void addGeofence() {
        // 创建PendingIntent
        Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
        geofencePendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        // 请求地理围栏更新
        if (

相关内容

热门资讯

此事引发广泛关注!皮球茶馆辅助... 此事引发广泛关注!皮球茶馆辅助器,微乐小程序自建房辅助下载,手段教程(讲解有挂)-哔哩哔哩1、实时微...
透视好牌!德扑圈有透视吗,德普... 透视好牌!德扑圈有透视吗,德普辅助器怎么用(透视)原来真的有挂(有挂神器)-哔哩哔哩1、德普辅助器怎...
此事引发网友热议!河洛杠次插件... 您好,新珊瑚大厅辅助这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家...
透视模拟器!德普之星的辅助工具... 透视模拟器!德普之星的辅助工具介绍,德普之星透视辅助软件激活码(透视)竟然存在有挂(有挂教学)-哔哩...
据玩家消息!雀神麻雀科技公司,... 据玩家消息!雀神麻雀科技公司,德普之星透视辅助软件,法子教程(有挂秘诀)-哔哩哔哩1、德普之星透视辅...
透视免费!德普之星的辅助工具介... 透视免费!德普之星的辅助工具介绍,德扑圈有透视吗(透视)一贯是有挂(有挂详细)-哔哩哔哩一、德普之星...
近日!决战十三水有挂脚本吗,a... 近日!决战十三水有挂脚本吗,aapoker发牌逻辑,指引教程(有挂规律)-哔哩哔哩1、下载好决战十三...
透视软件!德普之星透视辅助软件... 透视软件!德普之星透视辅助软件激活码,德普之星私人局透视(透视)好像是真的挂(有挂功能)-哔哩哔哩暗...
无独有偶!雀神麻雀充运势有用吗... 无独有偶!雀神麻雀充运势有用吗,极速官方游戏暗堡辅助器,学习教程(有挂秘籍)-哔哩哔哩1、极速官方游...
透视私人局!德普之星怎么开辅助... 透视私人局!德普之星怎么开辅助,德普之星透视软件免费入口官网(透视)都是是有挂(发现有挂)-哔哩哔哩...