2017-10-20 209 views
0

我使用ZXing Android嵌入式庫讀取QRCode並且onActivityResult未在QRCode成功掃描後調用,請幫助。ZXing not calling onActivityResult,當掃描QR碼

public class ScanActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_send_money); 

     Button scanButton = (Button) findViewById(R.id.scan_button); 
     scanButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       IntentIntegrator integrator = new IntentIntegrator(ScanActivity.this); 
       integrator.initiateScan(); 
      } 
     }); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     System.out.println("*************** Why is this method not called?"); 
     IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); 
     if (result != null) { 
      if (result.getContents() == null) { 
       Toast.makeText(this, "Result Not Found", Toast.LENGTH_LONG).show(); 
      } else { 
       Toast.makeText(this, "Result is: ", Toast.LENGTH_LONG).show(); 
      } 
     } else { 
      super.onActivityResult(requestCode, resultCode, data); 
     } 
    } 
} 
+1

上面的代碼將正常工作。 onActivityResult方法將在成功掃描條形碼後調用。我已經測試過這種行爲 –

回答

1

這段代碼的問題是,我已經在清單文件

<activity 
    android:name=".ScanActivity" android:noHistory="true" /> 

刪除android:noHistory="true"解決使用android:noHistory="true"ScanActivity問題

<activity 
    android:name=".ScanActivity" /> 
0
IntentIntegrator integrator = new IntentIntegrator(ScanActivity.this); 
      integrator.initiateScan(); 

integrator.initiateScan();不會調用或不反映startActivityForResult操作。

你必須調用startActivityForResult,以便讓你可以有回調的onActivityResult