Android 從底層實現讓應用殺不死(1)
情景還原:
我的應用呼叫了Notification,但是如果被流氓清理軟體殺死,在有些機型出現Notification沒有被抹除的情況,因為喪失了對Notification的引用,使用者也無法抹除這個Notification,這將大大降低使用者體驗。於是,我想出瞭如果我的應用可以不死,主動清除Notification。
既然開始做了,乾脆做了個小調查。
經獲取Root許可權TaskManager清除之後能重生的應用使用的方式(測試機型:魅藍Note )使用方法
public
class
YourDaemonService
{
public
void
onCreate()
{
Daemon.run( this ,YourDaemonService. class , 60 );
}
}
|
原理分析:
一、首先呼叫這個函式 開啟守護程序
Daemon.run(this, DaemonService.class, Daemon.INTERVAL_ONE_MINUTE * 2);
public
class
Daemon {
/**
*
Run daemon process. *
*
@param context context
*
@param daemonServiceClazz the name of daemon service class
*
@param interval the interval to check
*/
public
static
void
run( final
Context context, final
Class<?> daemonServiceClazz,
final
int
interval) { new
Thread( new
Runnable() {
@Override
public
void
run() {
Command.install(context,
BIN_DIR_NAME, DAEMON_BIN_NAME);
start(context,
daemonServiceClazz, interval);
}
}).start();
}
}
|
二、install 安裝庫
public
class
Daemon {
|