你需要使用广播接收器,那么你可以做所有的改变,你可以做一次你的onReceive()调用。
在你的清单中添加接收器和意图过滤这样
<receiver android:name=".ScreenOnOffReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
</intent-filter>
你可以把所有的逻辑在Java代码中。
public class ScreenOnOffReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
{
// some code
}
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
{
// put your all logic here
}
}
}
希望这会有所帮助。