Search code examples
androidbroadcastreceivermotorola-droid

Android broadcast receiver (screen) not working on Moto g


I am trying to capture screen on/off event on moto g phone. Main activity has

Intent intentscreen = new Intent(getApplicationContext(), ScreenService.class);
startService(intentscreen);

where as "ScreenService" looks like:

public int onStartCommand(Intent intent, int flags, int startId)
    {
        if (intent!=null)
        {

            super.onCreate();
            WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
            wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL , "MyWifiLock");
            if(!wifiLock.isHeld()){
                               wifiLock.acquire();
            }
            IntentFilter filter = new IntentFilter();
            filter.addAction(Intent.ACTION_SCREEN_ON);
            filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
            registerReceiver(this.mybroadcast, filter);
            return super.onStartCommand(intent, flags, startId);
        } 
        return START_STICKY;
}
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        unregisterReceiver(mybroadcast);
        if(wifiLock.isHeld()){
                  wifiLock.release();
         }
    }

while the receiver class looks like:

public class ScreenReceiver extends BroadcastReceiver {

       @Override
       public void onReceive(Context context, Intent intent) {
           Log.d("Screen", "--- I Fire!");

       }
    }

Manifest file has the permission, but the issue is that my phone receives the event for first few hours and then it stops receiving i.e. Log.d("Screen", "--- I Fire!"); is not displayed in my log cat.

Also to add further, if I acquire PowerManager.WakeLock wakeLock it doesn't work at all, but if I acquire WifiLock wifiLock = null, this works for few hours, any suggestion that how to keep the Wifi active probably?

Any suggestion that what possibly be wrong? Or any other type of lock should I acquire? I dont want to use a lot of battery for locks.


Solution

  • If your service gets killed by the system due to low memory it will later be restarted with null passed as the intent. When this happens it appears you would lose broadcast receiver as you do nothing if intent is null.

    Seems like it's not possible to get these broadcasts from a static receiver in the android manifest. So you'll probably have to keep your service.

    Something along these lines:

    public void onCreate (){
        super.onCreate();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        registerReceiver(mybroadcast, filter);
    }
    
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        return START_STICKY;
    }
    
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        unregisterReceiver(mybroadcast);
    }