A new method to improve the survival rate of Android application processes (Part 2)

A new method to improve the survival rate of Android application processes (Part 2)

[[179903]]

Continued from previous article

  • Create Account Service
  1. public class XXAuthService extends Service {
  2. private XXAuthenticator mAuthenticator;
  3.   
  4. @Override
  5. public void onCreate() {
  6. mAuthenticator = new XXAuthenticator(this);
  7. }
  8.   
  9. private XXAuthenticator getAuthenticator() {
  10. if (mAuthenticator == null )
  11. mAuthenticator = new XXAuthenticator(this);
  12. return mAuthenticator;
  13. }
  14.   
  15. @Override
  16. public IBinder onBind(Intent intent) {
  17. return getAuthenticator().getIBinder();
  18. }
  19.   
  20. class XXAuthenticator extends AbstractAccountAuthenticator {
  21. private final Context context;
  22. private AccountManager accountManager;
  23. public XXAuthenticator(Context context) {
  24. super(context);
  25. this.context = context;
  26. accountManager = AccountManager.get(context);
  27. }
  28.   
  29. @Override
  30. public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options)
  31. throws NetworkErrorException {
  32. // Add account sample code
  33. final Bundle bundle = new Bundle();
  34. final Intent intent = new Intent(context, AuthActivity.class);
  35. intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
  36. bundle.putParcelable(AccountManager.KEY_INTENT, intent);
  37. return bundle;
  38. }
  39.   
  40. @Override
  41. public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options)
  42. throws NetworkErrorException {
  43. // Authentication sample code
  44. String authToken = accountManager.peekAuthToken(account, getString(R.string.account_token_type));
  45. //if not , might be expired, register again
  46. if (TextUtils.isEmpty(authToken)) {
  47. final String password = accountManager.getPassword(account);
  48. if ( password != null ) {
  49. //get new token
  50. authToken = account.name + password ;
  51. }
  52. }
  53. //without password , need to sign again
  54. final Bundle bundle = new Bundle();
  55. if (!TextUtils.isEmpty(authToken)) {
  56. bundle.putString(AccountManager.KEY_ACCOUNT_NAME, account. name );
  57. bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
  58. bundle.putString(AccountManager.KEY_AUTHTOKEN, authToken);
  59. return bundle;
  60. }
  61.   
  62. // no account data at   all , need to do a sign
  63. final Intent intent = new Intent(context, AuthActivity.class);
  64. intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
  65. intent.putExtra(AuthActivity.ARG_ACCOUNT_NAME, account. name );
  66. bundle.putParcelable(AccountManager.KEY_INTENT, intent);
  67. return bundle;
  68. }
  69.   
  70. @Override
  71. public String getAuthTokenLabel(String authTokenType) {
  72. // throw new UnsupportedOperationException();
  73. return   null ;
  74. }
  75.   
  76. @Override
  77. public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
  78. return   null ;
  79. }
  80.   
  81. @Override
  82. public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options)
  83. throws NetworkErrorException {
  84. return   null ;
  85. }
  86.   
  87. @Override
  88. public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options)
  89. throws NetworkErrorException {
  90. return   null ;
  91. }
  92.   
  93. @Override
  94. public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features)
  95. throws NetworkErrorException {
  96. return   null ;
  97. }
  98. }
  99. }

  • Declare Account Service
  1. <service
  2. android: name = "**.XXAuthService"  
  3. android:exported= "true"  
  4. android:process= ":core" >
  5. <intent-filter>
  6. < action  
  7. android: name = "android.accounts.AccountAuthenticator" />
  8. </intent-filter>
  9. <meta-data
  10. android: name = "android.accounts.AccountAuthenticator"  
  11. android:resource= "@xml/authenticator" />
  12. </service>

The authenticator is:

  1. <?xml version= "1.0" encoding= "utf-8" ?>
  2. <account-authenticator xmlns:android= "http://schemas.android.com/apk/res/android"  
  3. android:accountType= "@string/account_auth_type"  
  4. android:icon= "@drawable/icon"  
  5. android:smallIcon= "@drawable/icon"  
  6. android:label= "@string/app_name"  
  7. />

  • Using Account Services

Same as SyncAdapter, used through AccountManager

Token application is mainly through AccountManager.getAuthToken) series of methods

. Adding an account is done through AccountManager.addAccount)

. Check if the account exists through AccountManager.getAccountsByType)

Refs

  • WeChat Android client background keep alive experience sharing
  • Android Low Memory Killer Principle
  • The dual service method introduced on stackOverflow
  • Write your own Android Sync Adapter
  • Write your own Android Authenticator
  • Android developer
    • android.accounts
    • AccountManager
    • AbstractAccountAuthenticator
    • AccountAuthenticatorActivity
    • Creating a Sync Adapter
  • Android article: Implementing from the bottom layer to prevent the process from being killed (failed Closed)
  • Use of NotificationListenerService in Android 4.3+
  • Going multiprocess on Android

<<:  A new method to improve the survival rate of Android application processes (Part 1)

>>:  A problem that needs to be avoided when using JSONObject

Recommend

Boiling tea around the fire, someone got infected

When the weather gets cold “Tea brewing around th...

Uncover the 4 core algorithms behind Tik Tok to help you easily become popular

On TikTok , being on the hot recommendations is t...

Brand naming system methodology!

Recently, I took on a project to help a preschool...

Lock in the bull's IPOs in Hong Kong to make steady profits

Introduction to the practical course resources fo...

Should you turn on iOS privacy tracking? You will understand after reading this

The official version of iOS14.5 has been released...

How can 5G mobile networks unlock advanced digital transformation?

5G connectivity is critical to facilitating the o...

NDRC releases electric vehicle electricity price policy: not expensive

The National Development and Reform Commission iss...

11 iOS project applications with high recommendation index on Github

iOS is a mobile operating system developed by App...

Tesla delays electric truck launch due to lack of production capacity

The production capacity problem of Tesla's ne...