public void StartGeoFence() { try { jcu = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); jou = jcu.GetStatic <AndroidJavaObject>("currentActivity"); mGeoFenceClient = new AndroidJavaObject("com.amap.api.fence.GeoFenceClient", jou); centerPoint = new AndroidJavaObject("com.amap.api.location.DPoint"); geofence = new AndroidJavaObject("com.amap.api.fence.GeoFence"); Intent = new AndroidJavaClass("android.content.Intent"); Bundle = new AndroidJavaClass("android.os.Bundle"); ConnectivityManager = new AndroidJavaClass("android.net.ConnectivityManager"); geofenceLis = new GFListener(); geofenceLis.GeoFenceCreateFinished += onGeoFenceCreateFinished; mGeoFenceClient.Call("setGeoFenceListener", geofenceLis); mGeoFenceClient.Call("setActivateAction", mGeoFenceClient.GetStatic <int>("GEOFENCE_IN") | mGeoFenceClient.GetStatic <int>("GEOFENCE_OUT") | mGeoFenceClient.GetStatic <int>("GEOFENCE_STAYED")); centerPoint.Call("setLatitude", 25.962D); centerPoint.Call("setLongitude", 115.405D); mGeoFenceClient.Call("addGeoFence", "丽景家园", "住宅", centerPoint, 1000F, 10, "打卡"); mGeoFenceClient.Call <AndroidJavaObject>("createPendingIntent", GEOFENCE_BROADCAST_ACTION); BroadcastReceiver = new AndroidJavaObject("com.unity3d.player.UABroadcastReceiver"); filter = new AndroidJavaObject("android.content.IntentFilter", ConnectivityManager.GetStatic <string>("CONNECTIVITY_ACTION")); filter.Call("addAction", GEOFENCE_BROADCAST_ACTION); jou.Call <AndroidJavaObject>("registerReceiver", BroadcastReceiver, filter); unlockBroadcast = new UnityBroadcastProxy(); BroadcastReceiver.CallStatic("setReceiver", unlockBroadcast); } catch (Exception ex) { tvReult.text = ex.Message; } }
public void StartGeoFence() { try { jcu = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); jou = jcu.GetStatic <AndroidJavaObject>("currentActivity"); mGeoFenceClient = new AndroidJavaObject("com.amap.api.fence.GeoFenceClient", jou); centerPoint = new AndroidJavaObject("com.amap.api.location.DPoint"); geofence = new AndroidJavaObject("com.amap.api.fence.GeoFence"); Intent = new AndroidJavaClass("android.content.Intent"); Bundle = new AndroidJavaClass("android.os.Bundle"); ConnectivityManager = new AndroidJavaClass("android.net.ConnectivityManager"); geofenceLis = new GFListener(); geofenceLis.GeoFenceCreateFinished += onGeoFenceCreateFinished; mGeoFenceClient.Call("setGeoFenceListener", geofenceLis); mGeoFenceClient.Call("setActivateAction", mGeoFenceClient.GetStatic <int>("GEOFENCE_IN") | mGeoFenceClient.GetStatic <int>("GEOFENCE_OUT") | mGeoFenceClient.GetStatic <int>("GEOFENCE_STAYED")); centerPoint.Call("setLatitude", 28.757D); centerPoint.Call("setLongitude", 115.837D); //多边形围栏 /** * Java : * * List<DPoint> points = new ArrayList<DPoint>(); * * points.add(new DPoint(39.992702, 116.470470)); * points.add(new DPoint(39.994387, 116.472498)); * points.add(new DPoint(39.994478, 116.474161)); * points.add(new DPoint(39.993163, 116.474504)); * points.add(new DPoint(39.991363, 116.472605)); */ AndroidJavaObject pointsList = new AndroidJavaObject("com.unity3d.mylibrary.PointsList"); AndroidJavaObject points = pointsList.Call <AndroidJavaObject>("createList"); points.Call <bool>("add", new AndroidJavaObject("com.amap.api.location.DPoint", 28.757324, 115.837392)); points.Call <bool>("add", new AndroidJavaObject("com.amap.api.location.DPoint", 28.757212, 115.837820)); points.Call <bool>("add", new AndroidJavaObject("com.amap.api.location.DPoint", 28.757389, 115.837847)); points.Call <bool>("add", new AndroidJavaObject("com.amap.api.location.DPoint", 28.757518, 115.837362)); mGeoFenceClient.Call("addGeoFence", points, "打卡"); //mGeoFenceClient.Call("addGeoFence", "稻香楼", "餐饮服务", centerPoint, 1000F, 10, "打卡"); mGeoFenceClient.Call <AndroidJavaObject>("createPendingIntent", GEOFENCE_BROADCAST_ACTION); BroadcastReceiver = new AndroidJavaObject("com.unity3d.mylibrary.UABroadcastReceiver"); filter = new AndroidJavaObject("android.content.IntentFilter", ConnectivityManager.GetStatic <string>("CONNECTIVITY_ACTION")); filter.Call("addAction", GEOFENCE_BROADCAST_ACTION); jou.Call <AndroidJavaObject>("registerReceiver", BroadcastReceiver, filter); unlockBroadcast = new UnityBroadcastProxy(); //unlockBroadcast.Invoke("onReceive", javaArgs); BroadcastReceiver.CallStatic("setReceiver", unlockBroadcast); } catch (Exception ex) { tvReult.text = ex.Message; } }