Recent

Author Topic: LAMW Timer service  (Read 654 times)

Segator

  • Jr. Member
  • **
  • Posts: 98
    • https://github.com/Nenirey
LAMW Timer service
« on: September 13, 2020, 09:17:51 pm »
Hi @jmpessoa, it's possible to implement a jtimerservice based on jtimer?, the proposed is keep the app running and do general tasks in the background usind the broadcast from the timer service like jdownloadservice but continuously.
i am Reinier, Nenirey and Segator :) https://github.com/Nenirey

jmpessoa

  • Hero Member
  • *****
  • Posts: 1623
Re: LAMW Timer service
« Reply #1 on: September 13, 2020, 09:39:04 pm »

Can you, please,  point out some java/android example?
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Segator

  • Jr. Member
  • **
  • Posts: 98
    • https://github.com/Nenirey
Re: LAMW Timer service
« Reply #2 on: September 13, 2020, 11:08:54 pm »
i think that it's possible using de jdownloadservice service and replace the downloaded funtion with the jtimer component, i don't see any example but you can based on jdownloadservice not download anything only send a broadcast with the ontimer event.
i am Reinier, Nenirey and Segator :) https://github.com/Nenirey

Segator

  • Jr. Member
  • **
  • Posts: 98
    • https://github.com/Nenirey
Re: LAMW Timer service
« Reply #3 on: September 14, 2020, 04:25:21 pm »
Hi i found this example on https://gist.github.com/mjohnsullivan/403149218ecb480e7759

Example of how to create a long running timer service that survives activity destruction by moving to the foreground, and back to the background when a new activity bind to it.

Code: Pascal  [Select][+][-]
  1. //
  2. //  Copyright 2015  Google Inc. All Rights Reserved.
  3. //
  4. //    Licensed under the Apache License, Version 2.0 (the "License");
  5. //    you may not use this file except in compliance with the License.
  6. //    You may obtain a copy of the License at
  7. //
  8. //        http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. //    Unless required by applicable law or agreed to in writing, software
  11. //    distributed under the License is distributed on an "AS IS" BASIS,
  12. //    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. //    See the License for the specific language governing permissions and
  14. //    limitations under the License.
  15.  
  16. package com.example.google.timerservice;
  17.  
  18. import android.app.Notification;
  19. import android.app.PendingIntent;
  20. import android.app.Service;
  21. import android.content.ComponentName;
  22. import android.content.Intent;
  23. import android.content.ServiceConnection;
  24. import android.os.Binder;
  25. import android.os.Bundle;
  26. import android.os.Handler;
  27. import android.os.IBinder;
  28. import android.os.Message;
  29. import android.support.v4.app.NotificationCompat;
  30. import android.support.v7.app.AppCompatActivity;
  31. import android.util.Log;
  32. import android.view.View;
  33. import android.widget.Button;
  34. import android.widget.TextView;
  35.  
  36. import java.lang.ref.WeakReference;
  37.  
  38. /**
  39.  * Example activity to manage a long-running timer, which survives the destruction of the activity
  40.  * by using a foreground service and notification
  41.  *
  42.  * Add the following to the manifest:
  43.  * <service android:name=".MainActivity$TimerService" android:exported="false" />
  44.  */
  45.  
  46. public class TimerActivity extends AppCompatActivity {
  47.  
  48.     private static final String TAG = TimerActivity.class.getSimpleName();
  49.  
  50.     private TimerService timerService;
  51.     private boolean serviceBound;
  52.  
  53.     private Button timerButton;
  54.     private TextView timerTextView;
  55.  
  56.     // Handler to update the UI every second when the timer is running
  57.     private final Handler mUpdateTimeHandler = new UIUpdateHandler(this);
  58.    
  59.     // Message type for the handler
  60.     private final static int MSG_UPDATE_TIME = 0;
  61.  
  62.     protected void onCreate(Bundle savedInstanceState) {
  63.         super.onCreate(savedInstanceState);
  64.         setContentView(R.layout.activity_main);
  65.  
  66.         timerButton = (Button)findViewById(R.id.timer_button);
  67.         timerTextView = (TextView)findViewById(R.id.timer_text_view);
  68.     }
  69.  
  70.     @Override
  71.     protected void onStart() {
  72.         super.onStart();
  73.         if (Log.isLoggable(TAG, Log.VERBOSE)) {
  74.             Log.v(TAG, "Starting and binding service");
  75.         }
  76.         Intent i = new Intent(this, TimerService.class);
  77.         startService(i);
  78.         bindService(i, mConnection, 0);
  79.     }
  80.  
  81.     @Override
  82.     protected void onStop() {
  83.         super.onStop();
  84.         updateUIStopRun();
  85.         if (serviceBound) {
  86.             // If a timer is active, foreground the service, otherwise kill the service
  87.             if (timerService.isTimerRunning()) {
  88.                 timerService.foreground();
  89.             }
  90.             else {
  91.                 stopService(new Intent(this, TimerService.class));
  92.             }
  93.             // Unbind the service
  94.             unbindService(mConnection);
  95.             serviceBound = false;
  96.         }
  97.     }
  98.  
  99.     public void runButtonClick(View v) {
  100.         if (serviceBound && !timerService.isTimerRunning()) {
  101.             if (Log.isLoggable(TAG, Log.VERBOSE)) {
  102.                 Log.v(TAG, "Starting timer");
  103.             }
  104.             timerService.startTimer();
  105.             updateUIStartRun();
  106.         }
  107.         else if (serviceBound && timerService.isTimerRunning()) {
  108.             if (Log.isLoggable(TAG, Log.VERBOSE)) {
  109.                 Log.v(TAG, "Stopping timer");
  110.             }
  111.             timerService.stopTimer();
  112.             updateUIStopRun();
  113.         }
  114.     }
  115.  
  116.     /**
  117.      * Updates the UI when a run starts
  118.      */
  119.     private void updateUIStartRun() {
  120.         mUpdateTimeHandler.sendEmptyMessage(MSG_UPDATE_TIME);
  121.         timerButton.setText(R.string.timer_stop_button);
  122.     }
  123.  
  124.     /**
  125.      * Updates the UI when a run stops
  126.      */
  127.     private void updateUIStopRun() {
  128.         mUpdateTimeHandler.removeMessages(MSG_UPDATE_TIME);
  129.         timerButton.setText(R.string.timer_start_button);
  130.     }
  131.  
  132.     /**
  133.      * Updates the timer readout in the UI; the service must be bound
  134.      */
  135.     private void updateUITimer() {
  136.         if (serviceBound) {
  137.             timerTextView.setText(timerService.elapsedTime() + " seconds");
  138.         }
  139.     }
  140.  
  141.     /**
  142.      * Callback for service binding, passed to bindService()
  143.      */
  144.     private ServiceConnection mConnection = new ServiceConnection() {
  145.  
  146.         @Override
  147.         public void onServiceConnected(ComponentName className, IBinder service) {
  148.             if (Log.isLoggable(TAG, Log.VERBOSE)) {
  149.                 Log.v(TAG, "Service bound");
  150.             }
  151.             TimerService.RunServiceBinder binder = (TimerService.RunServiceBinder) service;
  152.             timerService = binder.getService();
  153.             serviceBound = true;
  154.             // Ensure the service is not in the foreground when bound
  155.             timerService.background();
  156.             // Update the UI if the service is already running the timer
  157.             if (timerService.isTimerRunning()) {
  158.                 updateUIStartRun();
  159.             }
  160.         }
  161.  
  162.         @Override
  163.         public void onServiceDisconnected(ComponentName name) {
  164.             if (Log.isLoggable(TAG, Log.VERBOSE)) {
  165.                 Log.v(TAG, "Service disconnect");
  166.             }
  167.             serviceBound = false;
  168.         }
  169.     };
  170.  
  171.     /**
  172.      * When the timer is running, use this handler to update
  173.      * the UI every second to show timer progress
  174.      */
  175.     static class UIUpdateHandler extends Handler {
  176.  
  177.         private final static int UPDATE_RATE_MS = 1000;
  178.         private final WeakReference<TimerActivity> activity;
  179.  
  180.         UIUpdateHandler(TimerActivity activity) {
  181.             this.activity = new WeakReference<>(activity);
  182.         }
  183.  
  184.         @Override
  185.         public void handleMessage(Message message) {
  186.             if (MSG_UPDATE_TIME == message.what) {
  187.                 if (Log.isLoggable(TAG, Log.VERBOSE)) {
  188.                     Log.v(TAG, "updating time");
  189.                 }
  190.                 activity.get().updateUITimer();
  191.                 sendEmptyMessageDelayed(MSG_UPDATE_TIME, UPDATE_RATE_MS);
  192.             }
  193.         }
  194.     }
  195.  
  196.     /**
  197.      * Timer service tracks the start and end time of timer; service can be placed into the
  198.      * foreground to prevent it being killed when the activity goes away
  199.      */
  200.     public static class TimerService extends Service {
  201.  
  202.         private static final String TAG = TimerService.class.getSimpleName();
  203.  
  204.         // Start and end times in milliseconds
  205.         private long startTime, endTime;
  206.  
  207.         // Is the service tracking time?
  208.         private boolean isTimerRunning;
  209.  
  210.         // Foreground notification id
  211.         private static final int NOTIFICATION_ID = 1;
  212.  
  213.         // Service binder
  214.         private final IBinder serviceBinder = new RunServiceBinder();
  215.  
  216.         public class RunServiceBinder extends Binder {
  217.             TimerService getService() {
  218.                 return TimerService.this;
  219.             }
  220.         }
  221.  
  222.         @Override
  223.         public void onCreate() {
  224.             if (Log.isLoggable(TAG, Log.VERBOSE)) {
  225.                 Log.v(TAG, "Creating service");
  226.             }
  227.             startTime = 0;
  228.             endTime = 0;
  229.             isTimerRunning = false;
  230.         }
  231.  
  232.         @Override
  233.         public int onStartCommand(Intent intent, int flags, int startId) {
  234.             if (Log.isLoggable(TAG, Log.VERBOSE)) {
  235.                 Log.v(TAG, "Starting service");
  236.             }
  237.             return Service.START_STICKY;
  238.         }
  239.  
  240.         @Override
  241.         public IBinder onBind(Intent intent) {
  242.             if (Log.isLoggable(TAG, Log.VERBOSE)) {
  243.                 Log.v(TAG, "Binding service");
  244.             }
  245.             return serviceBinder;
  246.         }
  247.  
  248.         @Override
  249.         public void onDestroy() {
  250.             super.onDestroy();
  251.             if (Log.isLoggable(TAG, Log.VERBOSE)) {
  252.                 Log.v(TAG, "Destroying service");
  253.             }
  254.         }
  255.  
  256.         /**
  257.          * Starts the timer
  258.          */
  259.         public void startTimer() {
  260.             if (!isTimerRunning) {
  261.                 startTime = System.currentTimeMillis();
  262.                 isTimerRunning = true;
  263.             }
  264.             else {
  265.                 Log.e(TAG, "startTimer request for an already running timer");
  266.             }
  267.         }
  268.  
  269.         /**
  270.          * Stops the timer
  271.          */
  272.         public void stopTimer() {
  273.             if (isTimerRunning) {
  274.                 endTime = System.currentTimeMillis();
  275.                 isTimerRunning = false;
  276.             }
  277.             else {
  278.                 Log.e(TAG, "stopTimer request for a timer that isn't running");
  279.             }
  280.         }
  281.  
  282.         /**
  283.          * @return whether the timer is running
  284.          */
  285.         public boolean isTimerRunning() {
  286.             return isTimerRunning;
  287.         }
  288.  
  289.         /**
  290.          * Returns the  elapsed time
  291.          *
  292.          * @return the elapsed time in seconds
  293.          */
  294.         public long elapsedTime() {
  295.             // If the timer is running, the end time will be zero
  296.             return endTime > startTime ?
  297.                     (endTime - startTime) / 1000 :
  298.                     (System.currentTimeMillis() - startTime) / 1000;
  299.         }
  300.  
  301.         /**
  302.          * Place the service into the foreground
  303.          */
  304.         public void foreground() {
  305.             startForeground(NOTIFICATION_ID, createNotification());
  306.         }
  307.  
  308.         /**
  309.          * Return the service to the background
  310.          */
  311.         public void background() {
  312.             stopForeground(true);
  313.         }
  314.  
  315.         /**
  316.          * Creates a notification for placing the service into the foreground
  317.          *
  318.          * @return a notification for interacting with the service when in the foreground
  319.          */
  320.         private Notification createNotification() {
  321.             NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
  322.                     .setContentTitle("Timer Active")
  323.                     .setContentText("Tap to return to the timer")
  324.                     .setSmallIcon(R.mipmap.ic_launcher);
  325.  
  326.             Intent resultIntent = new Intent(this, TimerActivity.class);
  327.             PendingIntent resultPendingIntent =
  328.                     PendingIntent.getActivity(this, 0, resultIntent,
  329.                             PendingIntent.FLAG_UPDATE_CURRENT);
  330.             builder.setContentIntent(resultPendingIntent);
  331.  
  332.             return builder.build();
  333.         }
  334.     }
  335.  
  336. }
i am Reinier, Nenirey and Segator :) https://github.com/Nenirey

Segator

  • Jr. Member
  • **
  • Posts: 98
    • https://github.com/Nenirey
Re: LAMW Timer service
« Reply #4 on: September 15, 2020, 11:23:29 pm »
i am go to try modify the jtimer with the jdownloadservice method and class to improve a jtimerservice  :-[
i am Reinier, Nenirey and Segator :) https://github.com/Nenirey

jmpessoa

  • Hero Member
  • *****
  • Posts: 1623
Re: LAMW Timer service
« Reply #5 on: September 16, 2020, 04:33:41 am »

Yes,

mimic "jdownloadservice" can be more easy!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Segator

  • Jr. Member
  • **
  • Posts: 98
    • https://github.com/Nenirey
Re: LAMW Timer service
« Reply #6 on: September 17, 2020, 03:53:31 pm »
I make some progress, i have a concept test attached here, but i have some questions, when you use other service like USSD service and do this:

Code: Pascal  [Select][+][-]
  1. jBroadcastReceiver1.Unregister();

the service is stopped?, auto destroy? or just you no listen then anymore?.
i am Reinier, Nenirey and Segator :) https://github.com/Nenirey

jmpessoa

  • Hero Member
  • *****
  • Posts: 1623
Re: LAMW Timer service
« Reply #7 on: September 17, 2020, 06:59:17 pm »

Quote
or just you no listen then anymore?.

Yes.
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Segator

  • Jr. Member
  • **
  • Posts: 98
    • https://github.com/Nenirey
Re: LAMW Timer service
« Reply #8 on: October 13, 2020, 07:54:21 pm »
Hi all, i continue to trying implement a timer service for lamw, i make some progress but in the wrong direction, i finish the componet but when you using the class IntentService that is not a realy goog service to work in the background with a timer, it work but when you remove the main application from the recent running app the service stop, we must use the extends Service or the JobIntentService now hi is the problem:

jTimerService.java
Code: Pascal  [Select][+][-]
  1. package com.example.apptimerservicedemo1;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.util.Random;
  5. import android.app.PendingIntent;
  6. import android.app.Service;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.os.Environment;
  10. import android.os.Handler;
  11. import android.os.Build;
  12. import android.util.Log;
  13. import android.os.Binder;
  14. import android.os.IBinder;
  15. import android.widget.Toast;
  16.  
  17.  
  18. /*Draft java code by "Lazarus Android Module Wizard" [5/26/2016 20:15:41]*/
  19. /*https://github.com/jmpessoa/lazandroidmodulewizard*/
  20. /*jControl template*/
  21.  
  22. public class jTimerService extends Service {
  23.  
  24.    private long     pascalObj = 0;      // Pascal Object
  25.    private Controls controls  = null;   // Control Class -> Java/Pascal Interface ...
  26.    private Context  context   = null;
  27.    private int result = 0; //0 = fail;  -1 = ok!
  28.    private String mIntentAction;
  29.  
  30.    @Override
  31.    public IBinder onBind(Intent intent) {
  32.         return null;
  33.    }
  34.  
  35.    @Override
  36.    public void onCreate() {
  37.         Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
  38.  
  39.         //myPlayer = MediaPlayer.create(this, R.raw.sun);
  40.         //myPlayer.setLooping(false); // Set looping
  41.    }
  42.  
  43.    @Override
  44.    public void onStart(Intent intent, int startid) {
  45.         Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
  46.         //myPlayer.start();
  47.  
  48.    }
  49.  
  50.    @Override
  51.    public void onDestroy() {
  52.         Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
  53.         //myPlayer.stop();
  54.    }
  55.  
  56.    @Override
  57.    public int onStartCommand(Intent intent, int flags, int startId) {
  58.    Toast.makeText(controls.activity, "StartCommand",Toast.LENGTH_SHORT).show();
  59.        //super.onStartCommand(intent, flags, startId);
  60.        //startTimer();
  61.        //SetEnabled(true);
  62.        return START_STICKY;
  63.    }
  64.  
  65.    //GUIDELINE: please, preferentially, init all yours params names with "_", ex: int _flag, String _hello ...
  66.    public jTimerService(Controls _ctrls, long _Self) { //Add more others news "_xxx" params if needed!
  67.       //super("jTimerService");
  68.           //super(_ctrls.activity);
  69.       super();
  70.       context   = _ctrls.activity;
  71.       pascalObj = _Self;
  72.       controls  = _ctrls;
  73.    }
  74.  
  75.    public jTimerService() {
  76.    }
  77.  
  78.    public void jFree() {
  79.      //free local objects...
  80.    }
  81.  
  82.    //write others [public] methods code here......
  83.    //GUIDELINE: please, preferentially, init all yours params names with "_", ex: int _flag, String _hello ...
  84.  
  85.    //////////////////////////////////timer////////////////////////////////
  86.  
  87.    //Property
  88.    private boolean         mEnabled  = false;  // default : false
  89.    private int             mInterval = 1000;   // 1000msec
  90.  
  91.    //Java Object
  92.    private Handler      mHandler  = null;
  93.    private RunnableStop mRunnable = null;
  94.  
  95.    public class RunnableStop implements Runnable {
  96.  
  97.       boolean isEnabled;
  98.  
  99.       public RunnableStop() {
  100.         isEnabled = true;
  101.       }
  102.  
  103.       @Override
  104.       public void run() {
  105.  
  106.         if( !isEnabled ) return;
  107.  
  108.         //Start("", mIntentAction);
  109.  
  110.            //publishResults( -1, mIntentAction, (System.currentTimeMillis()));
  111.  
  112.           if ((mHandler != null) && isEnabled)
  113.                 mHandler.postDelayed(mRunnable, mInterval);
  114.  
  115.       }
  116.  
  117.       public void disable() {
  118.           isEnabled = false;
  119.       }
  120.    }
  121.    ///////////////////////////////////////////////////////////////////////
  122.  
  123.    //@Override
  124.    //protected void onHandleIntent(Intent intent) {   // will be called asynchronously by Android
  125.    //       //TODO Auto-generated method stub
  126.           //  //String urlPath = intent.getStringExtra("URL");
  127.           //  //String fileName = intent.getStringExtra("FILENAME");
  128.           //  //String filePath = intent.getStringExtra("PATH");
  129.           //  String iaction = intent.getStringExtra("ACTION");
  130.    //
  131.    //        long startTime = System.currentTimeMillis();
  132.    //
  133.           // result = -1; //Activity.RESULT_OK;
  134.    //
  135.           // startTime = System.currentTimeMillis();
  136.    //
  137.           // publishResults( result, iaction, (System.currentTimeMillis() - startTime));
  138.    //}
  139.  
  140.    public void publishResults( int result, String _action, long time) {
  141.  
  142.            //Implicit intents specify the action which should be performed and optionally data which
  143.            //provides content for the action.
  144.  
  145.             Intent intent = new Intent(_action);
  146.  
  147.             if  (result == -1)
  148.                intent.putExtra("Result", "RESULT_OK");
  149.             else
  150.                intent.putExtra("Result", "RESULT_CANCELED");
  151.             intent.putExtra("ElapsedTimeInSeconds", (int)time/1000); //long
  152.             sendBroadcast(intent);
  153.    }
  154.  
  155.    //Method to start the service
  156.    public void Start(String _intentActionNotification, int flags, int startId) {
  157.              //mURL = _urlString;
  158.              mIntentAction = _intentActionNotification;
  159.              mHandler  = new Handler();
  160.              //SetEnabled(true);
  161.              //Explicit intents explicitly define the component which should be called by the Android system,
  162.              //by using the Java class as identifier
  163.  
  164.              //Create an intent for a specific component.
  165.              Intent intent = new Intent(context, jTimerService.class);
  166.              //intent.putExtra("FILENAME", mFileSaveAs); //
  167.              //intent.putExtra("URL", mURL);
  168.              //intent.putExtra("PATH", mFILEPATH);
  169.              intent.putExtra("ACTION", mIntentAction);
  170.              controls.activity.startService(intent);
  171.              //getApplicationContext().startService(intent);
  172.              //startService(intent);
  173.              //context.onStartCommand(intent);
  174.    }
  175.  
  176.    public  void SetInterval(int interval) {
  177.             mInterval = interval;
  178.    }
  179.    public  void SetEnabled(boolean _enabled) {
  180.          if (mEnabled == _enabled) return;
  181.  
  182.          mEnabled = _enabled;
  183.  
  184.          if (mEnabled) {
  185.           if( mRunnable == null ){
  186.                 mRunnable = new RunnableStop();
  187.             mHandler.postDelayed(mRunnable, mInterval);
  188.           }
  189.          } else{
  190.  
  191.           if( mRunnable != null ){
  192.                   mRunnable.disable();
  193.                   mRunnable = null;
  194.                   mHandler.removeCallbacksAndMessages(null);
  195.           }
  196.          }
  197.  
  198.   }
  199.  
  200. }
  201.  

timerservice.pas
Code: Pascal  [Select][+][-]
  1. unit timerservice;
  2.  
  3. {$mode delphi}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, And_jni, AndroidWidget;
  9.  
  10. type
  11.  
  12. {Draft Component code by "Lazarus Android Module Wizard" [5/26/2016 23:50:23]}
  13. {https://github.com/jmpessoa/lazandroidmodulewizard}
  14.  
  15. {jControl template}
  16.  
  17. jTimerService = class(jControl)
  18.  private
  19.  
  20.  public
  21.     constructor Create(AOwner: TComponent); override;
  22.     destructor  Destroy; override;
  23.     procedure Init(refApp: jApp); override;
  24.     function jCreate(): jObject;
  25.     procedure jFree();
  26.     procedure Start(_intentActionNotification: string; _flags:integer; _startId:integer);
  27.     procedure SetEnabled(_enable:boolean);
  28.     procedure SetInterval(_interval:integer);
  29.  
  30.  published
  31.  
  32. end;
  33.  
  34. function jTimerService_jCreate(env: PJNIEnv;_Self: int64; this: jObject): jObject;
  35. procedure jTimerService_jFree(env: PJNIEnv; _jtimerservice: JObject);
  36. procedure jTimerService_Start(env: PJNIEnv; _jtimerservice: JObject;_intentAction: string; _flags:integer; _startId:integer);
  37. procedure jTimerService_SetEnabled(env: PJNIEnv; _jtimerservice: JObject; _enable: boolean);
  38. procedure jTimerService_SetInterval(env: PJNIEnv; _jtimerservice: JObject; _interval: integer);
  39.  
  40.  
  41. implementation
  42.  
  43.  
  44. {---------  jTimerService  --------------}
  45.  
  46. constructor jTimerService.Create(AOwner: TComponent);
  47. begin
  48.   inherited Create(AOwner);
  49. //your code here....
  50. end;
  51.  
  52. destructor jTimerService.Destroy;
  53. begin
  54.   if not (csDesigning in ComponentState) then
  55.   begin
  56.      if FjObject <> nil then
  57.      begin
  58.        jFree();
  59.        FjObject:= nil;
  60.      end;
  61.   end;
  62.   //you others free code here...'
  63.   inherited Destroy;
  64. end;
  65.  
  66. procedure jTimerService.Init(refApp: jApp);
  67. begin
  68.   if FInitialized  then Exit;
  69.   inherited Init(refApp); //set default ViewParent/FjPRLayout as jForm.View!
  70.   //your code here: set/initialize create params....
  71.   FjObject := jCreate(); if FjObject = nil then exit;
  72.   FInitialized:= True;
  73. end;
  74.  
  75.  
  76. function jTimerService.jCreate(): jObject;
  77. begin
  78.    Result:= jTimerService_jCreate(FjEnv, int64(Self), FjThis);
  79. end;
  80.  
  81. procedure jTimerService.jFree();
  82. begin
  83.   //in designing component state: set value here...
  84.   if FInitialized then
  85.      jTimerService_jFree(FjEnv, FjObject);
  86. end;
  87.  
  88. procedure jTimerService.Start(_intentActionNotification: string; _flags:integer; _startId:integer);
  89. begin
  90.   //in designing component state: set value here...
  91.   if FInitialized then
  92.      jTimerService_Start(FjEnv, FjObject,_intentActionNotification, _flags, _startId);
  93. end;
  94.  
  95. procedure jTimerService.SetEnabled(_enable: boolean);
  96. begin
  97.   //in designing component state: set value here...
  98.   if FInitialized then
  99.      jTimerService_SetEnabled(FjEnv, FjObject, _enable);
  100. end;
  101.  
  102. procedure jTimerService.SetInterval(_interval: integer);
  103. begin
  104.   //in designing component state: set value here...
  105.   if FInitialized then
  106.      jTimerService_SetInterval(FjEnv, FjObject, _interval);
  107. end;
  108.  
  109. {-------- jTimerService_JNI_Bridge ----------}
  110.  
  111. function jTimerService_jCreate(env: PJNIEnv;_Self: int64; this: jObject): jObject;
  112. var
  113.   jParams: array[0..0] of jValue;
  114.   jMethod: jMethodID=nil;
  115.   jCls: jClass=nil;
  116. begin
  117.   jParams[0].j:= _Self;
  118.   jCls:= Get_gjClass(env);
  119.   jMethod:= env^.GetMethodID(env, jCls, 'jTimerService_jCreate', '(J)Ljava/lang/Object;');
  120.   Result:= env^.CallObjectMethodA(env, this, jMethod, @jParams);
  121.   Result:= env^.NewGlobalRef(env, Result);
  122. end;
  123.  
  124. (*
  125. //Please, you need insert:
  126.  
  127.    public java.lang.Object jTimerService_jCreate(long _Self) {
  128.       return (java.lang.Object)(new jTimerService(this,_Self));
  129.    }
  130.  
  131. //to end of "public class Controls" in "Controls.java"
  132. *)
  133.  
  134.  
  135. procedure jTimerService_jFree(env: PJNIEnv; _jtimerservice: JObject);
  136. var
  137.   jMethod: jMethodID=nil;
  138.   jCls: jClass=nil;
  139. begin
  140.   jCls:= env^.GetObjectClass(env, _jtimerservice);
  141.   jMethod:= env^.GetMethodID(env, jCls, 'jFree', '()V');
  142.   env^.CallVoidMethod(env, _jtimerservice, jMethod);
  143.   env^.DeleteLocalRef(env, jCls);
  144. end;
  145.  
  146.  
  147. procedure jTimerService_Start(env: PJNIEnv; _jtimerservice: JObject; _intentAction: string; _flags:integer; _startId:integer);
  148. var
  149.   jParams: array[0..2] of jValue;
  150.   jMethod: jMethodID=nil;
  151.   jCls: jClass=nil;
  152. begin
  153.   jParams[0].l:= env^.NewStringUTF(env, PChar(_intentAction));
  154.   jParams[1].i := _flags;
  155.   jParams[2].i := _startId;
  156.   jCls:= env^.GetObjectClass(env, _jtimerservice);
  157.   jMethod:= env^.GetMethodID(env, jCls, 'Start', '(Ljava/lang/String;Ljava/lang/String;)(II)V');
  158.   env^.CallVoidMethodA(env, _jtimerservice, jMethod, @jParams);
  159.   env^.DeleteLocalRef(env,jParams[0].l);
  160.   env^.DeleteLocalRef(env, jCls);
  161. end;
  162.  
  163. procedure jTimerService_SetEnabled(env: PJNIEnv; _jtimerservice: JObject; _enable: boolean);
  164. var
  165.   jParams: array[0..0] of jValue;
  166.   jMethod: jMethodID=nil;
  167.   jCls: jClass=nil;
  168. begin
  169.   jParams[0].z := JBool(_enable);
  170.   jCls:= env^.GetObjectClass(env, _jtimerservice);
  171.   jMethod:= env^.GetMethodID(env, jCls, 'SetEnabled', '(Z)V');
  172.   env^.CallVoidMethodA(env, _jtimerservice, jMethod, @jParams);
  173.   env^.DeleteLocalRef(env, jCls);
  174. end;
  175.  
  176. procedure jTimerService_SetInterval(env: PJNIEnv; _jtimerservice: JObject; _interval: integer);
  177. var
  178.   jParams: array[0..0] of jValue;
  179.   jMethod: jMethodID=nil;
  180.   jCls: jClass=nil;
  181. begin
  182.   jParams[0].i := _interval;
  183.   jCls:= env^.GetObjectClass(env, _jtimerservice);
  184.   jMethod:= env^.GetMethodID(env, jCls, 'SetInterval', '(I)V');
  185.   env^.CallVoidMethodA(env, _jtimerservice, jMethod, @jParams);
  186.   env^.DeleteLocalRef(env, jCls);
  187. end;
  188.  
  189. end.
  190.  
  191.  

in then jTimerService.java i try to start the service with the Start methot but the app always crash, the problem is how start the service, controls.activity.startService(intent); work for an IntentService but not for this, any help.
i am Reinier, Nenirey and Segator :) https://github.com/Nenirey

 

TinyPortal © 2005-2018