【问题标题】:receiving intents while application is running在应用程序运行时接收意图
【发布时间】:2014-04-23 09:45:04
【问题描述】:

我已成功使我的应用程序在未运行时接收到意图,它会启动并将 URI 数据正确地传递到应用程序的其余部分。它是一个原生 C++ 应用,但下面的所有代码都与它的 Java/Android API 相关。

我正在尝试接收和处理在应用程序运行时收到的意图,无论是否在后台。到目前为止我还不能这样做,接收器似乎没有触发,我通常在从 shell 调用时得到这个错误,这表明我设置了错误的东西。

adb shell am start -W -a android.intent.action.VIEW -d "testappschema://Episode"
Starting: Intent { act=android.intent.action.VIEW dat=abg://Episode }
Error: Activity not started, unable to resolve Intent { act=android.intent.action.VIEW dat=testappschema://Episode flg=0x10000000 }

我已经尝试了广播接收器的清单和动态分配,如此处所述Vogella Tutorial

我也一直在阅读官方文档,但虽然内容丰富,但它并没有太多的代码示例。

我在此处包含我的清单 sn-p,请注意这是一个模板,某些值(通常以双下划线为前缀)在构建时自动填充

<application android:label="@string/app_name" android:icon="@drawable/app_icon" android:hasCode="true" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:debuggable="__DEBUGGABLE" android:name="__APPLICATIONNAME" android:largeHeap="true">
 <activity android:name="__ACTIVITYNAME" android:label="@string/app_name" android:configChanges="orientation|keyboardHidden|screenSize" android:screenOrientation="sensorLandscape" android:launchMode="singleTask">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
  <intent-filter android:label="@string/app_name">
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="testappschema_launch" />
  </intent-filter>
</activity>
<receiver   
    android:name="com.testapp.IntentListener"
    android:enabled="true"
    android:exported="true">
    <intent-filter android:label="@string/app_name">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="testappschema" />
    </intent-filter>
</receiver>

这是我的 BroadcastReciever 类

package com.testapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import android.util.Log;

public class IntentListener extends BroadcastReceiver{

    private Intent m_tCurrentIntent = null;

    @Override
    public void onReceive(Context context, Intent tIntent) {
            Log.d("IntentListener", "IntentRecv: " + tIntent.getData().toString());
        m_tCurrentIntent = tIntent;
    }

    public Intent getCurrentIntent()
    {
        Log.d("IntentListener", "getCurrentIntent");
        Intent tTempIntent = m_tCurrentIntent;
        m_tCurrentIntent = null;
        return tTempIntent;
    }

}

【问题讨论】:

  • 找到了,但又几个小时无法回答我自己的问题我实际上只需要在我的活动中使用 onNewIntent 方法,而不是使用广播接收器onNewIntent - android docs

标签: java android xml android-intent android-manifest


【解决方案1】:

如果您查看ADB shell commands,您会发现am start 仅用于启动Activity,您应该知道BroadcastReceiver != Activity。对于广播,您应该改用am broadcast

【讨论】:

  • 谢谢,这可能会有所帮助,但它仍然没有将数据传递给程序
  • adb shell am 广播 -W -a android.intent.action.VIEW -d "testappschema://Episode"
  • 我实际上只需要在我的活动中使用 onNewIntent 方法,而不是使用广播接收器onNewIntent - android docs
  • @Sedontane 这个命令对我来说很好,我得到以下输出 "IntentListener: IntentRecv: testappschema://Episode"
【解决方案2】:

我实际上只需要在我的活动中使用 onNewIntent 方法,而不是使用广播接收器 onNewIntent

【讨论】:

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