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

前端 未结 3 1108
Happy的楠姐
Happy的楠姐 2020-11-29 08:00

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

                

        
相关标签:
3条回答
  • 2020-11-29 08:19

    This is the best example I've found http://androidexample.com/Screen_Wake_Sleep_Event_Listner_Service_-_Android_Example/index.php?view=article_discription&aid=91&aaid=115

    0 讨论(0)
  • 2020-11-29 08:29

    Actullay i was faceing this issue but i resolve it succeessfully

    1) start service from your main activity

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

    2) register reciver in service class.

    @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

    0 讨论(0)
  • 2020-11-29 08:35

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

    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_ON is one of those. See this previous question for light blue advice on that topic.

    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".

    0 讨论(0)
提交回复
热议问题