2012-03-18 145 views
1

我正在處理接近警報的應用程序。我可以添加接近警報,但無法刪除這些接近警報。我已經在手機和虛擬設備上嘗試了我的代碼,但無法刪除它們。Android刪除接近警報

這裏是我的代碼:

哪裏保存到數據庫和接近警報位置添加

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

這是其他活動的活動一點點長的代碼,但這是把我的問題正確的最短途徑。 thnx的幫助。

回答

3

解決了!問題是我發送給addProximityAlert()removeProximityAlert()上的intent對象的操作不匹配。現在我可以在糾正我的錯誤後刪除接近警報。

+0

你能顯示你的最終代碼,所以我可以看到你是如何解決你的問題嗎? – deucalion0 2013-03-10 16:12:18

+1

Intent anIntent = new Intent(REMOVE_PROXIMITY);意圖intent =新意圖(PROX_ALERT_INTENT);.我的最終代碼在那裏。問題是; REMOVE_PROXIMITY和PROX_ALERT_INTENT應該匹配。當我第一次嘗試時,他們是不同的。當它們相同時,您可以刪除接近警報。 – mkeremkeskin 2013-03-11 15:17:25

+0

歡呼聲回覆我,我花了幾個星期試圖解決這個問題! – deucalion0 2013-03-13 21:57:09

0

REMOVE_PROXIMITY的值是多少?