2016-07-25 103 views
1

我有2個活動:創建/接收。隱含的意圖未能發送到我的其他活動

創建類:

public void onSendMessage(View view){ 
    EditText msgText = (EditText)findViewById(R.id.messageText); 
    String msg = msgText.getText().toString(); 
    Intent intent = new Intent(); 
    intent.setAction(Intent.ACTION_SEND); 
    intent.putExtra(Intent.EXTRA_TEXT, msg); 
    intent.setType("text/plain"); 

    if (intent.resolveActivity(getPackageManager()) != null){ 
     startActivity(intent); 
    } 
} 

接收類:

public static final String EXTRA_TEXT = "message"; 

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

    Intent intent = getIntent(); 
    String msgText = intent.getStringExtra(EXTRA_TEXT); 
    TextView msgView = (TextView)findViewById(R.id.receiveText); 
    msgView.setText(msgText); 
} 

清單:

​​

調試到它揭示了味精獲取第一個活動創建,但不知何故,我到達接收活動時爲空。

任何提示出了什麼問題?

編輯: 好的,只是爲了更清楚我希望達到的目標。我希望我的活動能夠將文本發送到消息應用程序和我的第二個活動。

發送此:

// This works for sending to Messaging app, but my second Activity can't read this 
intent.putExtra(Intent.EXTRA_TEXT, msg); 

// This works for sending to my second Activity, but the Messaging app can't read this 
intent.putExtra("EXTRA_TEXT", msg); 

請指教。

回答

0

我認爲你的問題來源於此行

public static final String EXTRA_TEXT = "message";` 

Receive類。

Intent類已經定義了EXTRA_TEXT常數here

+1

嘿,謝謝。現在你已經說了,我只是意識到自己正在愚蠢:) OOP的一年真的讓我的大腦慢下來。謝謝! – Farid

0

使用相同的keyName同時通過意圖與接收的時間傳遞數據..

某處定義的常量變量或常量創建一個類,然後把它作爲普通鍵名。

InSender FirstActivity

intent.putExtra("keyName", msg); 
startActivity(intent); 

InReceiver SecondActivity

String data= getIntent().getStringExtra("keyName"); 
+0

你能更具體嗎?因爲將其寫爲: intent.putExtra(「EXTRA_TEXT」,msg); String data = getIntent()。getStringExtra(「EXTRA_TEXT」); 適用於將消息發送到我的ReceiveActivity。但是現在,當我嘗試將消息發送到消息(Android SMS應用程序)時,它會中斷。 我很確定這與語法有關,但我無法弄清楚如何/爲什麼... – Farid

相關問題