Android Proximity警报 - 不触发
我疯了没有得到我做错了什么,我现在转向StackOverflow寻求帮助。Android Proximity警报 - 不触发
我想添加接近警报位置,当接近警报触发我收到通知。我在移动时获得位置更新,并且我持续地将应用程序的距离打印到我的接近警报居中位置。
问题是proxmity警报从不触发。尝试此操作时,我一直在使用带有实际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
以上是 Android Proximity警报 - 不触发 的全部内容, 来源链接: utcz.com/qa/266693.html