2010-10-30 88 views
3

所以現在我在我的應用程序中使用zxing條碼掃描器。下面是示例代碼(通用):如何使用Intent啓動活動並在新活動中傳遞變量?

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進去。我會怎麼做呢?

回答

6

使用Bundle內內意圖從一個活動數據傳遞給其他的方式相同。在你的情況,你會做這樣的事情 -

 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()方法創建包對象

1

你打電話startActivity()前通過"SCAN_MODE"其他活動,通過調用putExtra("some key", contents),然後BarcodeScanner呼叫this.getIntent().getStringExtra("some key")

相關問題