2015年6月17日星期三

Android:注册一个方向传感器的回调,能够让app常驻内存不被杀死

一个意外的发现。这能使app的oom_adj即使在后台时也是1(值越小越不容易被杀,一般后台app是7, 后台service是8)


随便什么地方加上一段代码就行了:

?
1
2
3
4
5
6
OrientationEventListener mOrientationListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_UI) {
    public void onOrientationChanged(int orientation) {
    }
};
if (mOrientationListener.canDetectOrientation())
    mOrientationListener.enable();

进一步设想,换成别的传感器类型也行,没有做跟多的实验,估计会有更少费系统能量的方法。

这个方法比起   startForeground(随便什么整数, new Notification(/*不要参数*/)) 的效果还要好,该方法只能让oom_adj降到2。

没有评论:

发表评论