Jump to content
Sign in to follow this  
Yaron

Sharing from YouTube to my App reliably (Android Intents)

Recommended Posts

I am sharing a video's URL from YouTube to my Android app.

 

I do this using Android Intents by modifying the "AndroidManifest.template.xml", adding:

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

 

Then in my app's form OnCreate event, I use:

  if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, AppEventService) then
    AppEventService.SetApplicationEventHandler(HandleAppEvent);

  MainActivity.registerIntentAction(TJIntent.JavaClass.ACTION_SEND);
  TMessageManager.DefaultManager.SubscribeToMessage(TMessageReceivedNotification, HandleActivityMessage);

Then HandleAppEvent looks like this:

function TMainForm.HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
var
  StartupIntent: JIntent;
begin
  Result := False;
  Case AAppEvent of
    TApplicationEvent.BecameActive:
    Begin
      clientAndroidBecameActive := True;
      If clientAndroidActivated = False then
      Begin
        clientAndroidActivated := True;
        StartupIntent := MainActivity.getIntent;
        if StartupIntent <> nil then
        Begin
          HandleIntentAction(StartupIntent);
          StartupIntent := nil;
        End;
      End;
    End;
  end;
end;

And HandleActivityMessage looks like:

procedure TMainForm.HandleActivityMessage(const Sender: TObject; const M: TMessage);
begin
  if M is TMessageReceivedNotification then HandleIntentAction(TMessageReceivedNotification(M).Value);
end;

And finally, HandleIntentAction:

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));
      // Do something useful with the URL
      Extras := nil;
    End
  end
end;

My problem is there are occasions after sharing where an intent is duplicated, triggering once on the share and again when opening the app later on.

 

Does anyone have experience with something similar?

 

 

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  

×