我发疯了,没有弄错我在做什么,现在我向StackOverflow寻求帮助。
我正在尝试添加具有接近警报的位置,并且当接近警报触发时,我会收到通知。我四处走动时正在获取位置更新,并且我的应用程序不断在距离我的接近警报居中的位置打印我的距离。
问题在于,接近警报永远不会触发。尝试此操作时,我一直在使用具有实际GPS的物理设备。
用于设置接近警报的代码。显示了最后的祝酒词,因此我知道这段代码可以运行。
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Intent intent = new Intent(PROX_ALERT_INTENT);
intent.putExtra(PROXIMITY_ITEM_ID, item.getID());
PendingIntent proximityIntent = PendingIntent.getBroadcast(this, item.getID(), intent, 0);
locationManager.addProximityAlert(
item.getPlace().getLocation().latitude, // the latitude of the central point of the alert region
item.getPlace().getLocation().longitude, // the longitude of the central point of the alert region
200, // the radius of the central point of the alert region, in meters
-1, // time for this proximity alert, in milliseconds, or -1 to indicate no expiration
proximityIntent // will be used to generate an Intent to fire when entry to or exit from the alert region is detected
);
IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
this.registerReceiver(new ProximityAlertBroadcastReceiver(), filter);
Toast.makeText(this, "Added new proximity alert event...", Toast.LENGTH_SHORT).show();
然后我有了广播接收器的代码。此代码永远不会运行,因此我永远也不会收到吐司或通知。
public class ProximityAlertBroadcastReceiver extends BroadcastReceiver {
private void createNotification(Context context, ToDoItem todoItem) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.todomaps_icon)
.setContentTitle(todoItem.getTask())
.setContentText("You are " + todoItem.getDistanceString() + " to todo: " + todoItem.getTask());
// Sets an ID for the notification
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "ON RECEIVE!", Toast.LENGTH_SHORT).show(); //TODO: TEMP
int itemID = intent.getIntExtra(AddItemActivity.PROXIMITY_ITEM_ID, -1);
DBHandler handler = new DBHandler(context);
Item item = handler.getItem(itemID);
createNotification(context, item);
}
}
我不知道这是否有区别,但是应用程序中用于查看到邻近警报中心距离的位置管理器实例是用于创建邻近警报的另一个实例。因此,创建接近警报的位置管理器实例不请求位置更新,但我想这无关紧要。
这是我使用的权限:
<permission
android:name="com.flipsoft.todomaps.permission.MAPS_RECEIVE"
android:protectionLevel="signature"></permission>
<uses-permission android:name="com.flipsoft.todomaps.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
最佳答案
您是否在清单上注册了接收器?
<receiver android:name="com.example.ProximityAlertBroadcastReceiver" >
<intent-filter>
<action android:name="<your intent>" />
</intent-filter>
</receiver>
参见Notify users when they reach a location