gpt4 book ai didi

android - 当应用程序未运行时,用于在 ANDROID 中打开/关闭屏幕的 BroadcastReceiver

转载 作者:行者123 更新时间:2023-12-04 12:12:10 24 4
gpt4 key购买 nike

我想检测屏幕关闭和打开事件,因为我创建了 BroadcastReceiver,它仅在应用程序运行时正常工作,当我从任务管理器中删除应用程序时,它不起作用。

Activity :

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
BroadcastReceiver myrcver = new BootReceiver();
IntentFilter screenStateFilter = new IntentFilter();
screenStateFilter.addAction(Intent.ACTION_SCREEN_ON);
screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(myrcver, screenStateFilter);
}
catch (Exception e){
e.fillInStackTrace();
}
}

list :
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="true"
android:label="BootReceiver">
<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" />
</intent-filter>
</receiver>

广播接收器:
public class BootReceiver extends BroadcastReceiver {
boolean screenOff;
@Override
public void onReceive(Context context, Intent intent) {
try {
if ("android.intent.action.SCREEN_ON".equals(intent.getAction())) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOff = false;
}
Intent i = new Intent(context, UpdateService.class);
i.putExtra("screen_state", screenOff);
context.startService(i);
}
}
catch (Exception e){
e.fillInStackTrace();
}
}

}

提前感谢帮助。

最佳答案

如果您希望它在后台运行,即使应用程序未运行,您也必须创建一个服务。
在服务类中,您必须注册到您的接收器。

public class ScreenOnOffService extends Service {

private ScreenOnOffReceiver mScreenReceiver;

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
registerScreenStatusReceiver();
}

@Override
public void onDestroy() {
unregisterScreenStatusReceiver();
}

private void registerScreenStatusReceiver() {
mScreenReceiver = new ScreenOnOffReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
registerReceiver(mScreenReceiver, filter);
}

private void unregisterScreenStatusReceiver() {
try {
if (mScreenReceiver != null) {
unregisterReceiver(mScreenReceiver);
}
} catch (IllegalArgumentException e) {}
}
}

创建服务类后,不要忘记将您的服务添加到 list 中:
<service android:name="com.myapp.services.ScreenOnOffService" />

然后要启动服务,您可以在主要 Activity 中编写以下内容:
Intent intent = new Intent(this, ScreenOnOffService.class);
startService(intent);

对于广播类:
public class ScreenOnOffReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Log.d("StackOverflow", "Screen Off");

} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.d("StackOverflow", "Screen On");
}
}
}

备注 : 对我来说 screen offscreen on ,做了 不是 在 manifest 内工作,但仅在运行时。

关于android - 当应用程序未运行时,用于在 ANDROID 中打开/关闭屏幕的 BroadcastReceiver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42467885/

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