我的应用程序通过调用两个服务开始:一个用于收集位置数据,另一个用于运行对该数据的计算,并且每十几小时就会针对特定点启动近距离警报。我的问题是,一旦用户进入或退出感兴趣的区域,我就会收到接近警报以调出调查活动。调查结果显示,但是当我在调查活动上调用完成()时,应用会退出到主屏幕,当我恢复应用时,我会再次返回调查活动。 我不确定这是不是因为我打电话给很多接近警报,他们每个人都可能在我走动时打电话给调查活动,所以我会有一大堆调查活动,但这对于为什么没有意义完成后我被带到主屏幕。
这是我称之为接近警报的地方。
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。