相关资料:
http://www.bubuko.com/infodetail-163304.html
结果:
1.打开权限Wake lock为True。
第三方单元:
1 unit Android.JNI.PowerManager; 2 3 interface 4 5 function AcquireWakeLock : Boolean; 6 procedure ReleaseWakeLock; 7 8 implementation 9 10 uses 11 System.SysUtils, 12 Androidapi.Helpers, 13 Androidapi.JNI, 14 Androidapi.JNIBridge, 15 Androidapi.JNI.GraphicsContentViewText, 16 Androidapi.JNI.JavaTypes, 17 FMX.Helpers.Android; 18 19 type 20 JPowerManager = interface; 21 JWakeLock = interface; 22 JWakeLockClass = interface(JObjectClass) 23 [‘{918E171F-CDB8-4464-9507-F49272CE7636}‘] 24 end; 25 26 [JavaSignature(‘android/os/PowerManager$WakeLock‘)] 27 JWakeLock = interface(JObject) 28 [‘{D17B1136-FA15-4AEB-85B1-2D490F0FD320}‘] 29 {Methods} 30 procedure acquire; cdecl; 31 procedure release; cdecl; 32 function isHeld: Boolean; cdecl; 33 end; 34 35 TJWakeLock = class(TJavaGenericImport<JWakeLockClass, JWakeLock>) end; 36 JPowerManagerClass = interface(JObjectClass) 37 [‘{7D0696A2-ADEA-4158-AE1F-5E720DEDBCF9}‘] 38 {Property methods} 39 function _GetFULL_WAKE_LOCK: Integer; cdecl; 40 function _GetSCREEN_BRIGHT_WAKE_LOCK: Integer; cdecl; 41 function _GetSCREEN_DIM_WAKE_LOCK: Integer; cdecl; 42 function _GetPARTIAL_WAKE_LOCK: Integer; cdecl; 43 {Properties} 44 //Keep screen on bright & keyboard on 45 //Deprecated in API level 17 - Jelly Bean MR1 46 property FULL_WAKE_LOCK: Integer read _GetFULL_WAKE_LOCK; 47 //Keep screen on bright 48 //Deprecated in API level 13 - Honeycomb MR2 49 property SCREEN_BRIGHT_WAKE_LOCK: Integer read _GetSCREEN_BRIGHT_WAKE_LOCK; 50 //Keep screen on dim 51 //Deprecated in API level 17 - Jelly Bean MR1 52 property SCREEN_DIM_WAKE_LOCK: Integer read _GetSCREEN_DIM_WAKE_LOCK; 53 //Keep CPU running, screen & keyboard can go off 54 property PARTIAL_WAKE_LOCK: Integer read _GetPARTIAL_WAKE_LOCK; 55 end; 56 57 [JavaSignature(‘android/os/PowerManager‘)] 58 JPowerManager = interface(JObject) 59 [‘{DEAED658-4353-4D17-B0A3-8179E48BE87F}‘] 60 {Methods} 61 function newWakeLock(levelAndFlags: Integer; tag: JString): JWakeLock; cdecl; 62 end; 63 64 TJPowerManager = class(TJavaGenericImport<JPowerManagerClass, JPowerManager>) end; 65 66 function GetPowerManager: JPowerManager; 67 var 68 PowerServiceNative: JObject; 69 begin 70 PowerServiceNative := SharedActivityContext.getSystemService( 71 TJContext.JavaClass.POWER_SERVICE); 72 if not Assigned(PowerServiceNative) then 73 raise Exception.Create(‘Could not locate Power Service‘); 74 Result := TJPowerManager.Wrap( 75 (PowerServiceNative as ILocalObject).GetObjectID); 76 if not Assigned(Result) then 77 raise Exception.Create(‘Could not access Power Manager‘); 78 end; 79 80 var 81 WakeLock: JWakeLock = nil; 82 function AcquireWakeLock: Boolean; 83 var 84 PowerManager: JPowerManager; 85 begin 86 Result := Assigned(WakeLock); 87 if not Result then 88 begin 89 PowerManager := GetPowerManager; 90 WakeLock := PowerManager.newWakeLock( 91 TJPowerManager.JavaClass.SCREEN_BRIGHT_WAKE_LOCK, 92 StringToJString(‘Runing‘)); 93 Result := Assigned(WakeLock); 94 end; 95 if Result then 96 begin 97 if not WakeLock.IsHeld then 98 begin 99 WakeLock.acquire; 100 Result := WakeLock.isHeld 101 end; 102 end; 103 end; 104 105 procedure ReleaseWakeLock; 106 begin 107 if Assigned(WakeLock) then 108 begin 109 WakeLock.release; 110 WakeLock := nil 111 end; 112 end; 113 114 end.
实例代码:
1 unit Unit1; 2 3 interface 4 5 uses 6 System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, 7 FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, 8 FMX.Controls.Presentation, FMX.StdCtrls; 9 10 type 11 TForm1 = class(TForm) 12 Label1: TLabel; 13 Button1: TButton; 14 Button2: TButton; 15 procedure Button1Click(Sender: TObject); 16 procedure Button2Click(Sender: TObject); 17 private 18 { Private declarations } 19 public 20 { Public declarations } 21 end; 22 23 var 24 Form1: TForm1; 25 26 implementation 27 uses 28 Android.JNI.PowerManager;//需要引入 29 {$R *.fmx} 30 {$R *.NmXhdpiPh.fmx ANDROID} 31 32 procedure TForm1.Button1Click(Sender: TObject); 33 begin 34 AcquireWakeLock;//获取唤醒锁 35 end; 36 37 procedure TForm1.Button2Click(Sender: TObject); 38 begin 39 ReleaseWakeLock;//释放唤醒锁 40 end; 41 42 end.
时间: 2024-10-12 12:54:41