2012-05-02 2 views
1

수신기 :근접 경보 발사는 두 번?

public class ProximityAlert extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     int id = intent.getIntExtra("id", -1); 
     String title = intent.getStringExtra("Title"); 

     Intent showDialog = new Intent(context, ShowMapDialog.class); 
     showDialog.putExtra("id", id); 
     showDialog.putExtra("Title", title); 
     context.startActivity(showDialog); 
    } 

} 

ShowMapDialog.java :

public class ShowMapDialog extends Activity { 

PowerManager.WakeLock wakeLock; 
AlertDialog alertbox; 

@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 

    Bundle extras = getIntent().getExtras(); 

    Prefs sp = new Prefs(); 

    int lastplaceid = sp.getLastPlaceID(getApplicationContext()); 
    boolean nopopup = sp.getNoPopup(getApplicationContext()); 

    final int id = extras.getInt("id"); 
    String Title = extras.getString("Title"); 

    Log.d("id+title",id+"+"+Title); 

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
    wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "Place of Interest"); 

    if(id != lastplaceid && !nopopup) { 
     wakeLock.acquire(); 

     sp.setLastPlaceID(getApplicationContext(), id); 

     Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 
     int dot = 200; 
     int dash = 500; 
     int long_gap = 1000; 
     long[] pattern = {0, dash, dot, dash, long_gap}; 
     v.vibrate(pattern, -1); 

     alertbox = new AlertDialog.Builder(ShowMapDialog.this).create(); 
     alertbox.setTitle(getString(R.string.dialogTitle)); 
     alertbox.setMessage(getString(R.string.dialogShowPlaceText1)+Title+getString(R.string.dialogShowPlaceText2)); 
     alertbox.setButton(getString(R.string.dialogShowPlaceYes), new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       Intent showPlace = new Intent(getApplicationContext(),Showplace.class); 
       showPlace.putExtra("id", id); 
       startActivity(showPlace); 
      } 
     }); 
     alertbox.setButton2(getString(R.string.dialogShowPlaceNo), new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       finish(); 
      } 
     }); 
     alertbox.show(); 
    } else if(id != lastplaceid && nopopup){ 
     wakeLock.acquire(); 
     sp.setLastPlaceID(getApplicationContext(), id); 
     Intent showPlace = new Intent(getApplicationContext(),Showplace.class); 
     showPlace.putExtra("id", id); 
     startActivity(showPlace); 
    } else { 
     finish(); 
    } 
} 

@Override 
public void onPause(){ 
    super.onPause(); 
    wakeLock.release(); 
    alertbox.dismiss(); 
    finish(); 
} 

}

만들기 ProximityAlerts :

private void setProximityAlert(String Title, double lat, double lon, float radius, final int id, int requestCode){ 
     // Expiration is x Minutes (x mins * 60secs * 1000milliSecs) 
     long expiration = -1; 

     Intent intent = new Intent(PROXIMITY_INTENT_ACTION); 
     intent.putExtra("id", id); 
     intent.putExtra("Title", Title); 

     PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT); 

     mlocManager.addProximityAlert(lat, lon, radius, expiration, pendingIntent); 
    } 

    public void placeMarkersPoints(){ 
     this.dh = new DataHelper(ShowMap.this); 
     List<Pontos> list = this.dh.selectAll(); 
     markerPlaces = new OverlayPlaces(getResources().getDrawable(R.drawable.marker_places), mapView); 
     for(Pontos p : list){ 
      markerPlaces.addPoint(new GeoPoint(p.getLat(),p.getLng()),p.getName().toString(),Integer.toString(p.getId())); 
      setProximityAlert(p.getName().toString(), p.getLat(), p.getLng(), p.getRadius(), p.getId(), p.getId()); 
     } 
     mapView.getOverlays().add(markerPlaces); 
     mapView.invalidate(); 
    } 

나는이 같은에서 onCreate에 수신기를 등록 :

br = new ProximityAlert(); 
    mIntentFilter = new IntentFilter(PROXIMITY_INTENT_ACTION); 

onPause :

@Override 
public void onPause() { 
    super.onPause(); 
    mlocManager.removeUpdates(this); 
    unregisterReceiver(br); 
} 

onResume :

@Override 
protected void onResume() { 
    super.onResume(); 
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, this); 
    registerReceiver(br, mIntentFilter); 
} 

내가 두 번째 ProximityAlert 통과, 그것은 않습니다 두 번이 코드 : Log.d("id+title",id+"+"+Title);

어떤 아이디어가? 필요한 경우 더 많은 코드를 게시 할 수 있습니다.

+0

왜 디버그하지 않습니까? 너 어디서 실제로 붙어 있니? – nitind

+0

erm ... 실제로 호출 할 곳을보기 위해 무엇을 디버깅해야할지 모르겠다 ... – silentw

+0

@nitind 위의 주석을 살펴보십시오. – silentw

답변

1

사실 내 문제는 BroadcastReceivers에서 발생했습니다.

나는 그들을 파괴하고 있었지만 꽤 정확하게는 아니 었습니다. 그래서 때로는 두 배, 세 배 또는 w/e가되었습니다.

감사하지만 내가 직접 :) 해결

+0

마음이 수정 된 코드를 게시하고 있습니까? –

1

당신은 수신자에게 응용 프로그램이 다시 시작 할 때마다 등록된다.

@Override 
protected void onResume() { 
    super.onResume(); 
    ... 
    registerReceiver(br, mIntentFilter); 
} 

따라서 각각 근접 경보가 트리거 될 때마다 수신기 (PendingIntent)이 여러 인스턴스 (활성이 재개 될 때마다 하나에게)을 발사한다. 한 사람의 발신자와 복제 된 수신자가 있다고 상상해보십시오. 누구나 동시에 응답합니다.

onResume을 만들면 응용 프로그램이 일시 중지 될 때마다 근접 경고가 제거/파괴되는 한 괜찮습니다. onPause().

그렇지 않은 경우 기본/탐색 활동에서 한 번 만듭니다. 이 경우 수신자는 앱이 백그라운드 (예 : [알람 앱] 또는 [변경 사항 표시]가 원하는 동작 일 수 있음) 일 때에도 계속해서 실행됩니다.