Android 移除接近警报

标签 android alert proximity

我正在开发处理接近警报的应用程序。我可以添加接近警报,但无法删除这些接近警报。我已经在手机和虚拟设备上尝试了我的代码,但无法删除它们。

这是我的代码:

将位置保存到数据库并添加接近警报的 Activity

    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;
            }

以及我试图删除接近警报的其他 Activity

    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 对象的操作不匹配。现在我可以在纠正错误后删除接近警报。

关于Android 移除接近警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9761033/

相关文章:

android - 如何在 API 1.1 中获取多个推特用户的提要

javascript - 禁用 javascript 中的内置函数(警报)

swift - 检测物体靠近墙壁

java - 线程等待一组异步任务完成

android BottomAppBar 有额外的左侧填充

javascript - jquery获取输入框的值

JavaScript - iPad 应用程序中的警报消息

ios - CoreBluetooth 上的最大外围设备数量?

javascript - 在 HTML5/JavaScript 游戏中使用图 block 创建平滑的光照过渡

java - Android SQLite CursorIndexOutOfBounds