I actually just needed to use the method onNewIntent within my activity, instead of having a broadcast receiver onNewIntent
receiving intents while application is running
-
08-07-2023 - |
質問
I have successfully made my application receive intents while not running, it starts and passes the URI data through to the rest of the app correctly. It is a native C++ app but all the code below relates to the Java/Android API side of it.
I am trying to receive and process intents that are received while the application is running, whether backgrounded or not. So far I haven't been able to do this, the receiver just doesn't seem to trigger and I usually get this error when calling from the shell, which indicates that i have set something up incorrectly.
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 }
I have tried both the manifest and dynamic allocation of a BroadcastReciever as described here Vogella Tutorial
I have also been reading the official documentation but whilst informative it doesnt have much in the way of code samples.
I include my manifest snippet here, please note this is a template certain values (usually prefixed with a double underline) are autofilled at build time
<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>
And this is my BroadcastReciever class
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;
}
}
解決 2
他のヒント
If you will take a look on the ADB shell commands, you'll find, that am start
is used for starting Activity
only, you should know, that BroadcastReceiver != Activity
. For broadcasting you should use am broadcast
instead.