首页 > 解决方案 > Chrome 和 Android 设备之间的通信

问题描述

大家好,我正在尝试按照文档将字符串从 Chrome 应用程序发送到另一个应用程序,但没有结果。

我的页面在 Chrome 应用程序上打开:

<!DOCTYPE html>
<html>
<body>
<p>URL: <a 
href="intent://#Intent;scheme=printer_receiver;package= 
kangel.com.printerhandler;S.name=Andrea;i.age=35;end">Do action</a></p>
</body>
</html>

我的活动

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

    mText = findViewById(R.id.textUploaded);

    //TRY ONE
    Uri data = this.getIntent().getData();
    if (data != null && data.isHierarchical()) {
        String uri = this.getIntent().getDataString();
        Log.i("PrinterHandler", "Deep link clicked " + uri);
        mText.setText(uri);
    }
    //TRY TWO
    Bundle extras = getIntent().getExtras();
    if (extras != null){
        String name = extras.getString("name");
        Integer age = extras.getInt("age");
        if (name!=null && age!=null) {
            mText.setText(name);
        }
    }else{
        mText.setText("No Data");
    }
}

并体现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kangel.com.printerhandler">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <data android:scheme="printer_receiver" />
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.BROWSABLE" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

当我在浏览器中单击时,加载一些东西但从不加载应用程序。你有什么想法?

标签: androidgoogle-chromeandroid-intentwebintents

解决方案


推荐阅读