一种提高Android应用进程存活率新方法(下)
接上文 创建 Account服务 publicclassXXAuthServiceextendsService{ privateXXAuthenticatormAuthenticator; @Override publicvoidonCreate(){ mAuthenticator=newXXAuthenticator(this); } privateXXAuthenticatorgetAuthenticator(){ if(mAuthenticator==null) mAuthenticator=newXXAuthenticator(this); returnmAuthenticator; } @Override publicIBinderonBind(Intentintent){ returngetAuthenticator().getIBinder(); } classXXAuthenticatorextendsAbstractAccountAuthenticator{ privatefinalContextcontext; privateAccountManageraccountManager; publicXXAuthenticator(Contextcontext){ super(context); this.context=context; accountManager=AccountManager.get(context); } @Override publicBundleaddAccount(AccountAuthenticatorResponseresponse,StringaccountType,StringauthTokenType,String[]requiredFeatures,Bundleoptions) throwsNetworkErrorException{ //添加账号示例代码 finalBundlebundle=newBundle(); finalIntentintent=newIntent(context,AuthActivity.class); intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE,response); bundle.putParcelable(AccountManager.KEY_INTENT,intent); returnbundle; } @Override publicBundlegetAuthToken(AccountAuthenticatorResponseresponse,Accountaccount,StringauthTokenType,Bundleoptions) throwsNetworkErrorException{ //认证示例代码 StringauthToken=accountManager.peekAuthToken(account,getString(R.string.account_token_type)); //ifnot,mightbeexpired,registeragain if(TextUtils.isEmpty(authToken)){ finalStringpassword=accountManager.getPassword(account); if(password!=null){ //getnewtoken authToken=account.name+password; } } //withoutpassword,needtosignagain finalBundlebundle=newBundle(); if(!TextUtils.isEmpty(authToken)){ bundle.putString(AccountManager.KEY_ACCOUNT_NAME,account.name); bundle.putString(AccountManager.KEY_ACCOUNT_TYPE,account.type); bundle.putString(AccountManager.KEY_AUTHTOKEN,authToken); returnbundle; } //noaccountdataatall,needtodoasign finalIntentintent=newIntent(context,AuthActivity.class); intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE,response); intent.putExtra(AuthActivity.ARG_ACCOUNT_NAME,account.name); bundle.putParcelable(AccountManager.KEY_INTENT,intent); returnbundle; } @Override publicStringgetAuthTokenLabel(StringauthTokenType){ //thrownewUnsupportedOperationException(); returnnull; } @Override publicBundleeditProperties(AccountAuthenticatorResponseresponse,StringaccountType){ returnnull; } @Override publicBundleconfirmCredentials(AccountAuthenticatorResponseresponse,Accountaccount,Bundleoptions) throwsNetworkErrorException{ returnnull; } @Override publicBundleupdateCredentials(AccountAuthenticatorResponseresponse,Accountaccount,StringauthTokenType,Bundleoptions) throwsNetworkErrorException{ returnnull; } @Override publicBundlehasFeatures(AccountAuthenticatorResponseresponse,Accountaccount,String[]features) throwsNetworkErrorException{ returnnull; } } } 声明Account服务 <service android:name="**.XXAuthService" android:exported="true" android:process=":core"> <intent-filter> <action android:name="android.accounts.AccountAuthenticator"/> </intent-filter> <meta-data android:name="android.accounts.AccountAuthenticator" android:resource="@xml/authenticator"/> </service> 其中authenticator为: <?xmlversion="1.0"encoding="utf-8"?> <account-authenticatorxmlns:android="http://schemas.android.com/apk/res/android" android:accountType="@string/account_auth_type" android:icon="@drawable/icon" android:smallIcon="@drawable/icon" android:label="@string/app_name" /> 使用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 本文作者:佚名 来源:51CTO