FMX.Android Intents Sample

This demo shows interaction between Android applications using intents. It consists of two applications, SendIntent that creates and send the intent, and ReceiveIntent that receives the intent and displays the information.

Note:ReceiveIntent needs to be on the device to receive the broadcast information.

Select SendIntent on the Project Manager.

Select the Android device on the Project Manager.

Press F9 or choose Run > Run.

Note: Send both applications to the same Android device, otherwise you need to use push notifications.

Implementation

SendIntent

SendIntent project has one source file, Unit1.pas.

SendIntent application sends text using an intent. It uses the SendTextViaIntent procedure to create the intent object and start the activity for this intent.

procedureTForm1.SendTextViaIntent(constAText:string);varIntent:JIntent;//Declares the intent objectbeginIntent:=TJIntent.Create;Intent.setType(StringToJString('text/pas'));// Defines the data string.Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);//Defines the Action.Intent.putExtra(TJIntent.JavaClass.EXTRA_TEXT,StringtoJString(AText));ifMainActivity.getPackageManager.queryIntentActivities(Intent,TJPackageManager.JavaClass.MATCH_DEFAULT_ONLY).size>0then//Checks if there is at least one application capable of receiving the intent.MainActivity.startActivity(Intent);//Calls startActivity() to send the intent to the system.elseShowMessage('Receiver not found');end;

The primary pieces of information in an intent are:

Action: the general action to be performed. In our sample, ACTION_VIEW.

Data: the data to operate on, expressed as Uri. In our sample, 'text/pas'.

TButton1 sends the text to write a simple Hello World Object Pascal application. It calls SendTextViaIntent procedure with the text as a parameter, on the OnClick event.

TButton2 sends the structure of a Object Pascal .pas file in the same way as TButton1.

How Android Handles Incoming Intents

No app: If there are no apps on the device that can receive the implicit intent, the app crashes when it calls startActivity().

Note: First verify that there is an app that can receive the intent.

One app: When there is only one app that can handle the intent, the system immediately starts it.

More than one app: If there is more than one app that can handle the activity, the system displays a dialog for the user to select which app to use.

ReceiveIntent

ReceiveIntent project has one source file, Unit1.pas. It receives and shows the text from the SendIntent application.

HandleIntentAction function verifies that the intent contains information and shows the text on the TMemo1.

HandleAppEvent function verifies that the intent is not null and calls HandleIntentAction function with the received itent. This function handles the intent the first time ReceiveIntent opens.

ReceiveIntent procedure handles the intent when ReceiveIntent is already open.

The itent is register using MainActivity.registerIntentAction(TJIntent.JavaClass.ACTION_VIEW);.

To receive simple data from other applications, you need to update the Android Manifest file to create the intent filters that are necessary to receive intents for a specific action.

After editing the Android Manifest for a specific action, when SendIntent application tries to share the information passing the intent to startActivity(), ReceiveIntent appears as the option to view this information. If more than one options is available, an app chooser with all the apps appears.

Modifying the AndroidManifest.xml

Modify the Android Manifest xml file to add the intent filter. An intent filter informs the Android system about what intents the application can accept.

In this example, an intent filter is added to accept the intent created on the SendIntent application. The ReceiveIntent is the only application that can accept the intent from SendIntent application.

The application that receives the intent needs to add some tags to the Android Manifest.

After building your project for the first time with Android selected on the Project Manager, the AndroidManifest.template.xml is added to your project folder.

Edit the AndroidManifest.template.xml file to add the following lines:

<!--<%application-meta-data%>... <activity android:name="com.embarcadero.firemonkey.FMXNativeActivity" android:label="%activityLabel%" android:configChanges="orientation|keyboard|keyboardHidden" android:launchMode="singleTask"> ...--><intent-filter>
...
<actionandroid:name="android.intent.action.VIEW"/><!--Constant Value for the ACTION_VIEW--><categoryandroid:name="android.intent.category.DEFAULT"/><!-- Activities that want to receive implicit intents must include this constant--><categoryandroid:name="android.intent.category.BROWSABLE"/><dataandroid:mimeType="text/pas"/><!--Sets the MIME type of your intent.--></intent-filter><!--</activity>-->