防止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