码迷,mamicode.com
首页 > 其他好文 > 详细

设置休眠时间,获得休眠时间,监听屏幕状态,服务启动界面

时间:2016-08-19 20:47:20      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

public class MyService extends Service {
@Nullable
@Override

public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
Log.e("zz","zz");
MyService.ScreenListener mScreenListener= new MyService.ScreenListener(this);
mScreenListener.begin(new MyService.ScreenListener.ScreenStateListener() {
@Override
public void onScreenOn() {
// Toast.makeText(MainActivity.this,"屏幕已开启",Toast.LENGTH_SHORT).show();
Log.e("屏幕","屏幕一开启");
// Intent intent=new Intent(MyService.this,SleepActivity.class);
// startActivity(intent);

}

@Override
public void onScreenOff() {
// Toast.makeText(MainActivity.this,"屏幕已开启",Toast.LENGTH_SHORT).show();
Log.e("屏幕","屏幕一休眠");
Intent intent=new Intent(MyService.this,SleepActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(intent);
}

@Override
public void onUserPresent() {
// Toast.makeText(MainActivity.this,"屏幕已解锁",Toast.LENGTH_SHORT).show();
Log.e("屏幕","屏幕一解锁");
}
});
}

public static class ScreenListener {
private Context mContext;
private ScreenBroadcastReceiver mScreenReceiver;
private ScreenStateListener mScreenStateListener;

public ScreenListener(Context context) {
mContext = context;
mScreenReceiver = new ScreenBroadcastReceiver();
}

/**
* screen状态广播接收者
*/
private class ScreenBroadcastReceiver extends BroadcastReceiver {
private String action = null;

@Override
public void onReceive(Context context, Intent intent) {
action = intent.getAction();
if (Intent.ACTION_SCREEN_ON.equals(action)) { // 开屏
mScreenStateListener.onScreenOn();
} else if (Intent.ACTION_SCREEN_OFF.equals(action)) { // 锁屏
mScreenStateListener.onScreenOff();
} else if (Intent.ACTION_USER_PRESENT.equals(action)) { // 解锁
mScreenStateListener.onUserPresent();
}
}
}

/**
* 开始监听screen状态
*
* @param listener
*/
public void begin(ScreenListener.ScreenStateListener listener) {
mScreenStateListener = listener;
registerListener();
getScreenState();
}


/**
* 获取screen状态
*/
private void getScreenState() {
PowerManager manager = (PowerManager) mContext
.getSystemService(Context.POWER_SERVICE);
if (manager.isScreenOn()) {
if (mScreenStateListener != null) {
mScreenStateListener.onScreenOn();
}
} else {
if (mScreenStateListener != null) {
mScreenStateListener.onScreenOff();
}
}
}

/**
* 停止screen状态监听
*/
public void unregisterListener() {
mContext.unregisterReceiver(mScreenReceiver);
}

/**
* 启动screen状态广播接收器
*/
private void registerListener() {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
mContext.registerReceiver(mScreenReceiver, filter);
}

public interface ScreenStateListener {// 返回给调用者屏幕状态信息

public void onScreenOn();

public void onScreenOff();

public void onUserPresent();
}
}
}
在服务里创建广播,并且暴露接口返回广播内容。
/**
* 获得休眠时间 毫秒
*/
private int getScreenOffTime() {
int screenOffTime = 0;
try {
screenOffTime = Settings.System.getInt(getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT);
} catch (Exception localException) {

}
return screenOffTime;
}

/**
* 设置休眠时间 毫秒
*/
private void setScreenOffTime(int paramInt) {
try {
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT,
paramInt);
} catch (Exception localException) {
localException.printStackTrace();
}
}
获取休眠时间,设置休眠时间

设置休眠时间权限
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
设置从服务打开界面权限
<uses-permission android:name="android.permission.WAKE_LOCK" />






设置休眠时间,获得休眠时间,监听屏幕状态,服务启动界面

标签:

原文地址:http://www.cnblogs.com/xiguameng/p/5788897.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!