zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

【Android 进程保活】应用进程拉活 ( 账户同步拉活 | 账号服务注册 | 源码资源 )

2023-09-14 09:07:31 时间





一、 账户简介



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、查看账号设置


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

在这里插入图片描述





三、 源码资源



源码资源 :

在这里插入图片描述