【发布时间】: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