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

Dragon Boat Festival marketing tactics, take it and don’t thank me!

In the second half of the Internet, the performan...

Who is stronger, the gorilla or the sapphire?

Large-screen smartphones have driven the developm...

Under the new circumstances, what minefields must self-media stay away from?

This afternoon, the industry broke the news: In o...

AION Y Plus is a king bomb, with full intelligent driving range

AION Y Plus 610 Smart Driving Edition is launched...

Analysis of Taobao APP user system operation

This article attempts to be a systematic design g...

How to prepare a brand marketing promotion plan?

Writing a branding plan may seem difficult becaus...

Living alone for 16 years, without mating, it gave birth to a child

After living alone for many years and never seein...

On Mother's Day, I wrote copy for Jiang Xiaobai, Durex, and NetEase Cloud Music!

Durex and Jiang Xiaobai took advantage of the pos...

x Improve various leg shape training methods basic version

x Improve various leg shape training methods Basic...

Neuroscience tells you: How to cultivate user habits?

As Internet business matures, it has become a con...

Is it reliable to use parasites to treat tumors?

Studies by multiple research groups have shown th...