0

我的应用程序首先调用两项服务:一项用于收集位置数据,另一项用于对这些数据进行计算,并将每隔十几个小时针对特定点启动一次接近警报。我的问题是,一旦用户进入或退出感兴趣的区域,我就会收到接近警报以调用调查活动。调查出现了,但是当我在调查活动上调用完成()时,应用程序退出到主屏幕,当我恢复应用程序时,我再次回到调查活动。我不确定这是否是因为我正在调用许多接近警报,并且每个人都可能在我四处走动时调用了调查活动,这样我就会有一大堆调查活动,但这对于为什么没有意义完成后我被带到主屏幕。

这就是我称之为接近警报的地方。

private BroadcastReceiver mLocationUpdateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        proxsiglocList = intent.getParcelableArrayListExtra("Locations");
        for (int l = 0; l < proxsiglocList.size(); l++) {
            addProximityAlert(proxsiglocList.get(l).getLatitude(),
                    proxsiglocList.get(l).getLongitude());
        }
        proxsiglocList.clear();
    }
};

public void addProximityAlert(double latitude, double longitude) {
    LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Intent intent = new Intent(PROX_ALERT_INTENT + latitude + longitude);
    intent.putExtra("lat", latitude);
    intent.putExtra("lng", longitude);
    PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0,
            intent, 0);
    locManager.addProximityAlert(latitude, longitude, POINT_RADIUS,
            PROX_ALERT_EXPIRATION, proximityIntent);
    IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT + latitude
            + longitude);
    registerReceiver(new ProximityIntentReceiver(), filter);
}

这是我从 ProximityAlerts 调用调查的地方:

@Override
public void onReceive(Context context, Intent intent) {
    String key = LocationManager.KEY_PROXIMITY_ENTERING;
    Boolean entering = intent.getBooleanExtra(key, false);

    double latitude = intent.getDoubleExtra("lat", -1);
    double longitude = intent.getDoubleExtra("lng", -1);

    if (entering) {
        int id = 0;
        int updated = 0;

        LabelDatabase myDatabase = new LabelDatabase(context);
        myDatabase.open();
        Cursor cursor = myDatabase.getAllRows();
        cursor.moveToFirst();
        for (int i = 0; i < cursor.getCount(); i++) {
            if ((cursor.getDouble(1) == latitude)
                    && (cursor.getDouble(2) == longitude)) {
                id = cursor.getInt(0);
                updated = cursor.getInt(4);
                break;
            }
            cursor.moveToNext();
        }

        if (updated != 1) {
            myDatabase.updateAsked(id);
            Intent surveyIntent = new Intent(context, Survey.class);
            surveyIntent.putExtra("id", id);
            surveyIntent.putExtra("lat", latitude);
            surveyIntent.putExtra("lng", longitude);
            surveyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(surveyIntent);
        }
        myDatabase.close();

    } else {
        int id = 0;
        int updated = 0;

        LabelDatabase myDatabase = new LabelDatabase(context);
        myDatabase.open();
        Cursor cursor = myDatabase.getAllRows();
        cursor.moveToFirst();
        for (int i = 0; i < cursor.getCount(); i++) {
            if ((cursor.getDouble(1) == latitude)
                    && (cursor.getDouble(2) == longitude)) {
                id = cursor.getInt(0);
                updated = cursor.getInt(4);
                break;
            }
            cursor.moveToNext();
        }

        if (updated != 1) {
            myDatabase.updateAsked(id);
            Intent surveyIntent = new Intent(context, Survey.class);
            surveyIntent.putExtra("id", id);
            surveyIntent.putExtra("lat", latitude);
            surveyIntent.putExtra("lng", longitude);
            surveyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(surveyIntent);
        }
        myDatabase.close();
    }

}

这是我的调查活动的一部分:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.location_survey);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        id = extras.getInt("id");
        lat = extras.getDouble("lat");
        lng = extras.getDouble("lng");
    }

    // Add in the three spinners
    whereSpinner = (Spinner) findViewById(R.id.answer1);
    ArrayAdapter<CharSequence> whereAdapter = ArrayAdapter
            .createFromResource(this, R.array.where_array,
                    android.R.layout.simple_spinner_item);
    whereAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    whereSpinner.setAdapter(whereAdapter);

    whoSpinner = (Spinner) findViewById(R.id.answer3);
    ArrayAdapter<CharSequence> whoAdapter = ArrayAdapter
            .createFromResource(this, R.array.who_array,
                    android.R.layout.simple_spinner_item);
    whoAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    whoSpinner.setAdapter(whoAdapter);

}

// remove row and proximity alert
public void onNotSigClicked(View v) {
    LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    LabelDatabase myDatabase = new LabelDatabase(getBaseContext());
    myDatabase.open();
    myDatabase.deleterow(id);
    myDatabase.close();

    Intent intent = new Intent(PROX_ALERT_INTENT + lat + lng);
    PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0,
            intent, 0);
    locManager.removeProximityAlert(proximityIntent);
    finish();
}

public void onSaveClicked(View v) {
    // Save profile
    saveProfile();
    finish();
}

再一次,我不确定为什么我的一个或多个调查活动总是把我带到主屏幕,每次我返回时,即使我调用了finish(),我最终还是回到了调查活动。当我只遇到我的第一个调查活动并且当我调用完成()时,这很好用,我被带回我的 MainActivity。

4

0 回答 0