Bu yazımda daha önceden paylaşmış olduğum Broadcast Receiver Componenti ile birlikte internet kontrolünü nasıl yapabileceğinizden bahsedicem.
Neden böyle birşeye ihtiyacımız olabilir diye soran olursa, İnternet üzerinden işlem yapan bir uygulama yaptığınızı düşünün tam servere istek gönderilmeden önce internet gitme ihtimali olma şansı çok büyük
böyle bir durumda uygulama içerisinde önlem almak isteyebilirsiniz.
Başka bir ihtimal düşünelim dosya indirme hakkında bir uygulama yaptınız fakat sadece wifi aktifken indirme özelliği eklemek istiyorsunuz bunun gibi network olaylarını dinlemeniz gerekebilir.
Lafı fazla uzatmadan gerekli pas dosyasını en alta linkini ve direk kod olarak veriyorum onu indirin ve projenize dahil edin.
Şimdi projenize bahsetmiş olduğum Broadcast Receiver Componenti ekleyin kullanımını kendi başlığında öğrenebilirsiniz ek olarak dinlenmesini istediğimiz yayını ekleyeceğiz.
BroadcastReceiver1.Add('android.net.conn.CONNECTIVITY_CHANGE');
bu sayede network yayınında bir değişiklik olursa projemize bunu yakalaması için talimat verdik fakat eksik birşey kaldı oda uygulamada "Access network state" iznini true yapmak.
Uses bloğuna "NetworkState.Android" pas dosyamızı tanımlıyoruz.
Son olarak "OnReceive" eventine aşağıdaki kodları ekleyiniz.
var
NetworkInfo: TNetworkState;
begin
if NetworkInfo.GetState = TState.Wifi then
ShowMessage('Wifi aktif')
else if NetworkInfo.GetState = TState.Mobile then
ShowMessage('3G aktif')
else if NetworkInfo.GetState = TState.NotConnect then
ShowMessage('İnternet Yok')
NetworkState.Android.pas indirme Linki: https://www.mediafire.com/?3gxl3zdvmtquflu
unit NetworkState.Android;
interface
{$IFDEF ANDROID}
type
TState = (Wifi,Mobile,NotConnect);
TNetworkState = class
private
function IsConnected: Boolean;
function IsWifiConnected: Boolean;
function IsMobileConnected: Boolean;
public
function GetState: TState;
end;
{$ENDIF}
implementation
{$IFDEF ANDROID}
uses
System.SysUtils,
Androidapi.JNIBridge,
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.JavaTypes,
FMX.Helpers.Android,
AndroidApi.Helpers;
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;
JWifiManager = interface;
JWifiInfo = interface;
JWifiManagerClass = interface(JObjectClass)
['{69F35EA7-3EB9-48AA-B7FC-4FFD0E7D712F}']
function _GetACTION_PICK_WIFI_NETWORK: JString;
function _GetEXTRA_WIFI_INFO: JString;
function _GetWIFI_STATE_CHANGED_ACTION: JString;
property ACTION_PICK_WIFI_NETWORK: JString read _GetACTION_PICK_WIFI_NETWORK;
property EXTRA_WIFI_INFO: JString read _GetEXTRA_WIFI_INFO;
property WIFI_STATE_CHANGED_ACTION: JString read _GetWIFI_STATE_CHANGED_ACTION;
end;
[JavaSignature('android/net/wifi/WifiManager')]
JWifiManager = interface(JObject)
['{DA7107B9-1FAD-4A9E-AA09-8D5B84614E60}']
function isWifiEnabled: Boolean; cdecl;
function setWifiEnabled(enabled: Boolean):Boolean; cdecl;
//function getConfiguredNetworks : JList;cdecl;
function getConnectionInfo: JWifiInfo; cdecl;
end;
TJWifiManager = class(TJavaGenericImport) end;
JWifiInfoClass = interface(JObjectClass)
['{2B1CE79F-DE4A-40D9-BB2E-7F9F118D8C08}']
function _GetLINK_SPEED_UNITS:JString;
property LINK_SPEED_UNITS: JString read _GetLINK_SPEED_UNITS;
end;
[JavaSignature('android/net/wifi/WifiInfo')]
JWifiInfo = interface(JObject)
['{4F09E865-DB04-4E64-8C81-AEFB36DABC45}']
function getBSSID: JString; cdecl;
function getHiddenSSID: Boolean; cdecl;
function getIpAddress: integer; cdecl;
function getLinkSpeed: integer; cdecl;
function getMacAddress: JString; cdecl;
function getNetworkId: integer; cdecl;
function getRssi: integer; cdecl;
function getSSID: JString; cdecl;
end;
TJWifiInfo= class(TJavaGenericImport) end;
function GetWifiManager: JWifiManager;
var
WifiManagerNative: JObject;
begin
WifiManagerNative := SharedActivityContext.getSystemService(TJContext.JavaClass.WIFI_SERVICE);
if not Assigned(WifiManagerNative) then
raise Exception.Create('Could not locate Wifi Service');
Result := TJWifiManager.Wrap((WifiManagerNative as ILocalObject).GetObjectID);
if not Assigned(Result) then
raise Exception.Create('Could not access Wifi Manager');
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;
{ TPlatformNetworkState }
function TNetworkState.IsConnected: Boolean;
var
ConnectivityManager: JConnectivityManager;
ActiveNetwork: JNetworkInfo;
begin
ConnectivityManager := GetConnectivityManager;
ActiveNetwork := ConnectivityManager.getActiveNetworkInfo;
Result := Assigned(ActiveNetwork) and ActiveNetwork.isConnected;
end;
function TNetworkState.IsMobileConnected: Boolean;
var
ConnectivityManager: JConnectivityManager;
MobileNetwork: JNetworkInfo;
begin
ConnectivityManager := GetConnectivityManager;
MobileNetwork := ConnectivityManager.getNetworkInfo(TJConnectivityManager.JavaClass.TYPE_MOBILE);
Result := MobileNetwork.isConnected;
end;
function TNetworkState.IsWifiConnected: Boolean;
var
ConnectivityManager: JConnectivityManager;
WiFiNetwork: JNetworkInfo;
begin
ConnectivityManager := GetConnectivityManager;
WiFiNetwork := ConnectivityManager.getNetworkInfo(TJConnectivityManager.JavaClass.TYPE_WIFI);
Result := WiFiNetwork.isConnected;
end;
function TNetworkState.GetState: TState;
begin
if not IsConnected then
Result := NotConnect
else if IsMobileConnected then
Result := Mobile
else if IsWifiConnected then
Result := Wifi
end;
{$ENDIF}
end.

Hiç yorum yok:
Yorum Gönder