当前位置:主页 > 查看内容

一种提高Android应用进程存活率新方法(下)

发布时间:2021-09-19 00:00| 位朋友查看

简介:接上文 创建Account服务 public classXXAuthServiceextendsService{ privateXXAuthenticatormAuthenticator; @Override public voidonCreate(){ mAuthenticator=newXXAuthenticator(this); } privateXXAuthenticatorgetAuthenticator(){ if(mAuthenticator==……

接上文

  • 创建Account服务
  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. // 添加账号 示例代码 
  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. // 认证 示例代码 
  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.     } 
  • 声明Account服务
  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> 

其中authenticator为:

  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. /> 
  • 使用Account服务

同SyncAdapter,通过AccountManager使用

。申请Token主要是通过 AccountManager.getAuthToken)系列方法

。添加账号则通过 AccountManager.addAccount)

。查看是否存在账号通过 AccountManager.getAccountsByType)

Refs

  • 微信Android客户端后台保活经验分享
  • Android Low Memory Killer原理
  • stackOverflow 上介绍的双Service方法
  • Write your own Android Sync Adapter
  • Write your own Android Authenticator
  • Android developer
    • android.accounts
    • AccountManager
    • AbstractAccountAuthenticator
    • AccountAuthenticatorActivity
    • Creating a Sync Adapter
  • Android篇从底层实现让进程不被杀死(失效Closed)
  • Android 4.3+ NotificationListenerService 的使用
  • Going multiprocess on Android

本文转载自网络,原文链接:http://mp.weixin.qq.com/s/reyCha9ckRDkHf8ttCeSAA
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文

  • 周排行
  • 月排行
  • 总排行

随机推荐