我想让我的应用程序在startActivity函数后返回。我不需要返回任何东西,我只需要应用程序继续调用startActivity的同一点,原因是我正在寻找磁力计中的变化,然后在检测到应用程序可以继续时。 我还没有找到任何方法来做到这一点,我尝试了startActivityForResult,但是下面的工作。有没有一些方法来定义setup startActivity或startActivityForResult,以便它将返回到调用它的地步? 或其他方式来做到这一点。 我花了很多时间尝试不同的方法,但都没有工作,请帮助我用完想法。如何在startActivityForResult后返回
private void scanStart() { // was private added static
//
snloop=0;
if (startscan != 1) {
final Intent msens = new Intent(this, MldpBluetoothScanActivity.SensorActivity.class); // Added by Chris
msens.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivityForResult(msens,snloop);
}
startscan =0;
public static class SensorActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mMagnetometer;
private final float[] mMagnetometerReading = new float[3];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
}
@Override
protected void onPause() {
super.onPause();
// Don't receive any more updates from sensor.
mSensorManager.unregisterListener(this);
}
// Get readings from accelerometer and magnetometer. To simplify calculations,
// consider storing these readings as unit vectors.
@Override
public void onSensorChanged(SensorEvent event) {
float axisX = event.values[0];
float axisY = event.values[1];
float axisZ = event.values[2];
ActionBar actionBar = getActionBar(); //Get the ActionBar
// if (snloop == 0) {
if (axisZ > lastZ + 500 || axisZ < lastZ - 500) {
actionBar.setTitle("greater than 500"); //Set the title on the ActionBar
actionBar.setDisplayHomeAsUpEnabled(true); //Make home icon clickable with < symbol on the left to go back
setProgressBarIndeterminate(true); //Make the progress bar indeterminate
setProgressBarIndeterminateVisibility(true); //Make the progress bar visible
snloop = 1;
lastZ = axisZ;
startscan =1;
onPause();
else {
actionBar.setTitle("Please Place on Interchange Point"); //Set the title on the ActionBar
actionBar.setDisplayHomeAsUpEnabled(true); //Make home icon clickable with < symbol on the left to go back
setProgressBarIndeterminate(true); //Make the progress bar indeterminate
setProgressBarIndeterminateVisibility(true); //Make the progress bar visible
snloop = 0;
lastZ = axisZ;
}
'我只需要应用程序继续调用startActivity的同一点即可。如果你有正确的编码,那么在语句startActivity()之后没有进一步的代码。当您的活动进入后台时,活动可能会被杀死。这是一个正常的活动生命周期过程。您应该重写onSaveInstanceState以保存活动的实例变量。然后让他们回到onCreate()例如。 – greenapps