- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在开发带有今年发布的地理围栏的 android 应用程序。
我的理解是当用户进入(或离开)围栏时它应该触发 onHandleIntent 方法但是我很难触发到 onHandleIntent 方法
我已经检查了 3 天来弄清楚,但我最终还是做不到。
所以我需要任何人的帮助。
这是我的代码,我真的希望有人能帮助我。
///// MainActivity
package com.example.geofence;
import java.util.ArrayList;
import java.util.List;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
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;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationStatusCodes;
public class MainActivity extends FragmentActivity implements
ConnectionCallbacks,
OnConnectionFailedListener,
OnAddGeofencesResultListener
{
private IntentFilter mIntentFilter;
private LocationClient locationClient;
private LocationRequest locatRequest;
private PendingIntent intent;
private List<Geofence> mGeoList;
private Context mContext;
private Geofence companyLocation;
private GeofenceSampleReceiver mBroadcastReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
locatRequest = null;
mGeoList = new ArrayList<Geofence>();
intent = null;
locationClient = new LocationClient(this,this,this);
mIntentFilter = new IntentFilter();
mIntentFilter.addAction("com.example.geofence.ACTION_GEOFENCES_ADDED");
mIntentFilter.addCategory("com.example.geofence.CATEGORY_LOCATION_SERVICES");
mBroadcastReceiver = new GeofenceSampleReceiver();
}
@Override
protected void onStart() {
companyLocation = new Geofence.Builder()
.setRequestId("1")
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
.setCircularRegion(
49.220531, -122.986772, (float)50)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.build();
mGeoList.add(companyLocation);
locationClient.connect();
super.onStart();
}
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
intent = getTransitionPendingIntent();
locatRequest = LocationRequest.create();
locatRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
locatRequest.setInterval(5000);
try{
addGeofence();
}catch(UnsupportedOperationException e){
Toast.makeText(this, "add_geofences_already_requested_error",
Toast.LENGTH_LONG).show();
}
// locationClient.requestLocationUpdates(locatRequest, intent);
}
public void addGeofence(){
locationClient.addGeofences(mGeoList,intent , this);
}
private PendingIntent getTransitionPendingIntent() {
// Create an explicit Intent
Intent localIntent = new Intent(this,
ReceiveTransitionsIntentService.class);
/*
* Return the PendingIntent
*/
return PendingIntent.getService(
this,
0,
localIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
@Override
protected void onStop() {
locationClient.disconnect();
super.onStop();
}
@Override
public void onAddGeofencesResult(int statusCode, String[] geofenceRequestIds) {
// TODO Auto-generated method stub
Intent broadcastIntent = new Intent();
if(LocationStatusCodes.SUCCESS == statusCode){
Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();
broadcastIntent.setAction("com.example.android.geofence.ACTION_GEOFENCES_ADDED")
.addCategory("com.example.android.geofence.CATEGORY_LOCATION_SERVICES")
.putExtra("com.example.android.geofence.EXTRA_GEOFENCE_STATUS","test");
}
else{
Toast.makeText(this, "AddGeoError", Toast.LENGTH_SHORT).show();
}
LocalBroadcastManager.getInstance(mContext).sendBroadcast(broadcastIntent);
}
@Override
public void onConnectionFailed(ConnectionResult arg0) { // TODO Auto-generated method stub
int code = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
switch(code){
case ConnectionResult.SERVICE_MISSING :{
Toast.makeText(this, "SERVICE_MISSING " + code + " ConnectionResult.SERVICE_MISSING " +ConnectionResult.SERVICE_MISSING, Toast.LENGTH_SHORT).show();
break;
}
case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:{
Toast.makeText(this, "SERVICE_VERSION_UPDATE_REQUIRED " + code + " ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED " + ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED, Toast.LENGTH_SHORT).show();
break;
}
default:{
Toast.makeText(this, "start " + code, Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, mIntentFilter);
//locationClient.connect();
super.onResume();
}
@Override
public void onDisconnected() {
// TODO Auto-generated method stub
locationClient = null;
}
/*
* Handle results returned to this Activity by other Activities started with
* startActivityForResult(). In particular, the method onConnectionFailed() in
* GeofenceRemover and GeofenceRequester may call startResolutionForResult() to
* start an Activity that handles Google Play services problems. The result of this
* call returns here, to onActivityResult.
* calls
*/
@Override
protected void onActivityResult(
int requestCode, int resultCode, Intent data) {
}
/**
* Define a Broadcast receiver that receives updates from connection listeners and
* the geofence transition service.
*/
public class GeofenceSampleReceiver extends BroadcastReceiver {
/*
* Define the required method for broadcast receivers
* This method is invoked when a broadcast Intent triggers the receiver
*/
@Override
public void onReceive(Context context, Intent intent) {
// Check the action code and determine what to do
String action = intent.getAction();
// Intent contains information about errors in adding or removing geofences
if (TextUtils.equals(action, "com.example.geofence.ACTION_GEOFENCES_ADDED")) {
handleGeofenceStatus(context, intent);
// Intent contains information about a geofence transition
} else if (TextUtils.equals(action, "com.example.geofence.ACTION_GEOFENCE_TRANSITION")) {
handleGeofenceTransition(context, intent);
// The Intent contained an invalid action
} else {
Toast.makeText(context,"error", Toast.LENGTH_LONG).show();
}
}
/**
* If you want to display a UI message about adding or removing geofences, put it here.
*
* @param context A Context for this component
* @param intent The received broadcast Intent
*/
private void handleGeofenceStatus(Context context, Intent intent) {
}
/**
* Report geofence transitions to the UI
*
* @param context A Context for this component
* @param intent The Intent containing the transition
*/
private void handleGeofenceTransition(Context context, Intent intent) {
/*
* If you want to change the UI when a transition occurs, put the code
* here. The current design of the app uses a notification to inform the
* user that a transition has occurred.
*/
}
/**
* Report addition or removal errors to the UI, using a Toast
*
* @param intent A broadcast Intent sent by ReceiveTransitionsIntentService
*/
private void handleGeofenceError(Context context, Intent intent) {
}
}
}
///// ReceiveTransitionsIntentService
package com.example.geofence;
import java.util.List;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.LocationClient;
public class ReceiveTransitionsIntentService extends IntentService {
/**
* Sets an identifier for the service
*/
private Context mContext;
public ReceiveTransitionsIntentService(Context c) {
super("ReceiveTransitionsIntentService");
mContext =c;
}
/**
* Handles incoming intents
*@param intent The Intent sent by Location Services. This
* Intent is provided
* to Location Services (inside a PendingIntent) when you call
* addGeofences()
*/
@Override
protected void onHandleIntent(Intent intent) {
// First check for errors
//Toast.makeText(mContext, "onHandleIntent", Toast.LENGTH_LONG).show();
if (LocationClient.hasError(intent)) {
// Get the error code with a static method
int errorCode = LocationClient.getErrorCode(intent);
// Log the error
Log.e("ReceiveTransitionsIntentService",
"Location Services error: " +
Integer.toString(errorCode));
/*
* You can also send the error code to an Activity or
* Fragment with a broadcast Intent
*/
/*
* If there's no error, get the transition type and the IDs
* of the geofence or geofences that triggered the transition
*/
} else {
int transitionType = LocationClient.getGeofenceTransition(intent);
if(transitionType == Geofence.GEOFENCE_TRANSITION_ENTER ||
transitionType == Geofence.GEOFENCE_TRANSITION_EXIT){
List<Geofence> geofences = LocationClient.getTriggeringGeofences(intent);
String[] geofenceIds = new String [geofences.size()];
for(int i = 0; i <geofences.size(); i++){
geofenceIds[i] = geofences.get(i).getRequestId();
}
String ids = "1";
String transition = ((transitionType == Geofence.GEOFENCE_TRANSITION_ENTER ) ?"you are in":"you are out");
Toast.makeText(mContext, transition, Toast.LENGTH_LONG).show();
sendNotification(transition,ids);
//FOR THE NOTIFICATION.
//NotificationManager mNotificationManager =
// (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
}
}
private void sendNotification(String transitionType, String ids) {
// 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);
// Adds 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);
// Set the notification contents
builder.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(
transitionType+ " geofence_transition_notification_title " +ids)
.setContentIntent(notificationPendingIntent);
// Get an instance of the Notification manager
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Issue the notification
mNotificationManager.notify(0, builder.build());
}
}
最佳答案
你的半径设置为 50,这意味着你需要从你所在的位置移动 50 米才能触发 handle 。您是否尝试过使用 2m 半径?
关于android - 谷歌播放地理围栏 onHandleIntent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16797950/
我想知道地理数据、几何数据和数据库系统(例如 Oracle 或 SqlSqerver)中表示空间数据的标准数据类型之间有什么区别? 对我来说看起来是一样的,但我知道肯定有区别。. 最佳答案 首先,地理
我正在寻找用于存储 map 的理想数据库或数据结构的建议。本质上, map 由“道路”组成,如道路、路径等。道路包含节点(具有纬度和经度坐标,有时还有高度。) 任何此类数据库或结构: 应该能够快速(毫
我最近将一些预装的国家和省份形状下载到 Sql Server 2008 中 http://sqlsamplegeo.codeplex.com/ 除了地理列,每个国家和省都有一个“ShapeArea”和
我对google Analytics(分析)地理如何工作(特别是对于出差用户)有疑问。例如,如果我是森尼韦尔市的用户,并且我打开了一个应用程序,则Google ID在用户ID级别的分析将说用户数= 1
我正在使用 Geo::IP 对 IP 地址执行位置查找。一切正常,直到我遇到一个不在地理 IP 查找数据库中的 IP 地址并且程序突然关闭并给出此错误 Can't call method "city"
通用(或设备无关)物理位置的结构是什么?我的猜测是它可能是一个有两个长字段的结构,或者类似的东西。 此外,给定一个目的地位置和两个候选位置,是否有一种简单的算法可以确定哪个候选位置最接近目的地?我并不
我正在使用 d3 geomaps 创建美国的地理 map 。当我将鼠标悬停在状态上时,我希望突出显示该状态。那可能吗?这是我当前的代码:
在geoviews guide for Bokeh ,它指出 Bokeh 仅支持墨卡托投影,但大多数示例投影使用 PlateCarree,然后输出看起来像墨卡托投影。 有人知道图形的投影和输出是怎么回
我有一个geodataframe'all_locations',其中有一个几何列和一个带有点名称的列。在 map 上绘制点工作正常,但我想用位置名称注释这些点。 ['位置'] ['几何'] BUITH
我正在尝试在 Canvas 中缩放 map 。 var projection = d3.geoMercator() projection.fitExtent([[margin.left, margin
我目前正在处理这段代码,一切正常,但是当我用鼠标悬停在一个国家/地区时,我只希望国家/地区名称出现在标签中,而不是相关值。我可以这样做吗?如果是,怎么办? 提前致谢! 这是javascript代码:
我正在尝试在 mac os 上使用 Geodjango,我使用 postgresql 并安装了 GEOS,但出现此错误: dlopen(/usr/local/lib/libgeos_c.dylib,
我正在研究 GeoViews,我想知道我们是否可以将 slider 作为 GeoViews 中等值线图的输入。 我在 gdf 中有另一个变量,它是年份。是否可以使用 slider 显示年度 Tot
我有一个 Geometry (看起来像 WKB) 我的 postgres/postgis 数据库中的对象,我已经转储到 BigQuery 来做一些有趣的事情。我看到它存储为 bytes .我如何构建一
我正在使用 DPM 进行 azure 在线备份,并且想要将冗余从 Geo 更改为 Local,但它显示为灰色。有办法改变吗? 我不想为此创建新的错误,然后我必须再次将所有内容从 DPM 重新上传到 A
我正在实现 Google 地理 map ,我想做的是,例如,如果我单击 US在 map 上,我会将国家/地区名称作为警报(例如,如果我单击美国,我必须将美国作为警报),我最终可以将其用作变量。当我尝试
我正在制作一张具有缩放和平移功能的世界地图。我在某些城市上画了圆圈,圆圈的半径由数据决定。当鼠标悬停在这些圆圈上时,将出现一个工具提示来显示数据。 代码结构为 //在此选择上调用缩放行为 - 让我们调
我需要一个函数来计算一对 WGS 84 之间的距离定位到高精度,我计划使用 boost geometry 中的 geographic 函数. boost geometry Design Rationa
我正在尝试缓冲数据集中半径为 100 公里的点。我正在使用函数 gBuffer来自包裹rgeos .这是我到目前为止所拥有的: head( sampledf ) # postalcode
我正在用 C# 开发一个应用程序,它将使用 SQL Server 2008 中的“地理”数据类型来使用和存储地理位置。我计划使用 Entity Framework ,但很快发现它不支持空间数据。有没有
我是一名优秀的程序员,十分优秀!