【问题标题】:What is the data's that chrome intent shares with android appschrome intent 与 android 应用程序共享的数据是什么
【发布时间】: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


    【解决方案1】:

    移动设备上的 Chrome 浏览器除了链接或网址外,不共享任何内容。所以我正在考虑一种方法来从设备中的移动应用程序中获取图像和标题等信息。但这不是它的工作方式。您只有链接,因此您有两种选择,一种是将链接发送到您的服务器,然后抓取您想要的信息,例如抓取或抓取元数据或图形数据。另一个是在设备的移动应用中做同样的事情,这几乎是不可能的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多