如何在startActivityForResult之后返回

时间:2017-04-09 08:24:54

标签: android android-intent

我试图让我的应用程序在startActivity函数之后返回。我不需要返回任何东西,我只需要应用程序在调用startActivity的同一点继续,原因是我正在寻找磁力计的变化,然后在检测到应用程序时可以继续 我还没有找到任何办法,我尝试了startActivityForResult,但是nether也可以。有没有办法定义设置startActivity或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;
        }

2 个答案:

答案 0 :(得分: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();
        }
    }
}

答案 1 :(得分:0)

由于现在远离android studio,我无法为您编写确切的代码。但解释一下流程。

覆盖您呼叫onActivityResult的活动中的方法startActivityForResult

它会为您提供requestCoderesultCode和Intent(包含数据或不包含数据)。 requestCode与您使用startActivityForResult

发送的一样