简体   繁体   English

屏幕关闭或锁定时,android broadcastreceiver?

[英]android broadcastreceiver when screen off or lock?

I have broadcastreceiver in my application and it's work good, when i connect with the smart phone to bluetooth device it's show me a alert dialog. 我的应用程序中已经安装了广播接收器,并且运行良好,当我将智能手机连接到蓝牙设备时,会显示一个警告对话框。 But, if the screen is off or in the lock screen, it's not show me the alert dialog, and i want to show the alert dialog even if the smartphone on the lock screen or the screen off. 但是,如果屏幕关闭或处于锁定屏幕,则不会向我显示警报对话框,即使智能手机位于锁定屏幕或屏幕处于关闭状态,我也要显示警报对话框。 How i can do this? 我该怎么做?

Thanks! 谢谢!

public class ScreenReceiver extends BroadcastReceiver {


    public static boolean wasScreenOn = true;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
           //do something here

        }
    }
}

This is how you register the receiver. 这是注册接收者的方式。

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);

Since you didn't provide any code, I cannot be sure about this. 由于您未提供任何代码,因此我不确定。 However, your problem might be related to the context from which you are declaring the Intent (if any), or anyway the dialog. 但是,您的问题可能与您声明意图 (如果有)的上下文有关,或者与对话框有关。 Try to use context.getApplicationContext() instead of context / this: 尝试使用context.getApplicationContext()代替context / this:

final Intent dialogIntent = new Intent(context.getApplicationContext(), DialogActivity.class);
context.startActivity(dialogIntent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM