Good night guys, I'm a Brazilian developer, so excuse my English by google translator!

Good night guys, I'm a Brazilian developer, so excuse my English by google translator!
I'm having a problem to check the status of the internet connection and also with the toast friend developed by the community.
On the state of the connection I caught the following code "SharedActivityContext.getSystemService (TJContext.JavaClass.CONNECTIVITY_SERVICE);"
Can anyone help me know if the internet connection is active and is Wifi or 3G?

Comments

  1. I'll knock up a unit that offers connectivity a check and post it later.
    What was the issue with the toast solution?

    ReplyDelete
  2. Something like this should do it:

    unit Network;

    interface

    function IsConnected: Boolean;

    function IsWiFiConnected: Boolean;

    function IsMobileConnected: Boolean;

    implementation

    uses
      System.SysUtils,
      Androidapi.JNIBridge,
      Androidapi.JNI.GraphicsContentViewText,
      Androidapi.JNI.JavaTypes,
      FMX.Helpers.Android;

    type
      JConnectivityManager = interface;
      JNetworkInfo = interface;

      JNetworkInfoClass = interface(JObjectClass)
      ['{E92E86E8-0BDE-4D5F-B44E-3148BD63A14C}']
      end;

      [JavaSignature('android/net/NetworkInfo')]
      JNetworkInfo = interface(JObject)
      ['{6DF61A40-8D17-4E51-8EF2-32CDC81AC372}']
        {Methods}
        function isAvailable: Boolean; cdecl;
        function isConnected: Boolean; cdecl;
        function isConnectedOrConnecting: Boolean; cdecl;
      end;
      TJNetworkInfo = class(TJavaGenericImport) end;

      JConnectivityManagerClass = interface(JObjectClass)
      ['{E03A261F-59A4-4236-8CDF-0068FC6C5FA1}']
        {Property methods}
        function _GetTYPE_WIFI: Integer; cdecl;
        function _GetTYPE_WIMAX: Integer; cdecl;
        function _GetTYPE_MOBILE: Integer; cdecl;
        {Properties}
        property TYPE_WIFI: Integer read _GetTYPE_WIFI;
        property TYPE_WIMAX: Integer read _GetTYPE_WIMAX;
        property TYPE_MOBILE: Integer read _GetTYPE_MOBILE;
      end;

      [JavaSignature('android/net/ConnectivityManager')]
      JConnectivityManager = interface(JObject)
      ['{1C4C1873-65AE-4722-8EEF-36BBF423C9C5}']
        {Methods}
        function getActiveNetworkInfo: JNetworkInfo; cdecl;
        function getNetworkInfo(networkType: Integer): JNetworkInfo; cdecl;
      end;
      TJConnectivityManager = class(TJavaGenericImport) end;

    function GetConnectivityManager: JConnectivityManager;
    var
      ConnectivityServiceNative: JObject;
    begin
      ConnectivityServiceNative := SharedActivityContext.getSystemService(TJContext.JavaClass.CONNECTIVITY_SERVICE);
      if not Assigned(ConnectivityServiceNative) then
        raise Exception.Create('Could not locate Connectivity Service');
      Result := TJConnectivityManager.Wrap(
        (ConnectivityServiceNative as ILocalObject).GetObjectID);
      if not Assigned(Result) then
        raise Exception.Create('Could not access Connectivity Manager');
    end;

    function IsConnected: Boolean;
    var
      ConnectivityManager: JConnectivityManager;
      ActiveNetwork: JNetworkInfo;
    begin
      ConnectivityManager := GetConnectivityManager;
      ActiveNetwork := ConnectivityManager.getActiveNetworkInfo;
      Result := Assigned(ActiveNetwork) and ActiveNetwork.isConnected;
    end;

    function IsWiFiConnected: Boolean;
    var
      ConnectivityManager: JConnectivityManager;
      WiFiNetwork: JNetworkInfo;
    begin
      ConnectivityManager := GetConnectivityManager;
      WiFiNetwork := ConnectivityManager.getNetworkInfo(TJConnectivityManager.JavaClass.TYPE_WIFI);
      Result := WiFiNetwork.isConnected;
    end;

    function IsMobileConnected: Boolean;
    var
      ConnectivityManager: JConnectivityManager;
      MobileNetwork: JNetworkInfo;
    begin
      ConnectivityManager := GetConnectivityManager;
      MobileNetwork := ConnectivityManager.getNetworkInfo(TJConnectivityManager.JavaClass.TYPE_MOBILE);
      Result := MobileNetwork.isConnected;
    end;

    end.

    ReplyDelete
  3. I tested the toast implementation of +Brian Long. It should work just fine.

    ReplyDelete

  4. Great connectivity. As for the toast, when he calls the timer enabled the program closes completely

    ReplyDelete
  5. Ok, so maybe it's not my Toast wrapper then - that doesn't get involved with timers.
    In which case I have no idea to what you refer.

    ReplyDelete
  6. The "toast" this is your link?
    "https://www.cybertribe.de/info/components/fmx/toast/"
      Within the Unit "unit FMX.AndroidLike.Toast"
    These lines generate an exception that closes the program
    "self.FRectangle.AnimateFloat (" Opacity ", 1, 0.3);
       self.FTimer.Interval: = FDuration;
       self.FTimer.Enabled: = true; "

    ReplyDelete
  7. Hi. You are using the component not Brians native Toast JNI unit . I checked it and the component works with no problem in a testproject that contains a timer. What exactly are you trying to do? You should not access the timer of the component. It is just used internally by the component to fade out the toast after the configured duration.

    ReplyDelete
  8. I provided the original Android unit. I haven't looked at that FMX wrapper, which emulates the Android Toast, I think.
    I supplied the wrapper: Android.JNI.Toast.pas  - try that instead.

    ReplyDelete
  9. To use the FMX Wrapper you need to install the component TToast and use that within your project. I will add installation and how to use instructions on thursday to the weblink. If you send me an email I can send you a small example project in advance.

    ReplyDelete
  10. Sorry guys I was actually using the unit "toast" wrong.
    Now that I have used your +Brian And +Roland really worked perfectly.
    "Android.JNI.Toast.pas"

    Thanks again for the help.
    Can count on me for whatever you need, even holiday in Brazil. kkkkkkk

    ReplyDelete
  11. Cool.  I might come back to this offer.  :-)

    ReplyDelete

Post a Comment