【Android 进程保活】应用进程拉活 ( 账户同步拉活

您所在的位置:网站首页 安卓双进程保活 【Android 进程保活】应用进程拉活 ( 账户同步拉活

【Android 进程保活】应用进程拉活 ( 账户同步拉活

#【Android 进程保活】应用进程拉活 ( 账户同步拉活 | 来源: 网络整理| 查看: 265

文章目录 一、 账户简介二、 账号服务注册1、 服务 Service2、 AndroidManifest.xml 中注册 Service3、 账号验证资源4、查看账号设置 三、 源码资源

一、 账户简介

Android 手机的设备的 " 设置 " 中 , 有 " 账号 " 选项 ;

在这里插入图片描述

点进去后的账号页面 :

在这里插入图片描述

点击添加账号 , 有以下选项 :

在这里插入图片描述

由开发者开发的应用也可以添加账户 ;

上述的应用都是账户拉活的同行 ;

应用 APP 中可以注册 " 账户服务 Service " , 应用安装后 , 如果系统发现应用中有该类型服务 , 就会为该应用开放添加账户的功能 ;

系统通过 Binder 机制 , 操作用户的 " 账户服务 Service " ;

第三方应用可以通过该账户服务 , 将数据同步到服务器中 ;

系统在进行应用账户同步时 , 会自动将对应的应用拉活 ;

Google 官方提供了账户同步案例 , https://github.com/googlearchive/android-BasicSyncAdapter , 已经停止维护了 , 但是项目还是有参考价值的 ; ( 源码放在了本博客的资源文件中 )

在上述项目中指向了推荐的后台任务示例 https://github.com/android/background-tasks-samples ; ( 源码放在了本博客的资源文件中 )

二、 账号服务注册

注册一个服务 , 该服务的 onBind 方法返回 AbstractAccountAuthenticator 对象的 Binder , 只要该应用安装 , 就可以在 " 设置 -> 账号 " 中查看该应用的账号 ;

1、 服务 Service package kim.hsl.keep_progress_alive.account_service; import android.accounts.AbstractAccountAuthenticator; import android.accounts.Account; import android.accounts.AccountAuthenticatorResponse; import android.accounts.NetworkErrorException; import android.app.Service; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.IBinder; public class AuthenticationService extends Service { /** * 账户验证器 */ AccountAuthenticator mAccountAuthenticator; public AuthenticationService() { } @Override public IBinder onBind(Intent intent) { return mAccountAuthenticator.getIBinder(); } @Override public void onCreate() { super.onCreate(); // 创建账户验证器 mAccountAuthenticator = new AccountAuthenticator(this); } /** * 账户验证器 */ class AccountAuthenticator extends AbstractAccountAuthenticator { public AccountAuthenticator(Context context) { super(context); } @Override public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) { return null; } @Override public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, 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 public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features) throws NetworkErrorException { return null; } } } 2、 AndroidManifest.xml 中注册 Service

核心配置 :

完整配置文件 :

3、 账号验证资源

在 res 目录下创建 xml 目录 , 在 xml 目录下创建 account_authenticator.xml 文件 ,

在这里插入图片描述

4、查看账号设置

只要在应用中注册了该服务 , 手机系统中安装了该应用 , 就可以在 " 设置 -> 账号 -> 添加账号 " 界面中 , 查看到该应用的账号 ;

在这里插入图片描述

三、 源码资源

源码资源 :

GitHub 地址 : https://github.com/han1202012/Keep_Progress_AliveCSDN 源码快照 : https://download.csdn.net/download/han1202012/16602670

在这里插入图片描述



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3