方法一 // 申请设备电源锁,在服务start的时候。
代码:
private WakeLock mWakeLock;
private void acquireWakeLock()
{
if (null == mWakeLock)
{
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "LoginService");
if (null != mWakeLock) {
mWakeLock.acquire();
}
}
}
// 释放设备电源锁,在服务Destory的时候
private void releaseWakeLock()
{
if (null != mWakeLock)
{
mWakeLock.release();
mWakeLock = null;
}
}
方法二还有在onStartCommand里面最后return super.onStartCommand(intent, START_STICKY, startId);楼上刚刚已经说了 方法三QQ在通知栏不是设了一个不同于一般通知的通知嘛~
代码如下: 代码:
Notification notification = new Notification(R.drawable.qqbatch_logo, getString(R.string.app_name),System.currentTimeMillis()); PendingIntent pendingintent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); notification.setLatestEventInfo(this, "xxxx", "xxxxxxxxx", pendingintent); startForeground(0x111, notification); 方法四各种广播的监听
结束进程的方法http://blog.csdn.net/huxueyan521/article/details/8921976
|
本文介绍了一种防止手机休眠的服务运行方法,通过获取设备电源锁来确保服务持续运行,避免被系统终止。此外,还介绍了如何利用通知栏显示特定通知,以及通过广播监听和结束进程的方法来优化服务管理。

2670

被折叠的 条评论
为什么被折叠?



