如何防止自身服务被杀

因为我之前也在这个上面纠结过。

如360  QQ管家这些软件老是提供什么服务大
什么内存大然后把我们干掉。

现在就让大家如何防杀吧~

相信大家也在网上查过这些类似的吧?但是用了却没用(我不知道是不是这样);

第一:

@Deprecated
    public final void setForeground(boolean isForeground) { 
        Log.w(TAG, "setForeground: ignoring old API call on " + getClass().getName()); 
    }

该方法再后续版本中 ,未实现,就一空方法。因此调与没调没差。

而 由于在新的api中 有一

   
public final void startForeground(int id, Notification notification) { 
        try { 
            mActivityManager.setServiceForeground( 
                    new ComponentName(this, mClassName), mToken, id, 
                    notification, true); 
        } catch (RemoteException ex) { 
        } 
    }

该方法才真正实现了foreground,所以在提升自己优先级的时候记得使用的是:startForeground
如:

private void setForeground() 
        { 
                Notification note = new Notification(0, null, System.currentTimeMillis()); 
                note.flags |= Notification.FLAG_NO_CLEAR; 
                startForeground(42, note); 
        }

然后在服务里直接调用。


另外想分享的是另外个思路。
由于android
中可以通过android:process=""来指定进程名,在直接看360等软件杀的时候发现他对某些进程名不感冒。(不知道是不是有意的还是无意的)。
如:你把你的android:process="com.android.acore"
这种android的核心进程后,在他所列出要杀的名单中就不存在了 。即使在QQ管家里 上面标注的也是 “不推荐” 。
因此在对进程名方面 ,也是他们的一个BUG吧。

 

posted on 2012-11-12 15:23  nuliniao  阅读(630)  评论(0编辑  收藏  举报