android – StandBy喜欢活动

前端之家收集整理的这篇文章主要介绍了android – StandBy喜欢活动前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想为我的设备创建一个StandBy活动,到目前为止,我创建了一个活动,在调用时将关闭我的显示.

代码如下:

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


}

}

正如我所说的这个代码使我能够关闭屏幕,我可以打开屏幕点击两次电源按钮(我不知道为什么我有两次点击按钮两次,但这是次要问题) .

主要问题是当显示关闭时,生成ACTION_SCREEN_OFF动作,因此android EthernetService禁用我的连接.任何人都知道如何保持连接活动?

谢谢;)

解决方法

根据您的问题,我发现您需要保持以太网服务的活跃性.

首先,如果您在设备上有WiFi连接并且可以使用WiFi连接,则可以解决该问题.在这种情况下,您可以使用WifiLock和Partial WakeLock – see this question.

但是,如果您确实需要使用以太网连接,则可以尝试listening for ACTION_SCREEN_OFF intent,然后按照top reply to this question中的说明尝试重新启用以太网连接.请注意,在这种情况下,您的设备可能必须植根.

希望这可以帮助..

原文链接:https://www.f2er.com/android/314718.html

猜你在找的Android相关文章