二、 账户同步代码示例
1、 账户同步 Service
package kim.hsl.keep_progress_alive.account_service; import android.accounts.Account; import android.app.Service; import android.content.AbstractThreadedSyncAdapter; import android.content.ContentProviderClient; import android.content.Context; import android.content.Intent; import android.content.SyncResult; import android.os.Bundle; import android.os.IBinder; import android.util.Log; public class AccountSyncService extends Service { // 账户同步 IBinder 对象 private ThreadSyncAdapter mThreadSyncAdapter; public AccountSyncService() { } @Override public IBinder onBind(Intent intent) { return mThreadSyncAdapter.getSyncAdapterBinder(); } @Override public void onCreate() { super.onCreate(); mThreadSyncAdapter = new ThreadSyncAdapter(getApplicationContext(), true); } class ThreadSyncAdapter extends AbstractThreadedSyncAdapter{ public ThreadSyncAdapter(Context context, boolean autoInitialize) { super(context, autoInitialize); } public ThreadSyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs) { super(context, autoInitialize, allowParallelSyncs); } @Override public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) { // 账户同步操作 // 与数据库 , 服务器同步操作 , 这里只是为了应用进程拉活 , 不实现具体的逻辑 Log.i("AccountSyncService", "账户同步拉活激活"); } } }
2、 账户同步 ContentProvider
package kim.hsl.keep_progress_alive.account_service; import android.content.ContentProvider; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; import androidx.annotation.NonNull; import androidx.annotation.Nullable; public class AccountSyncContentProvider extends ContentProvider { @Override public boolean onCreate() { return false; } @Nullable @Override public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) { return null; } @Nullable @Override public String getType(@NonNull Uri uri) { return null; } @Nullable @Override public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) { return null; } @Override public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) { return 0; } @Override public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) { return 0; } }
3、 AndroidManifest.xml 清单文件
<?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" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" android:maxSdkVersion="22" /> <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" android:maxSdkVersion="22" /> <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS"/> <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> <!-- 账户同步服务 --> <service android:name=".account_service.AccountSyncService" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.content.SyncAdapter" /> </intent-filter> <meta-data android:name="android.content.SyncAdapter" android:resource="@xml/sync_adapter" /> </service> <!-- 账户同步 ContentProvider --> <provider android:authorities="kim.hsl.keep_progress_alive.provider" android:name=".account_service.AccountSyncContentProvider" /> </application> </manifest>