android - onActivityResult()在我在选项卡中使用时不调用(zxing扫描程序)

时间:2012-09-21 06:29:11

标签: android zxing android-tabs

我使用 zxing扫描程序来扫描条形码和QR码,因为它是开源的。它工作正常,除了在选项卡中意味着我在选项卡组中使用相同的代码,但这里它不起作用。问题是onActivityResult()没有被调用。我们如何解决这个问题。

code

子活动

public class ScannScreenActivity extends Activity 
{  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.scan, null);
        this.setContentView(viewToLoad);

         mShowCamBut.setOnClickListener(new OnClickListener() {   
                public void onClick(View v) 
                {
                     IntentIntegrator integrator = new IntentIntegrator(ScannScreenActivity.this);
                     integrator.initiateScan();                 
                }                                             
            });
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) 
    {
          IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
          if (scanResult != null)
          {

              System.out.println("ScannScreenActivity FormatName:"+scanResult.getFormatName());
              System.out.println("ScannScreenActivity Content:"+scanResult.getContents());
          }       
    }
}

的ActivityGroup

public class TabGroupActivity extends ActivityGroup 
{
   //......
  ........Some code

  protected void onActivityResult(int requestCode, int resultCode, Intent data) 
  {

      if (resultCode == Activity.RESULT_OK) 
      {       
          ScannScreenActivity activity = (ScannScreenActivity) getLocalActivityManager().getCurrentActivity();
          activity.onActivityResult(requestCode, resultCode, data);
      }
  }
}

日志

09-21 11:45:32.448: D/memalloc(10723): /dev/pmem: Unmapping buffer base:0x5239b000 size:8560640 offset:7024640
09-21 11:45:32.458: D/memalloc(10723): /dev/pmem: Unmapping buffer base:0x51f79000 size:4280320 offset:2744320
09-21 11:45:33.949: W/IInputConnectionWrapper(10723): showStatusIcon on inactive InputConnection
09-21 11:45:33.999: W/IInputConnectionWrapper(10723): InputConnection = android.view.inputmethod.BaseInputConnection@40dc2da0, active client = false

4 个答案:

答案 0 :(得分:1)

zxing项目提供了IntentIntegrator类的几种“风味”。您应该使用IntentIntegrator而不是编写自己的代码。而且,您需要使用正确的版本。尝试IntentIntegratorV30,它允许您将结果发送到片​​段而不是主要活动。我相信这是你需要的本质,因为你真的在使用片段,对吗?

答案 1 :(得分:-1)

ScannScreenActivity activity = (ScannScreenActivity) getLocalActivityManager().getCurrentActivity(); 
activity.setResult(resultCode, data);

答案 2 :(得分:-1)

我试过扫描我使用过的QR-code.source。 mudah2an可以提供帮助。

private void startScanner() {

    try {
        Intent intent = new Intent(Class1.this, CaptureActivity.class);
        intent.setAction(Intents.Scan.ACTION);
        intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
        startActivityForResult(intent, 1);
    } catch (Exception e) {
        e.printStackTrace()
    }
}

@Override
public void onActivityResult(int p_requestCode, int p_resultCode, Intent p_data) {
    try {
        switch (p_requestCode) {
            case 1:
                if (p_resultCode == RESULT_OK) {
                   System.out.println(p_data.getStringExtra("SCAN_RESULT"));
                }
                break;

            default:
                System.out.println("Scan Failed")
                break;
        }
    } catch (Exception e) {
        E.handle(this, e);
    } catch (Error e){
        E.handle(this, e);
    }
}

答案 3 :(得分:-1)

根据我的知识,无论何时启动结果活动,它都会调用onActivityResult()作为示例:

startActivityForResult(intent,KEY);

然后在onActivityResult:

protected void onActivityResult(int requestCode,int resultCode,Intent data){    
    switch(requestCode){
    case KEY:
        if(resultCode==Activity.RESULT_OK)  
            *do whatever you want to *
        }
}