我正在尝试从浏览器获取字符串。我在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()中。但是仍然没有任何变化。
根据这个解决方案,我添加了receivedIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
和
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
this.setIntent(intent);
}
没有效果。
为了更新意图附加信息,我应该怎么做?
PendingIntent.FLAG_UPDATE_CURRENT
- behelit