【问题标题】:Get a notification & dialog when the screen is locked屏幕锁定时获取通知和对话框
【发布时间】:2017-12-12 09:41:13
【问题描述】:

在我的应用中,当一些连接发生时,用户应该做出一些决定。我希望用户在对话框中执行此操作。因此,如果屏幕已解锁,则立即显示对话框,如果屏幕已锁定,则打开屏幕并显示通知(如短信通知),当用户触摸通知并解锁屏幕时,请查看对话框。

【问题讨论】:

  • 当一些连接发生时,用户应该做出一些决定——这是什么意思?即使您的应用程序被杀死,您是否想显示警报?
  • @Joe 是的,我的应用程序有一项服务正在等待连接。当连接建立时,应该通知用户并且应该做出一些决定。

标签: android android-studio notifications android-alertdialog


【解决方案1】:

看看wake lock,它可以让您打开屏幕并在您需要的一段时间内保持打开状态。

Here is the example.

【讨论】:

  • 谢谢。你的回答有帮助。我不需要使用广播接收器。
【解决方案2】:

创建广播接收器:

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

通过接收器,您将知道屏幕何时开启或关闭:

public class ScreenReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
        {
            screenon = false;
        }
        else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
        {
            screenon = true;
        }
    }

}

不要忘记在清单中声明接收者。 显示通知和对话框应该不是问题。 使用安卓文档。

【讨论】:

    【解决方案3】:

    只需正常发送通知,并将以下标志添加到从通知调用的活动中。

    YourNotificationService.java

    Intent intent = new Intent(context, YourActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    YourActivity.java

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        ...
    
        getWindow().addFlags(
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
            + WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
            + WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
        );
    
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      • 1970-01-01
      • 1970-01-01
      • 2020-04-06
      相关资源
      最近更新 更多