3

そのため、現在、アプリでzxingバーコードスキャナーを使用しています。コード例(generic)は次のとおりです。

if(position == 0){
            Intent intent = new Intent("com.google.zxing.client.android.SCAN");
            intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
            startActivityForResult(intent, 0);


        }

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                contents = intent.getStringExtra("SCAN_RESULT");
                format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                // Handle successful scan
                Intent i = new Intent(Main.this, BarcodeScanner.class);
                startActivity(i);
            } else if (resultCode == RESULT_CANCELED) {
                // Handle cancel
            }
        }
    }

ですから、を起動するときにBarcodeScanner.class、私もそれに渡したいと思いcontentsます。どうすればいいですか?

4

2 に答える 2

6

インテント内のバンドルを使用して、あるアクティビティから別のアクティビティにデータを渡します。あなたの場合、あなたは次のようなことをしなければならないでしょう-

        Intent intent = new Intent(Main.this,BarcodeScanner.class);

        //load the intent with a key "content" and assign it's value to content            
        intent.putExtra("content",contents);

        //launch the BarcodeScanner activity and send the intent along with it
        //note that content  is passed in as well             
        startActivity(intent);

情報は、インテント内にある「バンドル」オブジェクトに格納されます。バンドルは、インテントオブジェクトのputExtras()メソッドを呼び出すと作成されます。

于 2010-10-30T22:19:56.330 に答える
1

"SCAN_MODE"他のアクティビティに渡したのと同じ方法で、を呼び出すputExtra("some key", contents)前に呼び出しstartActivity()てから、BarcodeScanner内で呼び出しますthis.getIntent().getStringExtra("some key")

于 2010-10-30T21:43:33.070 に答える