【发布时间】:2014-03-01 00:05:37
【问题描述】:
我正在尝试从浏览器获取字符串。我在 AndroidManifest.xml 中使用了 SEND 操作
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
在 MainActivity.java 中,我有以下代码
@Override
public void onResume() {
super.onResume();
Intent receivedIntent=getIntent();
etSearch.setText(getStringFromIntent(receivedIntent));
}
public String getStringFromIntent(Intent receivedIntent) {
// get the action
String receivedAction = receivedIntent.getAction();
// find out what we are dealing with
String receivedType = receivedIntent.getType();
if (receivedAction.equals(Intent.ACTION_SEND)) {
if (receivedType.startsWith("text/")) {
// get the received text
String receivedText = receivedIntent
.getStringExtra(Intent.EXTRA_TEXT);
// check we have a string
if (receivedText != null) {
// set the text
return receivedText.trim();
}
}
}
return "";
}
我第一次尝试分享一切正常
当我再次尝试这样做时,文本没有更新
我认为是因为 onCreate()。所以我将代码移到 onResume() 中。什么都没发生。
根据这个solution我添加了receivedIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);和
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
this.setIntent(intent);
}
没有效果。
我应该怎么做才能额外更新 Intent?
【问题讨论】:
标签: android android-intent browser