将活动结果返回给另一个班级

时间:2015-10-30 19:15:10

标签: java android android-activity

所以我有RegisterActivity.java,在用户点击注册后,它应该调用ScanActivity.java中的ScanActivity()方法。

在ScanActivity()中,它将开始扫描活动,并将扫描结果返回给RegisterActivity.java。

使用扫描结果,用户验证将在RegisterActivity.java中进行。

怎么做?

在RegisterActivity.java

// call ScanActivity
ScanActivity scanActivity = new ScanActivity();

// get scan result from ScanActivity
String scanResult = scanActivity.ScanActivity();

// compare with useridtxt
// if useridtxt same with scan result, start save into parse
if (useridtxt.equals(scanResult)) {
    // some code here
} else if (!useridtxt.equals(scanResult)) {
    // some code here
} else {
    // some code here
}

在ScanActivity.java中

public class ScanActivity extends Activity {

public static final int CODE39 = 39;
private static final int ZBAR_SCANNER_REQUEST = 0;

String ScanActivity() {

    Intent intent = new Intent(this, ZBarScannerActivity.class);
    intent.putExtra(ZBarConstants.SCAN_MODES, new int[]{Symbol.CODE39});
    startActivityForResult(intent, ZBAR_SCANNER_REQUEST);
    String scanResult = getIntent().getStringExtra(ZBarConstants.SCAN_RESULT);
    return scanResult;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (resultCode == RESULT_OK)
    {
        // maybe save the scan result here, and pass it to String scanResult in RegisterActivity.java?
    } else if(resultCode == RESULT_CANCELED) {
        Toast.makeText(this, "Camera unavailable", Toast.LENGTH_SHORT).show();
    }
}

1 个答案:

答案 0 :(得分:0)

这很简单。 示例代码可以如下运行

startActivityForResult(Intent intent, int requestCode); //Fill intent with your desired class.

在目标类(ScanActivity)中,不要忘记将结果放回

setResult (int resultCode, Intent data) //in data bundle put all your required output

这样您就可以使用预期的结果代码提取匹配的数据。

现在在来电课上

protected void onActivityResult (int requestCode, int resultCode, Intent data) {} //Your logic to manipulate data