【Android 进程保活】应用进程拉活 ( 账户同步拉活 | 账号服务注册 | 源码资源 )
一、 账户简介
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
核心配置 :
<!-- 用于账户同步拉活 -->
<service
android:name=".account_service.AuthenticationService"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator"/>
</intent-filter>
<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/account_authenticator"/>
</service>
完整配置文件 :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kim.hsl.keep_progress_alive">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Keep_Progress_Alive">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--
设置最近任务列表中不显示该 Activity 组件 ( 不要被用户察觉 )
android:excludeFromRecents="true"
设置 Activity 亲和性
让该界面在一个独立的任务栈中 , 不要与本应用的其它任务栈放在一起
避免解除锁屏后 , 关闭 1 像素界面 , 将整个任务栈都唤醒
android:taskAffinity="kim.hsl.keep_progress_alive.alive"
-->
<activity
android:name=".one_pixel_activity.OnePixelActivity"
android:excludeFromRecents="true"
android:taskAffinity="kim.hsl.keep_progress_alive.onepixel"
android:theme="@style/OnePixelActivityTheme" />
<!-- 用于提权的前台进程 -->
<service
android:name=".foreground_service.ForegroundService"
android:enabled="true"
android:exported="true" />
<!-- 用于提权的前台进程, 关闭通知操作 -->
<service
android:name=".foreground_service.CancelNotificationService"
android:enabled="true"
android:exported="true" />
<!-- 系统 Service 机制拉活 -->
<service
android:name=".stick_service.StickService"
android:enabled="true"
android:exported="true" />
<!-- 用于账户同步拉活 -->
<service
android:name=".account_service.AuthenticationService"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator"/>
</intent-filter>
<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/account_authenticator"/>
</service>
</application>
</manifest>
3、 账号验证资源
在 res 目录下创建 xml 目录 , 在 xml 目录下创建 account_authenticator.xml 文件 ,
<?xml version="1.0" encoding="utf-8"?>
<account-authenticator
xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="keep_progress_alive.account"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" />
4、查看账号设置
只要在应用中注册了该服务 , 手机系统中安装了该应用 , 就可以在 " 设置 -> 账号 -> 添加账号 " 界面中 , 查看到该应用的账号 ;
三、 源码资源
源码资源 :
相关文章
- Android 弹出对话框Dialog充满屏幕宽度
- Android NFC技术(三)——初次开发Android NFC你须知道NdefMessage和NdefRecord
- 《Android开发案例驱动教程》
- 自行修改android.jar使其包含隐藏api
- 最新Android音视频开发进阶指北,音视频入门必备
- Android中Home键的监听和拦截示例
- Android 进入其他app和退出App杀死进程的方法详解
- Android 系统当前时间与网络时间同步
- 深入理解Android音视频同步机制(一)ExoPlayer的avsync逻辑
- android studio - gradle-6.5 - 同步gradle 时报错 你的主机中的软件中止了一个已建立的连接
- Android 属性动画简单分析(一)
- 【Android Gradle 插件】DexOptions 配置 ⑤ ( additionalParameters 属性配置 | --minimal-main-dex 参数最小化主 dex 字节码 )
- 【Android 进程保活】应用进程拉活 ( 账户同步拉活 | 账户同步 | 源码资源 )
- 使用天天模拟器开发android应用
- Android自己定义组件系列【8】——面膜文字动画
- Android在线源码
- android NDK编程:使用posix多线程与mutex相互排斥同步
- Android Service分类
- Win10 WSL编译Android开启ccache(提高编译速度)
- Android的16ms和垂直同步以及三重缓存