码迷,mamicode.com
首页 > 移动开发 > 详细

delphiXE7关于android API的使用和检测WIFI状态的问题

时间:2014-12-18 20:21:10      阅读:680      评论:0      收藏:0      [点我收藏+]

标签:des   android   style   blog   ar   io   color   os   使用   

在大富翁论坛上有一篇介绍delphi xe5下获取网络状态的文章并附带了一个Androidapi.JNI.Network.pas单元,我使用到XE7下,编译后点击按钮就出来一个错误对话框什么10027什么的,开始我以为是不通用那,我就利用 爱吃猪头肉 QQ: 1444386932 提供的jar转换工具转换了安卓jar包中的文件,正常运行后还是提示错误,后来发现是原来delphi编译器没有设置安卓的权限,可是这也让我稍微学习到一点,只要将安卓的jar文件转换下能让delphi调用,应该是所有的api都可以实现调用了吧,稍微参考下网上其他编程工具写的代码就可以转换为delphi的代码,挺兴奋的,终于近了一步,大富翁论坛上看到官方也提供了一个转换工具,只能提供给正式版用户使用,暂时下载不到,以后学习的时候再说吧,顺便附上我转换的几个文件,再次感谢 爱吃猪头肉 QQ: 1444386932 提供的这么好的工具,膜拜下...

 

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Layouts,
  FMX.Memo, FMX.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    mmo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure print(str:string) ;
  public
    { Public declarations }
    /// <summary>
    ///   检测安卓WIFI状态
    /// </summary>
    /// <returns>
    ///   返回WIFI状态
    /// </returns>
    function IsWiFiConnected: Boolean;
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

uses Androidapi.JNI.android.net.ConnectivityManager,
  Androidapi.JNI.android.net.NetworkInfo,
  Androidapi.JNI.android.net.NetworkInfo_DetailedState,
  Androidapi.JNI.android.net.NetworkInfo_State, FMX.Helpers.Android,
Androidapi.JNI.JavaTypes, Androidapi.JNI.GraphicsContentViewText,
FMX.Platform.Android, Androidapi.JNIBridge, Androidapi.JNI.Provider,
Androidapi.Helpers, AndroidAPI.jni.OS;

{ TForm1 }
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;

procedure TForm1.Button1Click(Sender: TObject);
begin
 if IsWiFiConnected then
 begin
   print(wifi正常);
 end
 else
 begin
   print(wifi断开);
 end;
end;

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

procedure TForm1.print(str: string);
begin
 mmo1.Lines.Add(str)
end;

end.
//====================================================
//
//  转换来自JavaClassToDelphiUnit
//  原始作者:ying32
//  QQ: 1444386932
//      396506155
//  Email:yuanfen3287@vip.qq.com
//
//  修改 By:Flying Wang & 爱吃猪头肉
//  请不要移除以上的任何信息。
//  请不要将本版本发到城通网盘,否则死全家。
//
//  Email:1765535979@qq.com
//
//  生成时间:2014/12/18 星期四 18:19:34
//  工具版本:1.0.2014.12.12
//
//====================================================
unit Androidapi.JNI.android.net.ConnectivityManager;

interface

uses
  Androidapi.JNI.android.net.NetworkInfo,
  Androidapi.JNIBridge,
  Androidapi.JNI.JavaTypes,
  Androidapi.JNI.Net;


type
// ===== Forward declarations =====

  JConnectivityManager = interface; //android.net.ConnectivityManager

// ===== Interface declarations =====

  JConnectivityManagerClass = interface(JObjectClass)
  [{713DEEDA-A465-4951-9D56-271FB50EBEBE}]
    { static Property Methods }
    {class} function _GetCONNECTIVITY_ACTION: JString;
    {class} function _GetEXTRA_NETWORK_INFO: JString;
    {class} function _GetEXTRA_IS_FAILOVER: JString;
    {class} function _GetEXTRA_OTHER_NETWORK_INFO: JString;
    {class} function _GetEXTRA_NO_CONNECTIVITY: JString;
    {class} function _GetEXTRA_REASON: JString;
    {class} function _GetEXTRA_EXTRA_INFO: JString;
    {class} function _GetACTION_BACKGROUND_DATA_SETTING_CHANGED: JString;
    {class} function _GetTYPE_MOBILE: Integer;
    {class} function _GetTYPE_WIFI: Integer;
    {class} function _GetTYPE_MOBILE_MMS: Integer;
    {class} function _GetTYPE_MOBILE_SUPL: Integer;
    {class} function _GetTYPE_MOBILE_DUN: Integer;
    {class} function _GetTYPE_MOBILE_HIPRI: Integer;
    {class} function _GetTYPE_WIMAX: Integer;
    {class} function _GetTYPE_BLUETOOTH: Integer;
    {class} function _GetTYPE_DUMMY: Integer;
    {class} function _GetTYPE_ETHERNET: Integer;
    {class} function _GetDEFAULT_NETWORK_PREFERENCE: Integer;

    { static Methods }
    {class} function isNetworkTypeValid(P1: Integer): Boolean; cdecl;

    { static Property }
    {class} property CONNECTIVITY_ACTION: JString read _GetCONNECTIVITY_ACTION;
    {class} property EXTRA_NETWORK_INFO: JString read _GetEXTRA_NETWORK_INFO;
    {class} property EXTRA_IS_FAILOVER: JString read _GetEXTRA_IS_FAILOVER;
    {class} property EXTRA_OTHER_NETWORK_INFO: JString read _GetEXTRA_OTHER_NETWORK_INFO;
    {class} property EXTRA_NO_CONNECTIVITY: JString read _GetEXTRA_NO_CONNECTIVITY;
    {class} property EXTRA_REASON: JString read _GetEXTRA_REASON;
    {class} property EXTRA_EXTRA_INFO: JString read _GetEXTRA_EXTRA_INFO;
    {class} property ACTION_BACKGROUND_DATA_SETTING_CHANGED: JString read _GetACTION_BACKGROUND_DATA_SETTING_CHANGED;
    {class} property TYPE_MOBILE: Integer read _GetTYPE_MOBILE;
    {class} property TYPE_WIFI: Integer read _GetTYPE_WIFI;
    {class} property TYPE_MOBILE_MMS: Integer read _GetTYPE_MOBILE_MMS;
    {class} property TYPE_MOBILE_SUPL: Integer read _GetTYPE_MOBILE_SUPL;
    {class} property TYPE_MOBILE_DUN: Integer read _GetTYPE_MOBILE_DUN;
    {class} property TYPE_MOBILE_HIPRI: Integer read _GetTYPE_MOBILE_HIPRI;
    {class} property TYPE_WIMAX: Integer read _GetTYPE_WIMAX;
    {class} property TYPE_BLUETOOTH: Integer read _GetTYPE_BLUETOOTH;
    {class} property TYPE_DUMMY: Integer read _GetTYPE_DUMMY;
    {class} property TYPE_ETHERNET: Integer read _GetTYPE_ETHERNET;
    {class} property DEFAULT_NETWORK_PREFERENCE: Integer read _GetDEFAULT_NETWORK_PREFERENCE;
  end;

  [JavaSignature(android/net/ConnectivityManager)]
  JConnectivityManager = interface(JObject)
  [{0DF48060-8B57-4DEF-8BC1-1D9D2DBF9D5D}]
    { Property Methods }

    { methods }
    procedure setNetworkPreference(preference: Integer); cdecl;
    function getNetworkPreference: Integer; cdecl;
    function getActiveNetworkInfo: JNetworkInfo; cdecl;
    function getNetworkInfo(networkType: Integer): JNetworkInfo; cdecl;
    function getAllNetworkInfo: TJavaObjectArray<JNetworkInfo>; cdecl;
    function startUsingNetworkFeature(networkType: Integer; feature: JString): Integer; cdecl;
    function stopUsingNetworkFeature(networkType: Integer; feature: JString): Integer; cdecl;
    function requestRouteToHost(networkType: Integer; hostAddress: Integer): Boolean; cdecl;
    function getBackgroundDataSetting: Boolean; cdecl;

    { Property }
  end;

  TJConnectivityManager = class(TJavaGenericImport<JConnectivityManagerClass, JConnectivityManager>) end;

implementation


procedure RegisterTypes;
begin
  TRegTypes.RegisterType(Androidapi.JNI.android.net.ConnectivityManager.JConnectivityManager, 
    TypeInfo(Androidapi.JNI.android.net.ConnectivityManager.JConnectivityManager));
end;


initialization
  RegisterTypes;
end.
//====================================================
//
//  转换来自JavaClassToDelphiUnit
//  原始作者:ying32
//  QQ: 1444386932
//      396506155
//  Email:yuanfen3287@vip.qq.com
//
//  修改 By:Flying Wang & 爱吃猪头肉
//  请不要移除以上的任何信息。
//  请不要将本版本发到城通网盘,否则死全家。
//
//  Email:1765535979@qq.com
//
//  生成时间:2014/12/18 星期四 18:24:32
//  工具版本:1.0.2014.12.12
//
//====================================================
unit Androidapi.JNI.android.net.NetworkInfo;

interface

uses
  Androidapi.JNI.android.net.NetworkInfo_DetailedState,
  Androidapi.JNI.android.net.NetworkInfo_State,
  Androidapi.JNIBridge, 
  Androidapi.JNI.JavaTypes, 
  Androidapi.JNI.Net, 
  Androidapi.JNI.os;


type
// ===== Forward declarations =====

  JNetworkInfo = interface; //android.net.NetworkInfo

// ===== Interface declarations =====

  JNetworkInfoClass = interface(JObjectClass)
  [{9C1E2D18-F7E9-4D0C-8844-F238B372540E}]
    { static Property Methods }

    { static Methods }

    { static Property }
  end;

  [JavaSignature(android/net/NetworkInfo)]
  JNetworkInfo = interface(JObject)
  [{C5EE947D-2A5A-47AA-A24D-3A7DAFFEF54F}]
    { Property Methods }

    { methods }
    function getType: Integer; cdecl;
    function getSubtype: Integer; cdecl;
    function getTypeName: JString; cdecl;
    function getSubtypeName: JString; cdecl;
    function isConnectedOrConnecting: Boolean; cdecl;
    function isConnected: Boolean; cdecl;
    function isAvailable: Boolean; cdecl;
    function isFailover: Boolean; cdecl;
    function isRoaming: Boolean; cdecl;
    function getState: JNetworkInfo_State; cdecl;
    function getDetailedState: JNetworkInfo_DetailedState; cdecl;
    function getReason: JString; cdecl;
    function getExtraInfo: JString; cdecl;
    function toString: JString; cdecl;
    function describeContents: Integer; cdecl;
    procedure writeToParcel(dest: JParcel; flags: Integer); cdecl;

    { Property }
  end;

  TJNetworkInfo = class(TJavaGenericImport<JNetworkInfoClass, JNetworkInfo>) end;

implementation

procedure RegisterTypes;
begin
  TRegTypes.RegisterType(Androidapi.JNI.android.net.NetworkInfo.JNetworkInfo, 
    TypeInfo(Androidapi.JNI.android.net.NetworkInfo.JNetworkInfo));
end;


initialization
  RegisterTypes;
end.
//====================================================
//
//  转换来自JavaClassToDelphiUnit
//  原始作者:ying32
//  QQ: 1444386932
//      396506155
//  Email:yuanfen3287@vip.qq.com
//
//  修改 By:Flying Wang & 爱吃猪头肉
//  请不要移除以上的任何信息。
//  请不要将本版本发到城通网盘,否则死全家。
//
//  Email:1765535979@qq.com
//
//  生成时间:2014/12/18 星期四 18:36:59
//  工具版本:1.0.2014.12.12
//
//====================================================
unit Androidapi.JNI.android.net.NetworkInfo_DetailedState;

interface

uses
  Androidapi.JNIBridge, 
  Androidapi.JNI.JavaTypes, 
  Androidapi.JNI.Net;


type
// ===== Forward declarations =====

  JNetworkInfo_DetailedState = interface; //android.net.NetworkInfo$DetailedState

// ===== Interface declarations =====

  JNetworkInfo_DetailedStateClass = interface(JObjectClass)
  [{3E279658-127E-48B4-A265-DCA4F6D19729}]
    { static Property Methods }
    {class} function _GetAUTHENTICATING: JNetworkInfo_DetailedState;
    {class} function _GetBLOCKED: JNetworkInfo_DetailedState;
    {class} function _GetCONNECTED: JNetworkInfo_DetailedState;
    {class} function _GetCONNECTING: JNetworkInfo_DetailedState;
    {class} function _GetDISCONNECTED: JNetworkInfo_DetailedState;
    {class} function _GetDISCONNECTING: JNetworkInfo_DetailedState;
    {class} function _GetFAILED: JNetworkInfo_DetailedState;
    {class} function _GetIDLE: JNetworkInfo_DetailedState;
    {class} function _GetOBTAINING_IPADDR: JNetworkInfo_DetailedState;
    {class} function _GetSCANNING: JNetworkInfo_DetailedState;
    {class} function _GetSUSPENDED: JNetworkInfo_DetailedState;

    { static Methods }
    {class} function values: TJavaObjectArray<JNetworkInfo_DetailedState>; cdecl;
    {class} function valueOf(P1: JString): JNetworkInfo_DetailedState; cdecl;

    { static Property }
    {class} property AUTHENTICATING: JNetworkInfo_DetailedState read _GetAUTHENTICATING;
    {class} property BLOCKED: JNetworkInfo_DetailedState read _GetBLOCKED;
    {class} property CONNECTED: JNetworkInfo_DetailedState read _GetCONNECTED;
    {class} property CONNECTING: JNetworkInfo_DetailedState read _GetCONNECTING;
    {class} property DISCONNECTED: JNetworkInfo_DetailedState read _GetDISCONNECTED;
    {class} property DISCONNECTING: JNetworkInfo_DetailedState read _GetDISCONNECTING;
    {class} property FAILED: JNetworkInfo_DetailedState read _GetFAILED;
    {class} property IDLE: JNetworkInfo_DetailedState read _GetIDLE;
    {class} property OBTAINING_IPADDR: JNetworkInfo_DetailedState read _GetOBTAINING_IPADDR;
    {class} property SCANNING: JNetworkInfo_DetailedState read _GetSCANNING;
    {class} property SUSPENDED: JNetworkInfo_DetailedState read _GetSUSPENDED;
  end;

  [JavaSignature(android/net/NetworkInfo$DetailedState)]
  JNetworkInfo_DetailedState = interface(JObject)
  [{B310D74B-786C-460B-A6E5-CEA0BFDF8986}]
    { Property Methods }

    { methods }

    { Property }
  end;

  TJNetworkInfo_DetailedState = class(TJavaGenericImport<JNetworkInfo_DetailedStateClass, JNetworkInfo_DetailedState>) end;

implementation

procedure RegisterTypes;
begin
  TRegTypes.RegisterType(Androidapi.JNI.android.net.NetworkInfo_DetailedState.JNetworkInfo_DetailedState, 
    TypeInfo(Androidapi.JNI.android.net.NetworkInfo_DetailedState.JNetworkInfo_DetailedState));
end;


initialization
  RegisterTypes;
end.
//====================================================
//
//  转换来自JavaClassToDelphiUnit
//  原始作者:ying32
//  QQ: 1444386932
//      396506155
//  Email:yuanfen3287@vip.qq.com
//
//  修改 By:Flying Wang & 爱吃猪头肉
//  请不要移除以上的任何信息。
//  请不要将本版本发到城通网盘,否则死全家。
//
//  Email:1765535979@qq.com
//
//  生成时间:2014/12/18 星期四 18:35:41
//  工具版本:1.0.2014.12.12
//
//====================================================
unit Androidapi.JNI.android.net.NetworkInfo_State;

interface

uses
  Androidapi.JNIBridge, 
  Androidapi.JNI.JavaTypes, 
  Androidapi.JNI.Net;


type
// ===== Forward declarations =====

  JNetworkInfo_State = interface; //android.net.NetworkInfo$State

// ===== Interface declarations =====

  JNetworkInfo_StateClass = interface(JObjectClass)
  [{7984D00F-5A1B-4208-9D34-8E474879A636}]
    { static Property Methods }
    {class} function _GetCONNECTED: JNetworkInfo_State;
    {class} function _GetCONNECTING: JNetworkInfo_State;
    {class} function _GetDISCONNECTED: JNetworkInfo_State;
    {class} function _GetDISCONNECTING: JNetworkInfo_State;
    {class} function _GetSUSPENDED: JNetworkInfo_State;
    {class} function _GetUNKNOWN: JNetworkInfo_State;

    { static Methods }
    {class} function values: TJavaObjectArray<JNetworkInfo_State>; cdecl;
    {class} function valueOf(P1: JString): JNetworkInfo_State; cdecl;

    { static Property }
    {class} property CONNECTED: JNetworkInfo_State read _GetCONNECTED;
    {class} property CONNECTING: JNetworkInfo_State read _GetCONNECTING;
    {class} property DISCONNECTED: JNetworkInfo_State read _GetDISCONNECTED;
    {class} property DISCONNECTING: JNetworkInfo_State read _GetDISCONNECTING;
    {class} property SUSPENDED: JNetworkInfo_State read _GetSUSPENDED;
    {class} property UNKNOWN: JNetworkInfo_State read _GetUNKNOWN;
  end;

  [JavaSignature(android/net/NetworkInfo$State)]
  JNetworkInfo_State = interface(JObject)
  [{D13A31B6-1E02-49B7-A926-DF64671A1814}]
    { Property Methods }

    { methods }

    { Property }
  end;

  TJNetworkInfo_State = class(TJavaGenericImport<JNetworkInfo_StateClass, JNetworkInfo_State>) end;

implementation

procedure RegisterTypes;
begin
  TRegTypes.RegisterType(Androidapi.JNI.android.net.NetworkInfo_State.JNetworkInfo_State, 
    TypeInfo(Androidapi.JNI.android.net.NetworkInfo_State.JNetworkInfo_State));
end;


initialization
  RegisterTypes;
end.

 

delphiXE7关于android API的使用和检测WIFI状态的问题

标签:des   android   style   blog   ar   io   color   os   使用   

原文地址:http://www.cnblogs.com/peikeke316/p/4172448.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!