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

本文主要是介绍【Android 进程保活】应用进程拉活 ( 账户同步拉活 | 账号服务注册 | 源码资源 ),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 一、 账户简介
  • 二、 账号服务注册
    • 1、 服务 Service
    • 2、 AndroidManifest.xml 中注册 Service
    • 3、 账号验证资源
    • 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() {}@Overridepublic IBinder onBind(Intent intent) {return mAccountAuthenticator.getIBinder();}@Overridepublic void onCreate() {super.onCreate();// 创建账户验证器mAccountAuthenticator = new AccountAuthenticator(this);}/*** 账户验证器*/class AccountAuthenticator extends AbstractAccountAuthenticator {public AccountAuthenticator(Context context) {super(context);}@Overridepublic Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {return null;}@Overridepublic Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException {return null;}@Overridepublic Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options) throws NetworkErrorException {return null;}@Overridepublic Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {return null;}@Overridepublic String getAuthTokenLabel(String authTokenType) {return null;}@Overridepublic Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {return null;}@Overridepublic Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features) throws NetworkErrorException {return null;}}
}


2、 AndroidManifest.xml 中注册 Service


核心配置 :

        <!-- 用于账户同步拉活 --><serviceandroid:name=".account_service.AuthenticationService"android:enabled="true"android:exported="true" ><intent-filter><action android:name="android.accounts.AccountAuthenticator"/></intent-filter><meta-dataandroid: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" /><applicationandroid: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"--><activityandroid:name=".one_pixel_activity.OnePixelActivity"android:excludeFromRecents="true"android:taskAffinity="kim.hsl.keep_progress_alive.onepixel"android:theme="@style/OnePixelActivityTheme" /><!-- 用于提权的前台进程 --><serviceandroid:name=".foreground_service.ForegroundService"android:enabled="true"android:exported="true" /><!-- 用于提权的前台进程, 关闭通知操作 --><serviceandroid:name=".foreground_service.CancelNotificationService"android:enabled="true"android:exported="true" /><!-- 系统 Service 机制拉活 --><serviceandroid:name=".stick_service.StickService"android:enabled="true"android:exported="true" /><!-- 用于账户同步拉活 --><serviceandroid:name=".account_service.AuthenticationService"android:enabled="true"android:exported="true" ><intent-filter><action android:name="android.accounts.AccountAuthenticator"/></intent-filter><meta-dataandroid: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-authenticatorxmlns: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、查看账号设置


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

在这里插入图片描述





三、 源码资源



源码资源 :

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

在这里插入图片描述

这篇关于【Android 进程保活】应用进程拉活 ( 账户同步拉活 | 账号服务注册 | 源码资源 )的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/718478

相关文章

CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比

《CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比》CSS中的position属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布... css 中的 position 属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布局和层叠关

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

C++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操

canal实现mysql数据同步的详细过程

《canal实现mysql数据同步的详细过程》:本文主要介绍canal实现mysql数据同步的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的... 目录1、canal下载2、mysql同步用户创建和授权3、canal admin安装和启动4、canal

Windows的CMD窗口如何查看并杀死nginx进程

《Windows的CMD窗口如何查看并杀死nginx进程》:本文主要介绍Windows的CMD窗口如何查看并杀死nginx进程问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录Windows的CMD窗口查看并杀死nginx进程开启nginx查看nginx进程停止nginx服务

Linux实现线程同步的多种方式汇总

《Linux实现线程同步的多种方式汇总》本文详细介绍了Linux下线程同步的多种方法,包括互斥锁、自旋锁、信号量以及它们的使用示例,通过这些同步机制,可以解决线程安全问题,防止资源竞争导致的错误,示例... 目录什么是线程同步?一、互斥锁(单人洗手间规则)适用场景:特点:二、条件变量(咖啡厅取餐系统)工作流

Mysql的主从同步/复制的原理分析

《Mysql的主从同步/复制的原理分析》:本文主要介绍Mysql的主从同步/复制的原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录为什么要主从同步?mysql主从同步架构有哪些?Mysql主从复制的原理/整体流程级联复制架构为什么好?Mysql主从复制注意

Java进程CPU使用率过高排查步骤详细讲解

《Java进程CPU使用率过高排查步骤详细讲解》:本文主要介绍Java进程CPU使用率过高排查的相关资料,针对Java进程CPU使用率高的问题,我们可以遵循以下步骤进行排查和优化,文中通过代码介绍... 目录前言一、初步定位问题1.1 确认进程状态1.2 确定Java进程ID1.3 快速生成线程堆栈二、分析

Python使用Tkinter打造一个完整的桌面应用

《Python使用Tkinter打造一个完整的桌面应用》在Python生态中,Tkinter就像一把瑞士军刀,它没有花哨的特效,却能快速搭建出实用的图形界面,作为Python自带的标准库,无需安装即可... 目录一、界面搭建:像搭积木一样组合控件二、菜单系统:给应用装上“控制中枢”三、事件驱动:让界面“活”

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio