2017-04-09 43 views
0

我想让我的应用程序在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; 
     } 
+0

'我只需要应用程序继续调用startActivity的同一点即可。如果你有正确的编码,那么在语句startActivity()之后没有进一步的代码。当您的活动进入后台时,活动可能会被杀死。这是一个正常的活动生命周期过程。您应该重写onSaveInstanceState以保存活动的实例变量。然后让他们回到onCreate()例如。 – greenapps

回答

0

要完成活动中使用这样的:

Intent intent = new Intent(); 
intent.putExtra("SOMETHING", "EXTRAS"); 
this.setResult(RESULT_OK, intent); 
finish(); 

,然后你会得到导致在你这里父活动:

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == request_Code) { 
     if (resultCode == RESULT_OK) { 
      String returnedResult = data.getData().toString(); 
     } 
    } 
} 
+0

谢谢,从答复我认为没有直接的方式返回,但我需要将结果后的所有代码移入或从onActivityResult方法调用。没有其他解决方法。 – Chrish

+0

是的,你是对的 – John

0

我无法写出准确的代码在这里为你,因为现在远离android工作室。但解释流程。

覆盖您的活动方法onActivityResult您拨打startActivityForResult的地方。

它会给你的requestCoderesultCode,和意图(有数据或无)。 requestCode将与您发送的内容一样startActivityForResult

相关问题