Android App 防止 后台服务 被杀掉

来源:互联网 发布:网络兼职被骗已追回 编辑:程序博客网 时间:2024/05/20 20:45


方法一

 // 申请设备电源锁,在服务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;    }  }

方法二


这个方法是防止手机休眠。你的服务就会一直运行下去,不会被系统kill掉。亲测可行。
还有在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
 


0 0
原创粉丝点击