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

How can a novice do a good job in business promotion?

Judging from the current situation, the main prob...

Listen, the dinosaur is coughing!

Author: Wen Lele Dolly the Dinosaur Photo credit:...

How to write the Double 12 event plan? This universal solution is for you!

It feels like everyone is busy and exhausted from...

How can operations learn from user feedback and avoid detours?

Learning from user feedback is a big or small thi...

How to efficiently guide products to achieve self-propagation growth model

As a member of the growth department, when observ...

How do community apps stimulate users to produce high-quality content?

The product chain of a community product is: &quo...

Optimization method for Toutiao information flow advertising

Toutiao advertising optimization (taking games as...