Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PendingEvent中进行位置更新后,在android中删除和添加地理围栏_Android_Android Pendingintent_Android Geofence - Fatal编程技术网

在PendingEvent中进行位置更新后,在android中删除和添加地理围栏

在PendingEvent中进行位置更新后,在android中删除和添加地理围栏,android,android-pendingintent,android-geofence,Android,Android Pendingintent,Android Geofence,我正在开发一个android应用程序,使用新的android GeoFenceAPI,我想注册100多个Geofences。 我在后台每1分钟更新一次我的位置,在IntentService类的OnHandleContent方法中,我计算当前位置和最近的100之间的距离,然后我需要监视这100个地理围栏 问题是,在获得当前位置后,我无法添加最近的100个地理围栏,所有这些都必须在应用程序关闭时才能工作 这是我的密码: package com.example.android.geofence; i

我正在开发一个android应用程序,使用新的android GeoFenceAPI,我想注册100多个Geofences。
我在后台每1分钟更新一次我的位置,在IntentService类的OnHandleContent方法中,我计算当前位置和最近的100之间的距离,然后我需要监视这100个地理围栏

问题是,在获得当前位置后,我无法添加最近的100个地理围栏,所有这些都必须在应用程序关闭时才能工作

这是我的密码:

package com.example.android.geofence;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.text.format.DateUtils;
import android.util.Log;
import android.widget.Toast;

import com.example.android.geofence.GeofenceUtils.REMOVE_TYPE;
import com.example.android.geofence.GeofenceUtils.REQUEST_TYPE;
import com.example.android.geofence.MainActivity.GeofenceSampleReceiver;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationClient.OnAddGeofencesResultListener;

public class LocationService extends IntentService implements OnAddGeofencesResultListener, GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener {

private String TAG = this.getClass().getSimpleName();
public static int notificationID = 0;

LocationClient geoLocationClient;

public String monitoredLounges = "";

public LocationService() {
    super("Fused Location");
}

public LocationService(String name) {
    super("Fused Location");
}

@Override
   public void onCreate() {
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (resultCode == ConnectionResult.SUCCESS) {
        geoLocationClient = new LocationClient(this, this, this);
        // when I'm using connect() in order to addgeofences my application stops unexpectedly
        geoLocationClient.connect();
    }
}

@Override
protected void onHandleIntent(Intent intent) {
    Location location = intent.getParcelableExtra(LocationClient.KEY_LOCATION_CHANGED);

    if(location != null){
        RegisterGeofences(location);

        Log.i(TAG, "onHandleIntent " + location.getLatitude() + "," + location.getLongitude());
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
        Builder noti = new NotificationCompat.Builder(this);
        noti.setContentTitle("Fused Location");
        noti.setContentText(notificationID + "," + location.getLatitude() + "," + location.getLongitude());
        noti.setSmallIcon(R.drawable.ic_launcher);
        notificationManager.notify(notificationID, noti.build());
        notificationID++;
    }
}

public void RegisterGeofences(Location _location) {
    MySQLiteHelper myDbHelper = new MySQLiteHelper(this);
    List<Lounge> closestRegions = myDbHelper.getClosestRegions(_location.getLatitude(), _location.getLongitude());
    ArrayList<Geofence> geofenceList = new ArrayList<Geofence>();

    int regionIndex = 0;
    for (Region cn : closestRegions){
        regionIndex++;
        if(regionIndex> 100) break;

            Geofence geofence = new Geofence.Builder()
                .setRequestId(cn.getTitle())
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
                .setCircularRegion(cn.getLatitude(), cn.getLongitude(), (float) 500.00)
                .setExpirationDuration(Geofence.NEVER_EXPIRE)
                .build();
            monitoredLounges += cn.getTitle() + "&";
            geofenceList.add(geofence);

    }
    PendingIntent geoFencePendingIntent = PendingIntent.getService(this, 0,
            new Intent(this, GeofenceIntentService.class), PendingIntent.FLAG_UPDATE_CURRENT);

    geoLocationClient.addGeofences(geofenceList, geoFencePendingIntent, this);      

}

@Override
public void onConnectionFailed(ConnectionResult arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onConnected(Bundle arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onDisconnected() {
    // TODO Auto-generated method stub

}

@Override
public void onAddGeofencesResult(int arg0, String[] arg1) {
    // TODO Auto-generated method stub

}

}
package com.example.android.geofiness;
导入java.util.ArrayList;
导入java.util.List;
导入android.app.Dialog;
导入android.app.IntentService;
导入android.app.NotificationManager;
导入android.app.pendingent;
导入android.content.Context;
导入android.content.Intent;
导入android.content.IntentFilter;
导入android.location.location;
导入android.os.Bundle;
导入android.support.v4.app.NotificationCompat;
导入android.support.v4.app.NotificationCompat.Builder;
导入android.text.format.DateUtils;
导入android.util.Log;
导入android.widget.Toast;
导入com.example.android.geofence.GeofenceUtils.REMOVE_TYPE;
导入com.example.android.geofinence.geofinecutils.REQUEST\u类型;
导入com.example.android.geofeence.MainActivity.geofeenceSampleReceiver;
导入com.google.android.gms.common.ConnectionResult;
导入com.google.android.gms.common.GooglePlayServicesClient;
导入com.google.android.gms.common.GooglePlayServicesUtil;
导入com.google.android.gms.location.geofense;
导入com.google.android.gms.location.LocationClient;
导入com.google.android.gms.location.LocationClient.OnAddGeofencesResultListener;
公共类LocationService扩展了IntentService在AddGeoFencesResultListener、GooglePlayServicesClient.ConnectionCallbacks、GooglePlayServicesClient.OnConnectionFailedListener上的实现{
私有字符串标记=this.getClass().getSimpleName();
公共静态int notificationID=0;
LocationClient地理位置客户端;
公共字符串monitoredLounges=“”;
公共场所服务{
超级(“融合位置”);
}
公共位置服务(字符串名称){
超级(“融合位置”);
}
@凌驾
public void onCreate(){
int-resultCode=GooglePlayServicesUtil.isgoogleplayservicesavaailable(this);
if(resultCode==ConnectionResult.SUCCESS){
geoLocationClient=新的LocationClient(这个,这个,这个);
//当我使用connect()添加地理围栏时,我的应用程序意外停止
geoLocationClient.connect();
}
}
@凌驾
受保护的手部内容无效(意图){
Location Location=intent.getParcelableExtra(LocationClient.KEY\u Location\u已更改);
如果(位置!=null){
登记围栏(位置);
Log.i(标记“onHandleIntent”+location.getLatitude()+”,“+location.getLatitude());
NotificationManager NotificationManager=(NotificationManager)getSystemService(通知服务);
Builder noti=新的通知compat.Builder(本);
noti.setContentTitle(“融合位置”);
noti.setContentText(notificationID+“,“+location.getLatitude()+”,“+location.getLatitude()”);
noti.setSmallIcon(R.drawable.ic_启动器);
notificationManager.notify(notificationID,noti.build());
notificationID++;
}
}
公共无效注册表地理围栏(位置_位置){
MySQLiteHelper myDbHelper=新的MySQLiteHelper(此);
List closestRegions=myDbHelper.getClosestRegions(_location.getLatitude(),_location.getLongitude());
ArrayList geofenceList=新建ArrayList();
int regionIndex=0;
适用于(cn地区:封闭街道地区){
regionIndex++;
如果(区域指数>100)中断;
Geofence Geofence=新建Geofence.Builder()
.setRequestId(cn.getTitle())
.setTransitionTypes(geofines.geofines_TRANSITION_ENTER | geofines.geofines_TRANSITION_EXIT)
.setCircularRegion(cn.getLatitude(),cn.getLatitude(),(float)500.00)
.setExpirationDuration(地理围栏永不过期)
.build();
monitoredLounges+=cn.getTitle()+“&”;
地理围栏列表。添加(地理围栏);
}
PendingEvent GeoFencePendingEvent=PendingEvent.getService(此,0,
新意图(this,geofenginententservice.class),pendingent.FLAG\u UPDATE\u CURRENT);
geoLocationClient.addGeofences(geofenceList,GeoFencePendingEvent,this);
}
@凌驾
连接失败的公共void(连接结果arg0){
//TODO自动生成的方法存根
}
@凌驾
未连接的公共空间(捆绑arg0){
//TODO自动生成的方法存根
}
@凌驾
公共空间已断开连接(){
//TODO自动生成的方法存根
}
@凌驾
onAddGeofencesResult上的公共void(int arg0,字符串[]arg1){
//TODO自动生成的方法存根
}
}

另外请注意,在添加RegisterGeofences方法之前,与updatelocations有关的所有内容都在后台正常工作,IntentService在作业完成后不会运行(即,它会在一段时间后销毁服务)。而是创建一个
服务
并运行它们。因为地理围栏仅在
geoLocationClient
已连接且可用时运行。还要断开服务
onDestroy()中的
geoLocationClient