![](/img/trans.png)
[英]JSP - How can I send data and recieve a response when sending to an external site
[英]How can I receive send and recieve multiple data with an implicit intent?
我正在從一個活動向另一個活動發送隱式意圖,並且我想發送一些String變量。 我這樣做是隱式的,因為我希望在多個項目中使用活動B,這是庫項目的一部分。
我的意圖很好,但是活動B並未讀取額外的數據。
這是我在活動A中創建意圖的代碼:
Intent intent = new Intent(this, ShareResultsActivity.class);
intent.putExtra(EMAIL_SUBJECT, "Results");//String
intent.putExtra(RESULTS_SAVENAME, saveName);//String
intent.putExtra(RESULTS_BODY, body);//String
intent.putExtra(WITH_SAVE_BUTTON, withSaveButton);//Boolean
startActivity(intent);
在活動B中:
Bundle extras = getIntent().getExtras();
Boolean withSaveButton = true;
if(extras != null){
emailSubject = extras.getString("EMAIL_SUBJECT");
resultsSaveName = extras.getString("RESULTS_SAVENAME");
resultsBody = extras.getString("RESULTS_BODY");
withSaveButton = extras.getBoolean("WITH_SAVE_BUTTON", true);
}
這不起作用,並且String變量為null。
我還嘗試過以明確意圖接收數據的方式來執行此操作:
Intent intent = getIntent();
emailSubject = intent.getStringExtra("EMAIL_SUBJECT");
resultsSaveName = intent.getStringExtra("RESULTS_SAVENAME");
resultsBody = intent.getStringExtra("RESULTS_BODY");
Boolean withSaveButton = intent.getBooleanExtra("WITH_SAVE_BUTTON", true);
但這也不起作用。
我可以使它工作的唯一方法是,如果我在getStringExtra中引用了發送活動,但是由於它應該是一個庫類,所以我無法執行此操作。
誰能對此有所啟示? 還是我會以錯誤的方式處理此問題?
我假設您的代碼是這樣的:
final static String EMAIL_SUBJECT = "email_subject";
// RESULTS_SAVENAME and others are the same style.
Intent intent = new Intent(this, ShareResultsActivity.class);
intent.putExtra(EMAIL_SUBJECT, "Results");//String
startActivity(intent);
然后在另一個活動中:
Intent intent = getIntent();
emailSubject = intent.getStringExtra("email_subject");
// or this
// emailSubject = intent.getStringExtra(EMAIL_SUBJECT);
請使用密鑰,兩個活動中的密鑰應該相同。 我在兩個活動中清楚地看到了鍵名的命名約定的差異,如果鍵名相同,則該數據不應傳遞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.