防止APP退到被安卓系统清理

一个是尽量提高APP权限,无非就是保持APP始终界面在前台

二是使用守护进程方法,被清理了立刻自己启动,

三是前台跟后台进程分开,被重启了恢复原始环境。

 

 

// 申请设备电源锁,在服务start的时候。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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;
     }
}

这个方法是防止手机休眠。你的服务就会一直运行下去,不会被系统kill掉。亲测可行。
还有在onStartCommand里面最后return super.onStartCommand(intent, START_STICKY, startId);

 

QQ在通知栏不是设了一个不同于一般通知的通知

代码如下:

1
2
3
4
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);

转载于:https://www.cnblogs.com/lucktian/p/6952840.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值