Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StandBy like activity

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

like image 539
axl coder Avatar asked Dec 05 '14 17:12

axl coder


1 Answers

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

like image 54
Andrei Anischevici Avatar answered Sep 28 '22 23:09

Andrei Anischevici