Jump to content
Sign in to follow this  
Yaron

Android: One app receiving multiple intents

Recommended Posts

I am trying to write Android intent code so when a user chooses to share text with another application, my app will appear twice as an option, each with a different label and a mechanism I can distinguish between the two intents.

 

Basically, I'm trying to have  "Play" and "Queue"  options for URLs shared from the YouTube app.

 

I have no issues with just one intent, defining in  "AndroidManifest.template.xml"

            <intent-filter>  
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>

And in code:

  // Register the type of intent action that we want to be able to receive.
  // Note: A corresponding <action> tag must also exist in the <intent-filter> section of AndroidManifest.template.xml.
  MainActivity.registerIntentAction(TJIntent.JavaClass.ACTION_SEND);
  TMessageManager.DefaultManager.SubscribeToMessage(TMessageReceivedNotification, HandleActivityMessage);
procedure TMainForm.HandleActivityMessage(const Sender: TObject; const M: TMessage);
begin
  if M is TMessageReceivedNotification then
    HandleIntentAction(TMessageReceivedNotification(M).Value);
end;
function TMainForm.HandleIntentAction(const Data: JIntent): Boolean;
var
  Extras   : JBundle;
  sURL     : String;
begin
  Result := False;
  if Data <> nil then
  begin
    Extras := Data.getExtras;
    if Extras <> nil then
    Begin
      sURL := JStringToString(Extras.getString(TJIntent.JavaClass.EXTRA_TEXT));
      {$IFDEF TRACEDEBUG}AddDebugEntry('Intent string "'+sURL+'"');{$ENDIF}
      Extras := nil;
    End
  end
end;

 

How can I add a second intent?

Share this post


Link to post
5 hours ago, Dave Nottage said:

Does calling registerIntentAction with another action not work?

I don't see the mechanism that would allow me to distinguish between multiple intents.

I don't understand how calling "MainActivity.registerIntentAction(TJIntent.JavaClass.ACTION_SEND);" again would help.

 

Share this post


Link to post

I tried modifying the manifest file to include two entries with different labels:

            <intent-filter android:label="Play in XX">  
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter> 
            <intent-filter android:label="Queue in XX">  
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>

However, only the first entry shows up in YouTube and on top of that I'm not even sure how to read the label value from my Delphi code so I can distinguish between the two call even if YouTube did list them.

Edited by Yaron

Share this post


Link to post
13 hours ago, Yaron said:

I don't see the mechanism that would allow me to distinguish between multiple intents.

You can distinguish between intent filters by the action, e.g:

if Data.getAction.equals(TJIntent.JavaClass.ACTION_SEND) then

 

9 hours ago, Yaron said:

However, only the first entry shows up in YouTube

Probably because you cannot have 2 filters working with the same action. I suggest reading:

https://developer.android.com/training/basics/intents/filters

Share this post


Link to post

@Dave Nottage

Yes, I am working with the same "SEND" action.

 

I read the article you linked to, but didn't see any mention regarding the same action used more than once.

 

I did get an answer on stackoverflow that I should use an Activity Alias, but haven't had time to test it yet and I haven't figured out yet (mainly due to lack of time) how to do this in delphi :

if (intent.component.className.endsWith(".Queue"){
    // putting data in the queue...
} else {
    // playing requested data...
}


 

Edited by Yaron

Share this post


Link to post
12 hours ago, Yaron said:

if (intent.component.className.endsWith(".Queue"){

Delphi equivalent:

if JStringToString(intent.getComponent.getClassName).EndsWith('.Queue') then

 

  • Like 1

Share this post


Link to post

If anyone is interested, here's the relevant section of the manifest file after I tested it works correctly:
 

        <activity android:name="com.embarcadero.firemonkey.FMXNativeActivity"
                android:label="ZP Remote"
                android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
                android:launchMode="singleTask">
            <!-- Tell NativeActivity the name of our .so -->
            <meta-data android:name="android.app.lib_name"
                android:value="ZP_Remote" />
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter> 
            <intent-filter android:label="Play in ZP">  
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter> 
        </activity>
        <activity-alias
                android:targetActivity="com.embarcadero.firemonkey.FMXNativeActivity"
                android:name="com.embarcadero.firemonkey.FMXNativeActivity.Queue">
            <!-- Put second SEND action inside activity-alias tag -->
            <intent-filter android:label="Queue in ZP">
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>
        </activity-alias>

 

Edited by Yaron
  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×