简体   繁体   English

屏幕关闭时如何开始活动?

[英]How do i start activity when screen goes off?

I have an application which is doing its work only when devices screen goes off. 我有一个仅在设备屏幕关闭时才在工作的应用程序。 I have set up broadcast receiver ( Intent.ACTION_SCREEN_OFF ) and it works fine. 我已经设置了广播接收器( Intent.ACTION_SCREEN_OFF ),并且工作正常。 I got record in my LOGCAT always when devices screen goes off. 当设备屏幕关闭时,我总是在LOGCAT中记录。 So far so good. 到现在为止还挺好。

My onReceive code from ACTION_SCREEN_OFF uses some code calculating stuff, and all executes fine (When screen goes off). 我来自ACTION_SCREEN_OFF的onReceive代码使用一些代码来计算内容,并且全部执行正常(当屏幕关闭时)。 So far so good. 到现在为止还挺好。

At the end of my onReceive code i'm starting new activity, but the onCreate of targeting activity is NOT always executing. 在我的onReceive代码结束时,我将开始新的活动,但是定向活动的onCreate并不总是执行。 (For example on my HTC Desire 2.3.7 works fine. On HTC Wildfire S 2.3.5, Xperia Arc S 2.3.4 it does not execute, and on Samsung Galaxy ACE 2.2.1 , it depends. Sometimes is executing sometimes isn't). (例如,在我的HTC Desire 2.3.7上工作正常。在HTC Wildfire S 2.3.5,Xperia Arc S 2.3.4上它无法执行,而在Samsung Galaxy ACE 2.2.1上则取决于执行情况。 t)。 My LOGCAT shows that onReceive executed till the end but Activity was not started. 我的LOGCAT显示onReceive一直执行到最后,但Activity尚未启动。 I'm using following code to starting this activity: 我正在使用以下代码来开始此活动:

Intent startH_Activity = new Intent(getApplicationContext(), HandleActivity.class);
        startH_Activity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startH_Activity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(startH_Activity);

The important things to note: 需要注意的重要事项:

  • I said that on some phones mentioned above it doesn't work. 我说过,在上面提到的某些手机上它不起作用。 Well its not working immediately, like it should (when SCREEN_OFF fires ). 好吧,它不能立即工作,就像应该的那样(当SCREEN_OFF触发时)。 Always activity starts like.. 10-15 minutes after screen went off (which is not acceptable) 总是开始活动。..屏幕关闭后10-15分钟(这是不可接受的)

  • When screen goes off and activity does not start , if I press POWER button on device it immediatelyfires off my target activity and app is working like normal. 当屏幕关闭且活动无法开始时,如果我按设备上的电源按钮,则会立即触发我的目标活动,并且应用程序将正常运行。 (again this is not acceptable. It should fire automatically). (同样,这是不可接受的。它应该会自动触发)。

  • When ANY device is connected to the PC it works like it should. 当任何设备连接到PC时,它的工作均应正常进行。 Activity starts immediatelyafter screen goes off. 屏幕关闭后,活动立即开始。

After reading a lot of Stack Overflow I realized that this is because MAYBE device goes to sleep. 在阅读了很多堆栈溢出后,我意识到这是因为MAYBE设备进入睡眠状态。 That's why it works like normal if i press POWER button, or Wakes up automatically after 10-15min because it synchronizes or something. 这就是为什么如果我按电源按钮或在10到15分钟后会自动唤醒,因为它会同步或类似的问题,它会像正常一样工作。 So I used WAKELOCK. 所以我用了WAKELOCK。

//OnCreate Service
powMan = (PowerManager) getSystemService(POWER_SERVICE);
wake = powMan.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");

//OnReceive (screen goes off)
wake.acquire();

But still without a success. 但是仍然没有成功。 (Am i doing wrong?) Even wake lock is acquired my activity won't show up on these devices. (我做错了吗?)即使获得了唤醒锁,我的活动也不会显示在这些设备上。

Basically what I'm asking. 基本上我在问什么。 How do i open usual activity when screen goes off? 屏幕关闭时如何打开日常活动? Or at least how to turn screen ON if activity won't start (remember, pressing on power button shows up my activity 或者至少在活动无法开始时如何打开屏幕(请记住,按电源按钮会显示我的活动

I would really need some help now, because I'm getting really frustrated about this. 我现在真的需要一些帮助,因为对此我感到非常沮丧。 Thank you for help. 谢谢你的帮助。

屏幕关闭时,您应该启动服务而不是活动。

I would register yet another broadcast receiver and start the Activity when screen goes on ( Intent.ACTION_SCREEN_ON ). 屏幕打开时,我将注册另一个广播接收器并启动Activity( Intent.ACTION_SCREEN_ON )。

Two issues: 两个问题:

1) How do you pass data from one receiver to another? 1)如何将数据从一个接收器传递到另一个接收器? What comes to my mind is either starting a Service or saving the information to a file (or shared preferences). 我想到的是启动服务或将信息保存到文件(或共享首选项)中。

2) What happens if you have two events that must start an Activity? 2)如果必须启动一个活动的两个事件会怎样? (Eg "something started" and "something finished".) You must resolve this, either showing both events or just the latest one. (例如“某事已开始”和“某事已结束”。)您必须解决此问题,要么显示两个事件,要么仅显示最新事件。

Note that the user can turn the screen on and off without unlocking the screen ( Intent.ACTION_USER_PRESENT ), and that if the phone is configured not to lock the screen, Intent.ACTION_USER_PRESENT does not happen. 请注意,用户可以在不解锁屏幕的情况下打开和关闭屏幕( Intent.ACTION_USER_PRESENT ),并且如果将电话​​配置为不锁定屏幕,则不会发生Intent.ACTION_USER_PRESENT

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

相关问题 当我按下“开始”按钮时,它应该转到下一个活动,但它会转到主屏幕 - When I press button Start it should go to next activity but it goes to home screen android屏幕关闭时如何保持IOIO连接? - How to keep IOIO connected when android screen goes off? 当活动失去焦点时(当主屏幕或屏幕关闭时),Media Player会暂停音乐 - Media Player pauses the music when the activity loses focus(when home or screen goes off) 关闭屏幕后,有时我的应用程序活动会进入后台 - Activity of my app goes into background sometimes after I turn off the screen 开始另一个活动时如何停止可运行对象? - How do I stop a runnable when I start another activity? 当设置的警报响起时,如何调用OnReceive()函数? - How do I invoke the OnReceive() function when a set alarm goes off? 屏幕关闭(黑色)时,完成当前活动并开始另一个活动 - finish current activity and start another activity when screen being off (black) 选择 listItem 时如何启动新活动? - How do I start a new activity when a listItem is selected? 单击选项卡后如何启动新活动? - How do I start a new activity when a tab is clicked? 当屏幕关闭或锁定在 Java 的 Android 工作室中时,如何继续运行活动? - How can I keep running an activity when screen is off or locked in Android studio in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM