I can setup my android app to start at specified time with Android AlarmManager. It works nice, I used this manual and this forum for details (use google translator).
So I'm creating DEX file from Java code (from XE7 you can simply attach JAR without creating dex(!)). To detect if My app was started from AlarmManager I decided to put boolean var to intent, using this java manual from stackoverflow, so I added to java code this line:
TestLauncher.putExtra("StartedFromAM", true);
Full Java code, that will be compiled to jar (or dex):
package com.TestReceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AlarmReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Intent TestLauncher = new Intent();
TestLauncher.setClassName(context, "com.embarcadero.firemonkey.FMXNativeActivity");
TestLauncher.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
TestLauncher.putExtra("StartedFromAM", true);
context.startActivity(TestLauncher);
}
}
So now in theory my Intent has StartedFromAM argument.
It compiles to dex file ok, the problem is I can't read this boolean from Delphi code - it is always = 0 (false).
I tried this:
ShowMessage(SharedActivity.getIntent.getBooleanExtra(StringToJString('StartedFromAM'), false).ToString);
ShowMessage(MainActivity.getIntent.getBooleanExtra(StringToJString('StartedFromAM'), false).ToString);
Update 1
Thanks to Mr. Blong I already got right solution on how to detect that my activity was started from Android AlarmManager, this update is just about PutExtra in Java Code and reading extra value from intent from Delphi. You can skip it if you want.
Now I know that I can connect JAR files (I have Berlin upd 2), without creating dex.
So I created new jar file - which full java code I showed above.
Then I created new project, and setup alarm with this code
procedure SetAlarmWakeUpAdnroid(aSeconds: integer);
function getTimeAfterInSecs(Seconds: Integer): Int64;
var
Calendar: JCalendar;
begin
Calendar := TJCalendar.JavaClass.getInstance;
Calendar.add(TJCalendar.JavaClass.SECOND, Seconds);
Result := Calendar.getTimeInMillis;
end;
var
Intent: JIntent;
PendingIntent: JPendingIntent;
vRes: boolean;
begin
Intent := TJIntent.Create;
Intent.setClassName(TAndroidHelper.Context, StringToJString('com.embarcadero.firemonkey.FMXNativeActivity'));
PendingIntent := TJPendingIntent.JavaClass.getActivity(TAndroidHelper.Context, 1, Intent, 0);
TAndroidHelper.AlarmManager.&set(TJAlarmManager.JavaClass.RTC_WAKEUP, getTimeAfterInSecs(30),
PendingIntent);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
SetAlarmWakeUpAdnroid(30);
end;
Then I added OnFormCreate event
procedure TForm1.FormCreate(Sender: TObject);
begin
ShowMessage('StartedFromAM =' + TAndroidHelper.Activity.getIntent.getBooleanExtra(StringToJString('StartedFromAM'), false).ToString);
ShowMessage('EXTRA_ALARM_COUNT = ' + TAndroidHelper.Activity.getIntent.getIntExtra(TJIntent.JavaClass.EXTRA_ALARM_COUNT, 0).ToString);
end;
When Alarm manager started my app, I got EXTRA_ALARM_COUNT = 1 and StartedFromAM = 0.
See Question&Answers more detail:
os