【发布时间】:2018-09-10 20:51:18
【问题描述】:
我正在尝试从 chrome 意图中获取信息。场景是当我从 chrome 或任何其他浏览器与我的应用程序共享网页时,我想获取标题和图像,让我们说一个简短的描述并将其存储到我的应用程序中。但问题是就我所做的研究而言,我从 chrome 意图中得到的唯一东西就是 URL。所以问题是如果我可以获取 URL 旁边的额外数据,如何找出与我的应用程序共享的 chrome Intent 如何获取它? 下面的代码只从chrome中抓取URL,那么如何抓取网页标题和图片呢?
androidManifest.xml
<activity
android:name=".WebActivity"
android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme.NoActionBar"></activity>
<activity
android:name=".GetDataActivity"
android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<!-- Used to handle Chrome then menu then share.-->
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:mimeType="text/plain" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>
GetDataActivity.java
public class GetDataActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_data);
// Get intent, action and MIME type
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent); // Handle text being sent
} else if (type.startsWith("image/")) {
handleSendImage(intent); // Handle single image being sent
}
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendMultipleTexts(intent); // Handle multiple images being sent
}
} else {
// Handle other intents, such as being started from the home screen
}
}
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
TextView datTitle = findViewById(R.id.dataTitle);
datTitle.setText(sharedText);
}
}
void handleSendImage(Intent intent) {
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
ImageView datImage = findViewById(R.id.dataImage);
datImage.setImageURI(imageUri);
}
}
void handleSendMultipleTexts(Intent intent) {
ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (imageUris != null) {
// Update UI to reflect multiple images being shared
}
}
}
【问题讨论】:
标签: android google-chrome android-intent browser