当应用未运行时,在ANDROID中打开/关闭屏幕的BroadcastReceiver [英] BroadcastReceiver for Screen On/Off in ANDROID When App is not running

查看:83
本文介绍了当应用未运行时,在ANDROID中打开/关闭屏幕的BroadcastReceiver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检测我已创建的BroadcastReceiver的屏幕关闭和打开事件,它仅在应用程序正在运行时有效,并且当我从任务管理器中删除该应用程序时它无法正常工作。

I want to detect screen off and on event for that i have created BroadcastReceiver, It only works fine when app is running and when i remove the app from task manager then it does not works.

活动:

@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();
    }
}

Menifest:

<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();
    }
}

}

需要提前提供帮助。

推荐答案

聚会有点晚了,但可能会对某人有所帮助。
按照上述@CookieMonster的说明进行操作,然后在启动服务 ScreenOnOffService 时进一步这样做

A bit late for the party but might help someone. Do as mentioned by @CookieMonster above, further while starting your service ScreenOnOffService , do like this

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForegroundService(new Intent(this, ScreenOnOffService.class));
    } else {
        startService(new Intent(this, ScreenOnOffService.class));
    }

并在您的 ScreenOnOffService onCreate()方法中粘贴下面的代码

and in your ScreenOnOffService onCreate() method paste the below code

if (Build.VERSION.SDK_INT >= 26) {
        String CHANNEL_ID = "your_channel_id";
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                "Notification Channel Title",
                NotificationManager.IMPORTANCE_DEFAULT);

        ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("")
                .setContentText("").build();

        startForeground(1, notification);
    }

现在运行您的应用程序。它将按预期工作。

now run your application. It will work as expected.

这篇关于当应用未运行时,在ANDROID中打开/关闭屏幕的BroadcastReceiver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆