java - 接近警报触发两次?

标签 java android eclipse osmdroid

接收者:

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

}

显示 map 对话框.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);

暂停:

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

关于恢复:

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

当我通过第二个 ProximityAlert 时,它会执行两次此代码:Log.d("id+title",id+"+"+Title);

有什么想法吗?如果需要,我可以发布更多代码:)

最佳答案

实际上我的问题出在 BroadcastReceivers 上。

我正在摧毁它们,但不是很正确,所以有时它们会翻倍、三倍或 w/e。

谢谢,但我自己解决了:)

关于java - 接近警报触发两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10412677/

相关文章:

java - DATE 的 JDBC 错误格式

java - LRU缓存: linkedhashmap and iterator implementation not working

java - JPanel 调整大小未发生

android - 我如何在 Android 上进行身份验证

android - 将 ImageView 更改为黑白

android - 在 WebView 中加载大部分页面后关闭 ProgressCircle

java - 将下载的 minecraft mod 更新到 1.8

java - 在 iReport 中对列进行排序

java - 在 JRE 8 中使用 JavaFX, “Access restriction” 错误

c++ - OSX C++ Eclipse 经常崩溃,我该怎么办?