博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
提高Service优先级,避免被系统回收
阅读量:4208 次
发布时间:2019-05-26

本文共 4225 字,大约阅读时间需要 14 分钟。

设置一个不会被清除的Notification就能实现Service不会被系统回收,也不会被一般的第三方内存管理工具给清除掉;如果你不想显示一个Notification的话,可以这么做:
Notification noti = new Notification();startForeground(YOUR_NOTIFICATION_ID, noti);
 
public final void startForeground (int id,  notification)
Parameters
id The identifier for this notification as per .
notification The Notification to be displayed.

Make this service run in the foreground, supplying the ongoing notification to be shown to the user while in this state. By default services are background, meaning that if the system needs to kill them to reclaim more memory (such as to display a large page in a web browser), they can be killed without too much harm. You can set this flag if killing your service would be disruptive to the user, such as if your service is performing background music playback, so the user would notice if their music stopped playing.

If you need your application to run on platform versions prior to API level 5, you can use the following model to call the the older setForeground() or this modern method as appropriate:

private static final Class
[] mSetForegroundSignature = new Class[] {
    boolean.class};private static final Class
[] mStartForegroundSignature = new Class[] {
    int.class, Notification.class};private static final Class
[] mStopForegroundSignature = new Class[] {
    boolean.class};private NotificationManager mNM;private Method mSetForeground;private Method mStartForeground;private Method mStopForeground;private Object[] mSetForegroundArgs = new Object[1];private Object[] mStartForegroundArgs = new Object[2];private Object[] mStopForegroundArgs = new Object[1];void invokeMethod(Method method, Object[] args) {
    try {
        method.invoke(this, args);    } catch (InvocationTargetException e) {
        // Should not happen.        Log.w("ApiDemos", "Unable to invoke method", e);    } catch (IllegalAccessException e) {
        // Should not happen.        Log.w("ApiDemos", "Unable to invoke method", e);    }}/** * This is a wrapper around the new startForeground method, using the older * APIs if it is not available. */void startForegroundCompat(int id, Notification notification) {
    // If we have the new startForeground API, then use it.    if (mStartForeground != null) {
        mStartForegroundArgs[0] = Integer.valueOf(id);        mStartForegroundArgs[1] = notification;        invokeMethod(mStartForeground, mStartForegroundArgs);        return;    }    // Fall back on the old API.    mSetForegroundArgs[0] = Boolean.TRUE;    invokeMethod(mSetForeground, mSetForegroundArgs);    mNM.notify(id, notification);}/** * This is a wrapper around the new stopForeground method, using the older * APIs if it is not available. */void stopForegroundCompat(int id) {
    // If we have the new stopForeground API, then use it.    if (mStopForeground != null) {
        mStopForegroundArgs[0] = Boolean.TRUE;        invokeMethod(mStopForeground, mStopForegroundArgs);        return;    }    // Fall back on the old API.  Note to cancel BEFORE changing the    // foreground state, since we could be killed at that point.    mNM.cancel(id);    mSetForegroundArgs[0] = Boolean.FALSE;    invokeMethod(mSetForeground, mSetForegroundArgs);}@Overridepublic void onCreate() {
    mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);    try {
        mStartForeground = getClass().getMethod("startForeground",                mStartForegroundSignature);        mStopForeground = getClass().getMethod("stopForeground",                mStopForegroundSignature);        return;    } catch (NoSuchMethodException e) {
        // Running on an older platform.        mStartForeground = mStopForeground = null;    }    try {
        mSetForeground = getClass().getMethod("setForeground",                mSetForegroundSignature);    } catch (NoSuchMethodException e) {
        throw new IllegalStateException(                "OS doesn't have Service.startForeground OR Service.setForeground!");    }}@Overridepublic void onDestroy() {
    // Make sure our notification is gone.    stopForegroundCompat(R.string.foreground_service_started);}

转载地址:http://eolli.baihongyu.com/

你可能感兴趣的文章
ARM e7f000f0 udf 指令异常
查看>>
IDENTITY_INSERT
查看>>
SQL SERVER常用语法汇总
查看>>
SQL SEVER碎片化及压缩整理
查看>>
sqlserver常用函数大全
查看>>
SQLServer批量重建索引(整理)
查看>>
txt导入到table
查看>>
当前正在执行的SQL
查看>>
获取SQL Server数据库元数据的几种方法
查看>>
聚集索引重建之后
查看>>
区分大小写
查看>>
收缩数据库
查看>>
数据库修复
查看>>
找出执行时间最长的10条SQL
查看>>
主键就是聚集索引吗
查看>>
一、存储结构   在SQL Server中,有许多不同的可用排列规则选项。   二进制:按字符的数字表示形式排序(ASCII码中,用数字32表示空格,用68表示字母"D")。因为所有内容都表示为数字
查看>>
SqlServer2008数据备份以及远程备份
查看>>
SQL Server 非聚集索引的覆盖,连接,交叉和过滤 (第二篇)
查看>>
备份恢复
查看>>
sqlserver 备份
查看>>