Android实例-屏幕操持常亮(XE8+小米2)

相关资料:

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

Android实例-屏幕操持常亮(XE8+小米2)的相关文章

[转]delphi xe6 android屏幕操持常亮

1) setting the Project Options, Uses Permissions, Wake lock = True 2) Adding Android.JNI.PowerManager to the uses clause 3) call: AcquireWakeLock; // at start up. ReleaseWakeLock; // at shutdown. //----------------------------------------------------

Android实例-TRectangle加载图片(XE8+小米2)

结果: 1.加载图片很流畅,可以做背景用. 2.现在是加载了正形与圆形,其他形状能不能加载呢?自己测试哦,要多动手才行. 3.需要把图片打到包里哦(路径为“assets\internal\”). 实例代码: 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,

Android实例-利用WebBrowser实现浏览器(XE8+小米2)

结果: 1.网络好的情况下,打开很快,很流畅. 2.地址栏真心不好使,如果真要做应用,这块必须自己优化一下. 实例代码: 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, FMX.S

保持手机屏幕常亮无需权限

最近遇到一个项目,运行起来后发现手机屏幕一直常亮,网上搜到有两种方法,但是都没有解决问题 第一种方法: 大家想一想,我们在android里那个地方老是常定义权限呀,有了这个提示,大家就会想到是哪个文件了吧,AndroidManifest.xml:我们要在这个文件里定义一下权限就可以实现我们的手机屏幕保持常亮了.这个方法也是最简单的一个方法,那么我们下面就来看看它的代码: Java代码: 01.<uses-permission android:name="android.permission

Xamarin Essentials教程屏幕常亮ScreenLock

在一段时间内,如果用户没有对设备进行操作,设备就会自动关闭屏幕.用户必须手动操作,才能点亮屏幕.但是很多应用需要在用户没有操作的情况下长时间展示内容,如视频播放.运动数据展示等.这时就需要让屏幕保持常亮状态.在Xamarin.Essentials组件中,静态类ScreenLock可以保持屏幕激活状态. 原文地址:https://www.cnblogs.com/daxueba-ITdaren/p/9299291.html

Android实习札记(2)---妙用View的keepScreenOn保持屏幕常亮

Android实习札记(2)---妙用View的keepScreenOn保持屏幕常亮 --转载请注明出处:coder-pig 1)什么是屏幕常亮? 直译就是让我们的屏幕一直亮着呗,手机通常都带有省电模式什么的,比如设置无操作后,15秒 屏幕自动关闭,如果恰好我们的程序正在执行一些操作的话,突然的屏幕关闭可能会带来一些问题; 比如,应用正在安装或者应用正在更新,下载,处理数据等,于是乎,我们在特定的时候,需要让手机屏幕 保持光亮,那么要怎么让屏幕常亮呢? 2)怎么让屏幕常亮? 主流的两个方法: ①

Android保持屏幕常亮

Android保持屏幕常亮,PowerManager.WakeLock的使用 package com.hebaijun.wakelock;      import android.app.Activity;   import android.os.Bundle;   import android.os.PowerManager;      public class WakeLockActivity extends Activity {       PowerManager.WakeLock wa

Android保持屏幕常亮的方法

以前一直使用newWakeLock方法: this.powerManager = (PowerManager) this .getSystemService(Context.POWER_SERVICE); this.wakeLock = this.powerManager.newWakeLock( PowerManager.FULL_WAKE_LOCK, "My Lock"); //保持常亮 this.wakeLock.acquire(); //释放 this.wakeLock.rel

Android保持屏幕常亮唤醒状态

第一步:  首先添加权限: <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission> 第二步:代码实现如下: public class ScreenActivity extends Activity { PowerManager powerManager = null; WakeLock wakeLock = null; @Override protected