Android 安卓删除接近警报
我正在开发一款处理近距离警报的应用程序。我可以添加接近警报,但无法删除这些接近警报。我在手机和虚拟设备上都尝试了我的代码,但无法删除它们 这是我的密码: 将位置保存到数据库并添加接近警报的活动Android 安卓删除接近警报,android,alert,proximity,Android,Alert,Proximity,我正在开发一款处理近距离警报的应用程序。我可以添加接近警报,但无法删除这些接近警报。我在手机和虚拟设备上都尝试了我的代码,但无法删除它们 这是我的密码: 将位置保存到数据库并添加接近警报的活动 saveButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { String title
saveButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String title = titleText.getText().toString();
if(title.matches("")){
Toast.makeText(CurrentLocationActivity.this,"Please provide a Title",Toast.LENGTH_SHORT).show();
}
else{
saveLocation(saveProximityAlertPoint(title));
setResult(RESULT_OK);
Toast.makeText(CurrentLocationActivity.this,"Location Saved",Toast.LENGTH_SHORT).show();
}
}
});
private void saveLocation(int unique_location_id) {
if (location == null) {
Toast.makeText(this, "No last known location",Toast.LENGTH_LONG).show();
return ;
}
else{
String title = titleText.getText().toString();
String type = typeSpinner.getSelectedItem().toString();
range = (int) (radius + POINT_RADIUS);
int unique_id = unique_location_id;
Double latitude = location.getLatitude();
Double longitude = location.getLongitude();
location_id = mDbHelper.createLocation(title, type, range, unique_id, latitude, longitude);
titleText.setText("");
radiusBar.setProgress(0);
}
}
private int saveProximityAlertPoint(String title) {
if (location == null) {
Toast.makeText(this, "No last known location.",Toast.LENGTH_LONG).show();
return 0;
}
else{
Double latitudeProxAlert = location.getLatitude();
Double longitudeProxAlert = location.getLongitude();
int unique_location_id = addProximityAlert(latitudeProxAlert, longitudeProxAlert, title);
return unique_location_id;
}
}
private int addProximityAlert(double latitude, double longitude, String title) {
Intent intent = new Intent(PROX_ALERT_INTENT);
intent.putExtra(LOCAION_TITLE, title);
PendingIntent proximityIntent = PendingIntent.getBroadcast(this, ++pending_intent_unique_id , intent, 0);
locationManager.addProximityAlert(latitude, longitude, range, PROX_ALERT_EXPIRATION, proximityIntent );
IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
registerReceiver(new ProximityIntentReceiver(), filter);
Toast.makeText(this, "Proximity has been added for " + title + " with unique_id " + pending_intent_unique_id,
Toast.LENGTH_LONG).show();
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("UNIQUEID", pending_intent_unique_id);
editor.commit();
return pending_intent_unique_id;
}
还有另一项活动,我试图删除接近警报
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_delete:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
Cursor proxRemoveCursor = mDbHelper.fetchLocation(info.id);
removeProximityAlert(proxRemoveCursor.getInt(proxRemoveCursor.getColumnIndexOrThrow(DBAdapter.KEY_UNIQUE_LOCATION_ID)));
mDbHelper.deleteLocation(info.id);
proxRemoveCursor.close();
return true;
}
private void removeProximityAlert(int unique_id) {
String context = Context.LOCATION_SERVICE;
LocationManager locationManager = (LocationManager) getSystemService(context);
Intent anIntent = new Intent(REMOVE_PROXIMITY);
PendingIntent operation =
PendingIntent.getBroadcast(getApplicationContext(), unique_id , anIntent, 0);
locationManager.removeProximityAlert(operation);
}
}
代码有点长,但这是纠正我问题的最短方法。谢谢你的帮助。解决了它!问题是我在
addProximityAlert()
和removeProximityAlert()
上发送给intent对象的操作不匹配。现在,我可以在纠正错误后删除邻近警报 删除邻近性的值是多少?你能展示你的最终代码,让我看看你是如何解决问题的吗;意向意向=新意向(PROX_ALERT_Intent);。我的最终代码在那里。问题是;移除接近和接近警报的意图应匹配。当我第一次尝试时,它们是不同的。当它们相同时,您可以删除接近警报。为回复我干杯,我花了数周时间试图修复此问题!我仍然不知道你所说的匹配是什么意思,我已经试了一整天了,请你编辑你的问题,以显示你的最终代码,因为我已经尝试了一切。非常感谢。我的最终代码在那里,很抱歉我现在很忙,无法查看您的代码。添加接近警报时,此PendingEvent ProximityEvent=PendingEvent.getBroadcast(getApplicationContext(),uniqueId,intent,0);locationManager.addProximityAlert(纬度、经度、范围、PROX\u警报到期、ProximityContent);您正在给出一个待定的意图。删除此挂起的意图时,应与已创建的意图相同。