StandBy like activity

555 views Asked by At

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;)

2

There are 2 answers

1
Andrei Anischevici On

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

1
Mostafa Gazar On

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);