Android给App保活(10种⽅案)1.Activity 1像素保活
public class Activity1 extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
setContentView(R.layout.activity_1);
Window window = getWindow();
window.setGravity(Gravity.LEFT|Gravity.TOP);
WindowManager.LayoutParams layoutParams = Attributes();
layoutParams.width = 1;
layoutParams.height = 1;
layoutParams.x = 1;
layoutParams.y = 1;
window.setAttributes(layoutParams);
}
}
2.前台服务
public class ForegroundService extends Service {
private final static int SERVICE_ID = 1;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
if (Build.VERSION.SDK_INT<Build.VERSION_CODES.JELLY_BEAN_MR2){
//4.3以下
startForeground(SERVICE_ID,new Notification());
}else if (Build.VERSION.SDK_INT<Build.VERSION_CODES.O){
//7.0以下
startForeground(SERVICE_ID,new Notification());
/
/删除通知栏
startService(new Intent(this,InnerService.class));
}else {
//8.0以上
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//NotificationManager.IMPORTANCE_MIN 通知栏消息的重要级别最低,不让弹出
//IMPORTANCE_MIN 前台时,在阴影区能看到,后台时阴影区不消失,增加显⽰ IMPORTANCE_NONE时⼀样的提⽰
//IMPORTANCE_NONE app在前台没有通知显⽰,后台时有
NotificationChannel channel = new NotificationChannel("channel", "keep", NotificationManager.IMPORTANCE_NONE);
if (notificationManager!=null){
Notification notification = new Notification.Builder(this, "channel").build();
startForeground(SERVICE_ID,notification);
}
}
}
private static class InnerService extends Service{
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Log.d(KeepAliveApp.TAG, "onCreate: ");
startForeground(SERVICE_ID,new Notification());
stopSelf();
}
}
}
3.⼴播拉活
在发⽣特定系统事件时,系统会发出⼴播,通过在 AndroidManifest 中静态注册对应的⼴播,即可在发⽣响应事件时拉活。
4.利⽤系统机制拉活
START_STICKY:
“粘性”。如果service进程被kill掉,保留service的状态为开始状态,但不保留递送的intent对象。随后系统会尝试重新创建service,由于服务状态为开始状态,所以创建服务后⼀定会调⽤onStartCommand(Intent,int,int)⽅法。如果在此期间没有任何启动命令被传递到service,那么参数Intent将为null。
START_NOT_STICKY:
“⾮粘性的”。使⽤这个返回值时,如果在执⾏完onStartCommand后,服务被异常kill掉,系统不会⾃动重启该服务。
START_REDELIVER_INTENT:
重传Intent。使⽤这个返回值时,如果在执⾏完onStartCommand后,服务被异常kill掉,系统会⾃动重启该服务,并将Intent的值传⼊。
START_STICKY_COMPATIBILITY:
START_STICKY的兼容版本,但不保证服务被kill后⼀定能重启。
只要 targetSdkVersion 不⼩于5,就默认是 START_STICKY。 但是某些ROM 系统不会拉活。并且经过测试,Service 第⼀次被异常杀死后很快被重启,第⼆次会⽐第⼀次慢,第三次⼜会⽐前⼀次慢,⼀旦在短时间内 Service 被杀死4-5次,则系统不再拉起。
public class StickyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
StartCommand(intent, flags, startId);
}
}
5.账户同步拉活
/**
* 创建可添加⽤户
*/
public class AuthenticationService extends Service {
private AccountAuthenticator accountAuthenticator;
@Nullable
@Override
public IBinder onBind(Intent intent) {
IBinder();
}
@Override
public void onCreate() {
accountAuthenticator = new AccountAuthenticator(this);
}
public static class AccountAuthenticator extends AbstractAccountAuthenticator {
public AccountAuthenticator(Context context) {
super(context);
super(context);
}
@Override
public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
return null;
}
@Override
public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String aut
hTokenType,                                String[] requiredFeatures, Bundle options) throws NetworkErrorException {
return null;
}
@Override
public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account,
Bundle options) throws NetworkErrorException {
return null;
}
@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account,
String authTokenType, Bundle options) throws NetworkErrorException {
return null;
}
@Override
public String getAuthTokenLabel(String authTokenType) {
return null;
}
@Override
public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account,
String authTokenType, Bundle options) throws NetworkErrorException {
return null;
}
@Override安卓intent用法
public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account,
String[] features) throws NetworkErrorException {
return null;
}
}
}
public class AccountHelper {
private static final String TAG = "AccountHelper";
private static final String ACCOUNT_TYPE = "com.zsl.keepalive.account";
private static final String AUTHORITY = "com.zsl.keepalive.provider";
/
**
* 添加账号
*
* @param context
*/
public static void addAccount(Context context) {
AccountManager accountManager = (AccountManager) SystemService(Context.ACCOUNT_SERVICE);        // 获得此类型的账户
// 需要增加权限  GET_ACCOUNTS
Account[] accounts = AccountsByType(ACCOUNT_TYPE);
if (accounts.length > 0) {
Log.e(TAG, "账户已存在");
return;
}
Account account = new Account("zsl", ACCOUNT_TYPE);
// 给这个账户类型添加⼀个账户
// 需要增加权限  AUTHENTICATE_ACCOUNTS
accountManager.addAccountExplicitly(account, "keepalive", new Bundle());
}
/**
* 设置账户⾃动同步
*/
public static void autoSync() {
Account account = new Account("zsl", ACCOUNT_TYPE);
// 下⾯三个都需要同⼀个权限  WRITE_SYNC_SETTINGS
// 设置同步
ContentResolver.setIsSyncable(account, AUTHORITY, 1);
// ⾃动同步
ContentResolver.setSyncAutomatically(account, AUTHORITY, true);
// 设置同步周期
ContentResolver.addPeriodicSync(account, AUTHORITY, new Bundle(), 1);
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。