Sending Android Intents from Delphi (Part 1)

As you probably know, I work for the italian embarcadero representative (www.bittime.it), so last week, I’ve been in Milan (Italy) with the Delphi Product Manager Marco Cantù, to show the new Delphi XE5 for Android (and iOS…).

Users (old and new) were enthusiastics. The Delphi-WAY combined with the Android openness and flexibility, is really a good way to develop mobile apps for the business customers.

So, after the initial demos some users were interested in sending Android Intents from their Delphi app.

I’ve builded some demos about Intents, so I showed those demos to them (in the next weeks there will be many Delphi for Android training in Italy, so I’ve build those demo in advance)

In Delphi XE5 you can call java classes from the Google SDK. So I started investigating hot to do this.

There are a lot of possibilities, and I’m still doing research, but this is a simple Intent send demos.

The Intents demo app
The Intents demo app

 

This is the code under the first button.

procedure THeaderFooterForm.Button1Click(Sender: TObject);
var
  Intent: JIntent;
begin
  // JAVA SDK ANDROID HELP SAYS...
  // String url = "http://www.example.com";
  // Intent i = new Intent(Intent.ACTION_VIEW);
  // i.setData(Uri.parse(url));
  // startActivity(i);

  Intent := TJIntent.Create;
  Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
  Intent.setData(TJnet_Uri.JavaClass.parse
    (StringToJString('http://www.danieleteti.it')));
  MainActivity.startActivity(Intent);
end;

Simple, isn’t it?

With this piece of code, you cann open your device browser to a web site (my blog in the snippet).

Click on the first button and you will be redirected to this blog.

Open browser with intent
Open browser with intent

 

Intents are REALLY powerfull

With the next snippet, you can send piece of data (e.g. text) to other apps. You dont have to know the apps in advance, Android will do the match with the Implicit Intent. (It is similat to the TShowShareSheet action).

procedure THeaderFooterForm.Button2Click(Sender: TObject);
var
  Intent: JIntent;
begin
  // Intent intent = new Intent(Intent.ACTION_SEND);
  // intent.setType("text/plain");
  // intent.putExtra(android.content.Intent.EXTRA_TEXT, "Android Rocks!!!");
  // startActivity(intent);

  Intent := TJIntent.Create;
  Intent.setType(StringToJString('text/plain'));
  Intent.setAction(TJIntent.JavaClass.ACTION_SEND);
  Intent.putExtra(TJIntent.JavaClass.EXTRA_TEXT,
    StringToJString('Delphi Rocks!!!'));
  MainActivity.startActivity(Intent);
end;
Chose wich apps shoud be launched to handle the intent (and the text)
Chose wich apps shoud be launched to handle the intent (and the text)

 

Now, if you choose Twitter, you will get this screen.

Intent handle by twitter app
Intent handle by twitter app

 

I’ve demos about placing a call, sending an SMS, show a PDF file, accessing different kind of storage etc.
In the next part of this mini series I’ll show some other snippets.

There is still many areas to explore…

  • Services
  • BroadcastReceiver
  • Widget
  • Bluetooth
  • I/O
  • …?

I’ll keep you informed…