gpt4 book ai didi

android - ACTION_SCREEN_ON 和 ACTION_SCREEN_OFF 不起作用?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:10:53 26 4
gpt4 key购买 nike

我试图在屏幕关闭(锁定)时关闭 WiFi,并在屏幕打开(解锁)时再次打开它。

我制作了一个BroadcastReceiver;放入 list 代码:

<receiver android:name="MyIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.USER_PRESENT" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>

这是 MyIntentReceiver 类:

package org.androidpeople.boot;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyIntentReceiver extends BroadcastReceiver {
// Called when boot completes

public static boolean startup;

@Override
public void onReceive(Context context, Intent intent) {
// Set what activity should launch after boot completes

System.out.println("Intent Action: " + intent.getAction());

if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {

System.out.println("locked : ACTION_SCREEN_OFF");

} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {

System.out.println("not locked : ACTION_SCREEN_ON ");

} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {

System.out.println("User Unlocking it ");

}
else if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
// this to indicate that program is running
// automaticlly not manually by user
startup = true;
System.out.println("Automatic BOOT at StartUp");

Intent startupBootIntent = new Intent(context, LaunchActivity.class);
startupBootIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startupBootIntent);
}

}
}

结果是 - ACTION_SCREEN_ON并且 ACTION_SCREEN_OFF 从未触发!USER_PRESENTBOOT_COMPLETED 工作正常,但另一个没有。我使用的是模拟器,而不是真实设备 - 这会导致问题吗?

有什么帮助吗?我需要打开和关闭屏幕才能启用/禁用 WiFi以节省电池。

提前致谢

最佳答案

要捕获 SCREEN_OFF 和 SCREEN_ON 操作(可能还有其他操作),您必须通过代码而不是 list 来配置 BroadcastReceiver。

IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenStateBroadcastReceiver();
registerReceiver(mReceiver, intentFilter);

它已经过测试并且可以正常工作。

关于android - ACTION_SCREEN_ON 和 ACTION_SCREEN_OFF 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7366631/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com