我想将设备发送到睡眠状态或关闭屏幕.我调查过并发现了这个主题:在Android上关闭屏幕
基本上,有三种方法可以做到,但我发现三者有问题:
a)选择1:
PowerManager manager = (PowerManager) getSystemService(Context.POWER_SERVICE);
manager.goToSleep(int amountOfTime);
Run Code Online (Sandbox Code Playgroud)
问题:它导致FC.我已阅读我需要DEVICE_POWER权限,但无法为普通应用授予权限.
b)选择2:
PowerManager manager = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Your Tag");
wl.acquire();
wl.release();
Run Code Online (Sandbox Code Playgroud)
问题:这对我不起作用.我不知道为什么.它不会给我一个FC,但它是无害的.
c)选择3:
WindowManager.LayoutParams params = getWindow().getAttributes();
params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = 0;
getWindow().setAttributes(params);
Run Code Online (Sandbox Code Playgroud)
问题:我可以让它工作但是当试图打开设备它会做一些奇怪的事情,比如不喜欢返回,或者如果我的应用程序在前面,按下按钮后自动进入睡眠状态.与普通解决方案相比,这看起来更像是提示或解决方法.
任何人都可以告诉我任何好的方法来发送设备进入睡眠或关闭屏幕,可以运行没有问题吗?对我来说这样一个简单的功能并不是一个好的方法来使用它(或者至少有详细记录)
我开发了一个应用程序,它启动一个触发倒计时的自定义服务。该服务在计时器开始时运行通知,并在结束时运行其他通知。在测试期间,我发现有关通知和锁定屏幕的问题。我描述了我所做的测试:
1)如果我运行该应用程序并且不让手机关机或锁定,该服务会正确播放通知声音,就好像主应用程序(活动)不在前面一样。在这两种情况下,服务都会完美地触发通知。
2)如果我锁定手机并且锁定前应用程序在前面,则该服务会正确播放通知声音。
3)这是我的问题:如果我锁定手机并且锁定前应用程序不在前面,则通知没有声音,但服务看起来正在运行,因为当我按下手机中的“开”按钮时,然后屏幕打开后立即播放通知(声音和图标),就在我解锁手机之前的锁定屏幕中。
我在服务中的代码取消之前的通知,这是计时器结束后运行通知的代码:
private void notificacion_fin() {
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(0);
Notification not = new Notification();
// Ponemos la nueva notificacion de que hemos acabado
SharedPreferences preferences = this.getSharedPreferences("com.fsp.mypref", 0);
String not_sound = preferences.getString("notification_tone","");
not.audioStreamType = AudioManager.STREAM_NOTIFICATION;
not.sound = Uri.parse(not_sound);
if (tipo_cronometro == 1) {
not.icon = R.drawable.ic_stat_notify_time;
not.tickerText = "Task has finished";
} else {
not.icon = R.drawable.ic_stat_notify_complete;
not.tickerText = "Type 2 task has finished";
}
not.when = System.currentTimeMillis();
not.defaults |= Notification.DEFAULT_VIBRATE;
not.vibrate = new …Run Code Online (Sandbox Code Playgroud) 我想用这样的宏
#define Return(x) {call_my_function(); return (x);}
Run Code Online (Sandbox Code Playgroud)
是否有任何方法可以使用或声明没有括号的宏,所以我可以像真正的返回调用一样使用它而不是函数?例如:
BOOL my_func() {
code....
Return FALSE;
}
Run Code Online (Sandbox Code Playgroud)