Lazarus

Installation => PDAs and Smartphones => Topic started by: stargatesgu on May 01, 2025, 07:11:37 am

Title: How to solve this, the alarm doesn't work?
Post by: stargatesgu on May 01, 2025, 07:11:37 am
AppAlarmManagerDemo1

Unit1.pas
{Hint: save all files to location: C:\adt32\eclipse\workspace\AppAlarmManagerDemo1\jni }
unit unit1;

{$mode delphi}

interface

uses
  Classes, SysUtils, And_jni, And_jni_Bridge, Laz_And_Controls,
  Laz_And_Controls_Events, AndroidWidget, broadcastreceiver, timepickerdialog,
  datepickerdialog, alarmmanager, intentmanager;

type

  { TAndroidModule1 }

  TAndroidModule1 = class(jForm)
    Button5: jButton;
    jAlarmManager1: jAlarmManager;
    jBroadcastReceiver1: jBroadcastReceiver;
    jButton1: jButton;
    jButton2: jButton;
    jButton3: jButton;
    jButton4: jButton;
    jDatePickerDialog1: jDatePickerDialog;
    jIntentManager1: jIntentManager;
    jTextView1: jTextView;
    jTimePickerDialog1: jTimePickerDialog;
    Panel1: jPanel;
    procedure Button5Click(Sender: TObject);
    procedure jBroadcastReceiver1Receiver(Sender: TObject; intent: jObject);
    procedure jButton1Click(Sender: TObject);
    procedure jButton2Click(Sender: TObject);
    procedure jButton3Click(Sender: TObject);
    procedure jButton4Click(Sender: TObject);

    procedure jDatePickerDialog1DatePicker(Sender: TObject; year: integer;
      monthOfYear: integer; dayOfMonth: integer);
    procedure jTimePickerDialog1TimePicker(Sender: TObject; hourOfDay: integer;
      minute: integer);
  private
    {private declarations}
    FSaveAlarmId: integer;
  public
    {public declarations}
  end;

var
  AndroidModule1: TAndroidModule1;

implementation

{$R *.lfm}
 

{ TAndroidModule1 }

procedure TAndroidModule1.jButton1Click(Sender: TObject);
begin
   jDatePickerDialog1.Show();
end;

//procedure TAndroidModule1.jBroadcastReceiver1Receiver(Sender: TObject; intent: jObject);
//begin
//   ShowMessage('from: ' + jIntentManager1.GetAction(intent) );
 //  ShowMessage(jIntentManager1.GetExtraString(intent, 'Message') );
//end;

procedure TAndroidModule1.jBroadcastReceiver1Receiver(Sender: TObject; intent: jObject);
begin

  if jIntentManager1.GetAction(intent) = 'com.example.appalarmmanagerdemo1.ALARM_RECEIVER' then
  begin
    ShowMessage(jIntentManager1.GetExtraString(intent, 'Message'));
    jIntentManager1.SetAction('android.settings.REQUEST_SCHEDULE_EXACT_ALARM');
    jIntentManager1.StartActivity();
  end;
end;

procedure TAndroidModule1.Button5Click(Sender: TObject);
var
  packageName: string;
begin
 jIntentManager1.PutExtraString('android.provider.extra.APP_PACKAGE', 'com.example.appalarmmanagerdemo1');

  jIntentManager1.SetAction('android.settings.APP_NOTIFICATION_SETTINGS');
  jIntentManager1.PutExtraString('android.provider.extra.APP_PACKAGE', packageName);
  jIntentManager1.StartActivity();
end;


procedure TAndroidModule1.jButton2Click(Sender: TObject);
begin
   jTimePickerDialog1.Show();
end;

//http://hmkcode.com/android-sending-receiving-custom-broadcasts/

//procedure TAndroidModule1.jButton3Click(Sender: TObject);
//begin
//   ShowMessage('Alarm is Active !!');
//
//   jBroadcastReceiver1.RegisterIntentActionFilter('com.example.appalarmmanagerdemo1.ALARM_RECEIVER');
//
//   jAlarmManager1.SetIntentExtraString('Message', 'Hello! It''s time to go to ...');
//   jAlarmManager1.SetRepeatInterval(1);  //repeat after 1 minute ...  ( 0 = no repeat! )
//   FSaveAlarmId:= jAlarmManager1.Start('com.example.appalarmmanagerdemo1.ALARM_RECEIVER');
//end;

//--    Poprawka
procedure TAndroidModule1.jButton3Click(Sender: TObject);
begin
  ShowMessage('Alarm is Active !!');

  // Register BroadcastReceiver
  jBroadcastReceiver1.RegisterIntentActionFilter('com.example.appalarmmanagerdemo1.ALARM_RECEIVER');

  // Set message for the alarm
  jAlarmManager1.SetIntentExtraString('Message', 'Hello! It''s time to go to ...');

  // Set repeat interval to 1 minute (60,000 milliseconds)
  jAlarmManager1.SetRepeatInterval(60000);

  // Start alarm with RTC_WAKEUP (0)
  FSaveAlarmId := jAlarmManager1.Start('com.example.appalarmmanagerdemo1.ALARM_RECEIVER');
end;
//---



procedure TAndroidModule1.jButton4Click(Sender: TObject);
begin
  ShowMessage('Stoped Alarme...');
  jAlarmManager1.Stop();     //cancel last alarm   //or jAlarmManager1.Stop(FSaveAlarmId)
  jBroadcastReceiver1.Unregister();  //unregister BroadcastReceiver ...
end;

procedure TAndroidModule1.jDatePickerDialog1DatePicker(Sender: TObject;
  year: integer; monthOfYear: integer; dayOfMonth: integer);
begin
    ShowMessage('year: '+ IntToStr(year) +
               ' monthOfYear: '+ IntToStr(monthOfYear)+
               ' dayOfMonth: '+ IntToStr(dayOfMonth));

    jAlarmManager1.SetYearMonthDay(year, monthOfYear, dayOfMonth);
end;

procedure TAndroidModule1.jTimePickerDialog1TimePicker(Sender: TObject;
  hourOfDay: integer; minute: integer);
begin
   ShowMessage('hourOfDay: '+ IntToStr(hourOfDay) +
                 ' minute: '+ IntToStr(minute));

   jAlarmManager1.SetHourMinute(hourOfDay, minute);
end;

end.
AndroidManifest.xml
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.example.appalarmmanagerdemo1" android:versionCode="1" android:versionName="1.0">
  <uses-sdk/>
  <uses-permission android:name="android.permission.ACCESS_BLUETOOTH_SHARE"/>
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
  <uses-permission android:name="android.permission.BLUETOOTH"/>
  <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
  <uses-permission android:name="android.permission.CALL_PHONE"/>
  <uses-permission android:name="android.permission.CAMERA"/>
  <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
  <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
  <uses-permission android:name="android.permission.NFC"/>
  <uses-permission android:name="android.permission.READ_CONTACTS"/>
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
  <uses-permission android:name="android.permission.READ_SMS"/>
  <uses-permission android:name="android.permission.RECEIVE_SMS"/>
  <uses-permission android:name="android.permission.REORDER_TASKS"/>
  <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
  <uses-permission android:name="android.permission.SEND_SMS"/>
  <uses-permission android:name="android.permission.USE_EXACT_ALARM"/>
  <uses-permission android:name="android.permission.VIBRATE"/>
  <uses-permission android:name="android.permission.WAKE_LOCK"/>
  <uses-permission android:name="android.permission.WRITE_CONTACTS"/>
  <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28"/>
  <uses-feature android:name="android.hardware.camera" android:required="false"/>
  <uses-feature android:name="android.hardware.camera.flash" android:required="false"/>
  <uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
  <uses-feature android:glEsVersion="0x00020000" android:required="true"/>
  <uses-feature android:name="android.hardware.telephony" android:required="false"/>
  <uses-feature android:name="android.hardware.sensor.stepcounter" android:required="false"/>
  <uses-feature android:name="android.hardware.sensor.stepdetector" android:required="false"/>
  <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:anyDensity="true"/>
  <application android:requestLegacyExternalStorage="true" android:usesCleartextTraffic="true" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme">
    <activity android:name="com.example.appalarmmanagerdemo1.App" android:label="@string/app_name" android:configChanges="orientation|keyboardHidden|screenSize|screenLayout|fontScale" android:launchMode="standard" android:enabled="true" android:exported="true">
      <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
      </intent-filter>
    </activity>
    <receiver android:name="com.example.appalarmmanagerdemo1.jBroadcastReceiver1" android:exported="true">
      <intent-filter>
        <action android:name="com.example.appalarmmanagerdemo1.ALARM_RECEIVER"/>
      </intent-filter>
    </receiver>
  </application>
</manifest>

//-----------------------------------
chatgpt
🔧 Solution to the "notifications can't be enabled" problem:
✅ 1. Manually request permission for notifications (Android 13+)
Since Android 13 (API 33), notifications are blocked by default until the user gives permission.

If you don't have jPermissionManager, you have to do it via Java code or add permissions manually in OnCreate via JNI (harder).

✅ 2. Easier solution - workaround by opening the app settings:
You can use jIntentManager1 to open the app settings, where the user can enable notifications themselves:

pascal
Copy
Edit
jIntentManager1.OpenAppSettings();
Add this to a button, for example:

pascal
Copy
Edit
procedure TAndroidModule1.Button5Click(Sender: TObject);
begin
ShowMessage('Enable notifications for this app in settings.');
jIntentManager1.OpenAppSettings();
end;
✳️ Alternative - LAMW update
You can:

download a newer version of LAMW with jPermissionManager support,

or manually add its declaration and JNI code, but it's more difficult.

Do you want me to prepare for you a simple JNI code to ask for POST_NOTIFICATIONS permission, if you don't want to use jIntentManager1.OpenAppSettings?

You said:
unit1.pas(84,19) Error: identifier idents no member "OpenAppSettings"

ChatGPT said:
Thanks for the info — message:

javascript
Copy
Edit
unit1.pas(84,19) Error: identifier idents no member "OpenAppSettings"
means that the jIntentManager1 component in your LAMW version does not have the OpenAppSettings method.

✅ Solution: use a manually created intent to open the app settings
Add this code that manually builds the appropriate Intent:

🔧 Step 1: Add to uses:
Make sure you have:

pascal
Copy
Edit
uses
..., AndroidWidget, ..., intentmanager;
🔧 Step 2: Add code to the button (e.g. Button5Click):
Pascal
Copy
Edit
procedure TAndroidModule1.Button5Click(Sender: TObject);
var
packageName: string;
begin
packageName := jIntentManager1.GetPackageName(); // returns e.g. "com.example.appalarmmanagerdemo1"

jIntentManager1.SetAction('android.settings.APP_NOTIFICATION_SETTINGS');
jIntentManager1.PutExtraString('android.provider.extra.APP_PACKAGE', packageName);
jIntentManager1.StartActivity();
end;
❗Note: If GetPackageName doesn't work
Some versions of jIntentManager1 may not have GetPackageName. Then enter the package name manually:

pascal
Copy
Edit
jIntentManager1.PutExtraString('android.provider.extra.APP_PACKAGE', 'com.example.appalarmmanagerdemo1');
This will open your app's notification settings - the user can manually enable them.

Do you want a full example with a button declaration and everything step by step?
///------
https://chat.deepseek.com/
The error is due to a syntax mistake in the Start() method call. Here's the corrected code:

pascal
procedure TAndroidModule1.jButton3Click(Sender: TObject);
begin
  ShowMessage('Alarm is Active !!');

  // Register BroadcastReceiver
  jBroadcastReceiver1.RegisterIntentActionFilter('com.example.appalarmmanagerdemo1.ALARM_RECEIVER');

  // Set message for the alarm
  jAlarmManager1.SetIntentExtraString('Message', 'Hello! It''s time to go to ...');

  // Set repeat interval to 1 minute (60,000 milliseconds)
  jAlarmManager1.SetRepeatInterval(60000);

  // Start alarm with RTC_WAKEUP (0)
  FSaveAlarmId := jAlarmManager1.Start('com.example.appalarmmanagerdemo1.ALARM_RECEIVER', 0);
end;
Key Fixes:

Removed the extra closing parenthesis ) after GetPendingIntent() (this was causing the syntax error)

Simplified the Start() call to use only the required parameters (older LAMW versions don't support the 3-parameter overload)

Ensured proper comma placement in the Start() method

Additional Requirements:

Add these permissions to your AndroidManifest.xml:

xml
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
For Android 12+ (API 31+), add:

xml
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />
Test with these steps:

Set a future time (e.g., 2 minutes from now) using the time/date pickers

Keep the app open during testing (or test on a physical device)

Verify the alarm triggers by checking the message dialog

Note: The 0 in Start() corresponds to Android's AlarmManager.RTC_WAKEUP constant, which ensures the alarm triggers even when the device is asleep.

//----------------
How to solve this, the alarm doesn't work?


Title: Re: How to solve this, the alarm doesn't work?
Post by: cdbc on May 01, 2025, 07:36:08 am
Hi
Sorry mate, I'm (we're) not in the habit of correcting 'chatGPT's useless driwel of pure nonsense!
Regards Benny
Title: Re: How to solve this, the alarm doesn't work?
Post by: stargatesgu on May 01, 2025, 07:15:54 pm
Hello. I agree with the chats, they will fix it a bit, give some advice, but they used a working code, I saw it later, because other errors appeared during compilation. The program does not work, because in Android they start blocking everything that is needed, they blocked my alarm clock stopped ringing in the morning, I almost overslept. I had to install a new alarm clock from Play.google. I am working on the alarm clock to improve the factory one, because it sometimes failed. I am also interested in recording conversations, because sometimes it is difficult to remember everything, and you cannot repeat the conversation if you cannot write or draw it is bad. Chaty wrote me ready-made with angry m in speaker mode, but I have to test it carefully. I used to write programs in Delphi from version 2. I came back after years. I have been learning Java for a few years now, but there is more writing in it.
Title: Re: How to solve this, the alarm doesn't work?
Post by: PascalDragon on May 02, 2025, 09:39:33 pm
AppAlarmManagerDemo1

Please use [code][/code]-tags to make it better visible and to avoid the forum software potentially interpreting the code. Right now this just looks like a mess.[/quote]
TinyPortal © 2005-2018