android.intent.action.SCREEN_ON 不能作为接收者意图过滤器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2575242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:22:26  来源:igfitidea点击:

android.intent.action.SCREEN_ON doesn't work as a receiver intent filter

androidbroadcastreceiver

提问by Jim Blackler

I'm trying to get a BroadcastReceiver invoked when the screen is turned on. In my AndroidManifest.xml I have specified :

我试图在屏幕打开时调用 BroadcastReceiver。在我的 AndroidManifest.xml 中,我指定了:

                <receiver android:name="IntentReceiver">
                    <intent-filter>
                            <action android:name="android.intent.action.SCREEN_ON"></action>
                    </intent-filter>
                </receiver>

However it seems the receiver is never invoked (breakpoints don't fire, log statements ignored). I've swapped out SCREEN_ON for BOOT_COMPLETED for a test, and this doesget invoked.

然而,接收器似乎从未被调用(断点不会触发,日志语句被忽略)。我已经将 SCREEN_ON 换成 BOOT_COMPLETED 进行测试,这确实被调用了。

This is in a 1.6 (SDK level 4) project.

这是在 1.6(SDK 级别 4)项目中。

A Google Code Search revealed this, I downloaded the project and synced it, converted it to work with latest tools, but it too is not able to intercept that event.

谷歌代码搜索揭示了这一点,我下载了该项目并将其同步,将其转换为使用最新工具,但它也无法拦截该事件。

http://www.google.com/codesearch/p?hl=en#_8L9bayv7qE/trunk/phxandroid-intent-query/AndroidManifest.xml&q=android.intent.action.SCREEN_ON

http://www.google.com/codesearch/p?hl=en#_8L9bayv7qE/trunk/phxandroid-intent-query/AndroidManifest.xml&q=android.intent.action.SCREEN_ON

Is this perhaps no longer supported?

这可能不再受支持吗?

Previously I have been able to intercept this event successfully with a call to Context.registerReceiver() like so

以前我已经能够像这样调用 Context.registerReceiver() 成功拦截这个事件

registerReceiver(new BroadcastReceiver() {

  @Override
  public void onReceive(Context context, Intent intent) {
    // ... 
  }
}, new IntentFilter(Intent.ACTION_SCREEN_ON));

However this was performed by a long-living Service. Following sage advice from CommonsWare I have elected to try to remove the long-living Service and use different techniques. But I still need to detect the screen off and on events.

然而,这是由一个长期存在的服务执行的。遵循 CommonsWare 的明智建议,我选择尝试删除长期存在的服务并使用不同的技术。但我仍然需要检测屏幕关闭和开启事件。

回答by CommonsWare

Following sage advice from CommonsWare I have elected to try to remove the long-living Service and use different techniques.

遵循 CommonsWare 的明智建议,我选择尝试删除长期存在的服务并使用不同的技术。

Actually, I believe my advice was more of a light blue... :-)

实际上,我相信我的建议更像是浅蓝色... :-)

But I still need to detect the screen off and on events.

但我仍然需要检测屏幕关闭和开启事件。

There are certain events that Android does not want to start up new processes for, so the device does not get too slow from all sorts of stuff all having to run at once. ACTION_SCREEN_ONis one of those. See this previous questionfor light blue advice on that topic.

Android 不想为某些事件启动新进程,因此设备不会因为必须同时运行的各种内容而变得太慢。ACTION_SCREEN_ON是其中之一。有关该主题的浅蓝色建议,请参阅上一个问题

So, you need to ask yourself, "Self, do I really need to get control on those events?". The core Android team would like it if your answer was "no".

所以,你需要问问自己,“我自己,我真的需要控制这些事件吗?”。如果您的回答是“否”,那么核心 Android 团队会喜欢它。

回答by pradeep

Actullay i was faceing this issue but i resolve it succeessfully

Actullay 我正面临这个问题,但我成功地解决了它

1) start service from your main activity

1)从您的主要活动开始服务

   Intent i = new Intent(MainActivity.this, UpdateService.class);
    startService(i);

2) register reciver in service class.

2) 在服务类中注册接收者。

@Override
public void onCreate() {
    super.onCreate();



    // REGISTER RECEIVER THAT HANDLES SCREEN ON AND SCREEN OFF LOGIC
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    BroadcastReceiver mReceiver = new ScreenReciever();
    registerReceiver(mReceiver, filter);
}

3) Done

3)完成