为什么我的应用程序没有收到Intents?

时间:2015-04-12 02:38:35

标签: android android-intent

我正在尝试将我的应用程序接收其他应用程序共享的数据。即当有人通过Whatsapp发送youtube链接时,Youtube应用程序可以接收并打开显示。

我在清单上设置了这个:

<activity
    android:name="com.example.app.MainActivity"
    android:configChanges="orientation|screenSize|keyboardHidden"
    android:label="@string/app_name"
    android:launchMode="singleTask" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
</activity>

此代码进入MainActivity onCreate方法:

Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();

if (Intent.ACTION_SEND.equalsIgnoreCase(action) && type != null) {
    if ("text/plain".equals(type)) {
        String codigoIn = intent.getStringExtra(Intent.EXTRA_TEXT);
        if (codigoIn!=null) Toast.makeText(context, codigoIn, Toast.LENGTH_LONG).show();
    }
}

我尝试通过环聊测试发送自己的链接。当我点击链接时,选择器仅显示Chrome和Internet导航器。

拜托,怎么了?非常感谢你

更新:已解决

我已经解决了。我很迷茫。我不应该使用ACTION_SEND类别而是使用ACTION_VIEW。现在,清单是:

        <intent-filter android:label="my app">
            <data android:scheme="http" />
            <data android:host="myHost.com" />
            <data android:host="www.myHost.com" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.BROWSABLE" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

我把活动 - &gt; onResume方法:

    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();

    if (action.equalsIgnoreCase(Intent.ACTION_VIEW)){
        String dato = intent.getData().toString();
        Intent nuevoIntent = new Intent(context, Resultado.class);
        nuevoIntent.putExtra("codigo", dato);
        startActivity(nuevoIntent);
    }

我希望它对某人有用。花了两天时间修复

1 个答案:

答案 0 :(得分:0)

您可以尝试this answer 中的代码:

String action = intent.getAction(); 
if (action.equalsIgnoreCase(Intent.ACTION_SEND) && intent.hasExtra(Intent.EXTRA_TEXT)) { 
    String s = intent.getStringExtra(Intent.EXTRA_TEXT); 
    Toast.makeText(context, s, Toast.LENGTH_LONG).show();
}