简体   繁体   English

从LockScreen启动活动

[英]Start Activity from LockScreen

I have created an app that uses a Broadcast Receiver. 我创建了一个使用广播接收器的应用程序。 The Receiver needs to open another activity via intent. 接收方需要通过意图打开另一个活动。

The program works when phone in unlocked / not sleeping. 当手机处于解锁状态/未进入睡眠状态时,该程序将运行。

But when the screen of device is locked, the activity is not displayed. 但是当设备屏幕锁定时,不显示活动。

I want to run the activity every time a SMS is received even when phone is in sleep or password locked. 我想在每次收到短信时都运行该活动,即使手机处于睡眠状态或密码锁定也是如此。

use below code above setContentView(R.layout.main); setContentView(R.layout.main);上方使用以下代码setContentView(R.layout.main);

 final Window win = getWindow();
    win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                  | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); 
    win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                  | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

now your activity will display even when your device is locked 现在,即使您的设备已锁定,您的活动也会显示

I guess your activity is paused when the phone sleeps, so you don't receive the Broadcast. 我想您的活动会在手机休眠时暂停,因此您不会收到广播。

You could try to launch a Service. 您可以尝试启动服务。 It will be able to run even in sleep mode (like your media player is able to play music even when the phone is locked) 它甚至可以在睡眠模式下运行(例如,即使手机处于锁定状态,媒体播放器也可以播放音乐)

Put your receiver in a Serive an start your activity from there. 将您的接收器放在Serive上,从那里开始您的活动。 It should work 它应该工作

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

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