Java 在应用程序运行时接收意图
我已成功使我的应用程序在未运行时接收到意图,它将启动并正确地将URI数据传递给应用程序的其余部分。它是一个本地C++应用程序,但下面的所有代码都涉及到java/Android API方面。 我正在尝试接收和处理在应用程序运行时收到的意图,无论是否是后台的。到目前为止,我还没有做到这一点,接收器似乎没有触发,我通常在从shell调用时会出现此错误,这表明我设置了错误的内容Java 在应用程序运行时接收意图,java,android,xml,android-intent,android-manifest,Java,Android,Xml,Android Intent,Android Manifest,我已成功使我的应用程序在未运行时接收到意图,它将启动并正确地将URI数据传递给应用程序的其余部分。它是一个本地C++应用程序,但下面的所有代码都涉及到java/Android API方面。 我正在尝试接收和处理在应用程序运行时收到的意图,无论是否是后台的。到目前为止,我还没有做到这一点,接收器似乎没有触发,我通常在从shell调用时会出现此错误,这表明我设置了错误的内容 adb shell am start -W -a android.intent.action.VIEW -d "testapp
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 }
我已经尝试了广播接收者的清单和动态分配,如下所述
我也一直在阅读官方文档,但虽然信息丰富,但它没有太多的代码示例
我在这里包括我的清单片段,请注意这是一个模板,某些值通常以双下划线作为前缀,在构建时自动填充
<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>
如果您查看,您会发现,am start仅用于启动活动,您应该知道,该广播接收器!=活动对于广播,您应该使用am broadcast。实际上,我只需要在活动中使用onNewIntent方法,而不是使用广播接收器onNewIntent谢谢,这可能会有所帮助,但是它仍然没有将数据传递给程序ADB shell am broadcast-W-a android.intent.action.VIEW-d测试appschema://EpisodeI 实际上,我只需要在我的活动中使用onNewIntent方法,而不是使用广播接收器@Sedontane这个命令对我来说很好,我得到以下输出IntentListener﹕ 目的:测试appschema://Episodefound 它,但不能回答我自己的问题,再过几个小时,我实际上只需要在我的活动中使用onNewIntent方法,而不是使用广播接收器
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;
}
}