7

I would like to create a StandBy activity for my device, and so far I created an activity that when is called will turn off my display.

The code is the following:

public class MainActivity extends Activity {
private SensorManager mSensorManager;
private PowerManager mPowerManager;
private WindowManager mWindowManager;
private WakeLock mWakeLock;
private Button button;
private TextView textView;

/** Called when the activity is first created. */
@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        // Get an instance of the SensorManager
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

        // Get an instance of the PowerManager
        mPowerManager = (PowerManager) getSystemService(POWER_SERVICE);

        // Get an instance of the WindowManager
        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        mWindowManager.getDefaultDisplay();

        setContentView(R.layout.activity_main);
        // textView = (TextView)findViewById(R.id.textView1);
        button = (Button) findViewById(R.id.testText);
        button.setOnClickListener(mButtonStopListener);

        mWakeLock = mPowerManager.newWakeLock(
                PowerManager.PARTIAL_WAKE_LOCK, "Your Tag");
        // mWakeLock.acquire();
        final WindowManager.LayoutParams params = getWindow()
                .getAttributes();
        params.screenBrightness = 0;
        getWindow().setAttributes(params);

    } catch (final Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.e("onCreate", e.getMessage());
    }
} // END onCreate

View.OnClickListener mButtonStopListener = new OnClickListener() {
    @Override
    public void onClick(final View v) {
        try {
            if (mWakeLock.isHeld()) {
                mWakeLock.release();
                System.err.println("mWakeLock.release()  onTouch");
            }
        } catch (final Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.e("onPause", e.getMessage());
        }

    }
};

@Override
protected void onResume() {
    super.onResume();

    try {
        if (mWakeLock.isHeld()) {
            System.err.println("mWakeLock.release() onResume");
            mWakeLock.release();
        } else {
            System.err.println("mWakeLock.acquire() onResume");
            mWakeLock.acquire();

        }
    } catch (final Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.e("onResume", e.getMessage());
    }

}

@Override
protected void onPause() {
    super.onPause();


}

}

As I said this code enable me to turn off the screen, and I'm able to turn on the screen clicking twice the power button (I don't know why I have two click the button twice, but this is a secondary issue).

The main problem is that when the display turn off the action ACTION_SCREEN_OFF is generated, and as a consequence the android EthernetService disable my connection. Anyone know how to keep the connection active?

Thanks;)

axl coder
  • 739
  • 3
  • 19
  • I'm not exactly sure what you're trying, but would it be possible to put your connection in a Service which holds the wake-lock? If you're downloading something, then drop the wake lock so the device can do a full sleep. If you're just trying to keep a connection open, then it could be a pretty severe battery sink. – DeeV Dec 05 '14 at 17:54
  • My problem is that I need to wake up my device (even the screen) with a message received from my connection, so I can't loose the connection (I don't mind about battery problem, the only need of screen sleep is to save my display life). – axl coder Dec 08 '14 at 22:41
  • if you hold a PARTIAL_WAKE_LOCK the internet connection should not be disabled. – Goran Horia Mihail Dec 09 '14 at 10:02
  • As I posted I use a PARTIAL_WAKE_LOCK, this ensure only that the CPU doesn't go to sleep, but my ethernet interface is disabled. – axl coder Dec 09 '14 at 10:04

2 Answers2

2

From your question I see that you need to keep the Ethernet service alive.

First of all, the issue could be solved if you had WiFi connectivity on the device and a WiFi connection available. In this case you could use the WifiLock, together with a Partial WakeLock - see this question.

If, however, you really need to use the Ethernet connection, you could try listening for ACTION_SCREEN_OFF intent and then trying to re-enable the Ethernet connectivity by following the instructions in the top reply to this question. Note though, that in this case your device may have to be rooted.

Hope this helps..

Community
  • 1
  • 1
  • Thanks for your reply, unfortunately I can't use Wifi. Even the second solution you suggest is not good for me, because I need the ethernet connection to stay alive, and not to go down and then up again:( – axl coder Dec 15 '14 at 10:11
2

I'm able to turn on the screen clicking twice the power button (I don't know why I have two click the button twice, but this is a secondary issue)

That happens because technically the screen is still ON with brightness set to zero, so what actually happens is you turn the screen OFF with the first power button press then ON, that's two clicks.

I suspect that the connection was disabled only after the first power button press. I do not see where in the code you provided you restore the screen brightness.

Try setting the screenBrightness to 0.2 instead of zero and monitor the connection state or using log. Also the following the code should be enough.

Turn the screen "kinda" OFF

WindowManager.LayoutParams params = getWindow().getAttributes();
params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
previousScreenBrightness = params.screenBrightness;
params.screenBrightness = 0;
getWindow().setAttributes(params);

Turn the screen back ON

WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = previousScreenBrightness;
getWindow().setAttributes(params);
Mostafa Gazar
  • 2,487
  • 18
  • 23