问题描述
我工作的应用程序,其中涉及感应警报。我可以添加接近警报,但我无法删除这些接近警报。我已经尽了code无论在手机和虚拟设备,但我不能设法删除它们。
i am working on app which deals with proximity alerts. I can add proximity alert but I can not remove these proximity alerts. I have tried my code both on phone and virtual device but i couldn't manage to remove them.
下面是我的code:
在哪里保存到数据库中添加和接近警报地点活动
the activity where locations saved to database and proximity alerts added
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;
}
和在那里我试图删除接近警报的其他活动。
and the other activity where i tried to remove proximity alerts
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);
}
}
这是一个有点长code,但是这是把我的问题正确的最短途径。日Thnx的帮助。
It's a little bit long code but that's the shortest way to put my question right. Thnx for the help.
推荐答案
解决了这个问题!现在的问题是,我是发送给目标对象的 addProximityAlert的行动()
和 removeProximityAlert()
不匹配。现在我可以在纠正我的错误删除接近警报。
Solved it! The problem is that the actions I was sending to intent objects on addProximityAlert()
and removeProximityAlert()
wasn't matching. Now I can remove proximity alerts after correcting my mistake.
这篇关于Android的删除接近警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!