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

接上文

  • 创建
  • 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

本文作者:佚名

来源:51CTO

时间: 2024-09-08 12:57:23

一种提高Android应用进程存活率新方法(下)的相关文章

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

基础知识 Android 进程优先级 1 进程优先级等级一般分法 Activte process Visible Process Service process Background process Empty process 2 Service技巧 onStartCommand返回START_STICKY onDestroy中startself Service后台变前置,setForground(true) android:persistent = "true" 3 进程优先级号 P

4种提高多维数据分析的方法

[51CTO.com快译]联机分析处理(OLAP)需要有即时的响应,因此其性能是至关重要的.虽然其结构较为简单,但是在处理各种大的数据立方体(data cubes)时,会涉及到大量的计算. 常被称为OLAP(联机分析处理)的多维分析是一种交互式的数据分析过程,它包括:对于数据立方体(data cube)进行旋转(rotation).切片与切块(slice and dice).钻取(drill-down)等执行操作.其后端的计算结构较为简单,如下列SQL语句所示. SELECT D,..., SU

厌倦了编程书?来试试这3种提高编程技能的有趣方法吧

如果你曾经从书上学习编写代码,你就知道那有多乏味.为什么不试试一些激动人心的方法来使学习更有乐趣呢? fun1 下面介绍的这些网站每个都有自己独特的风格,但是它们都加入了游戏元素.这些稀奇古怪而有趣的游戏能够使你找回学习的乐趣. Code Combat 如果你正在学习JavaScript并且没有多少-或根本没有-编码经验,来试试这个免费的游戏.控制一个巫师和他的随从.关卡设置从简单的概念如预先编好的动作到带条件判断的行动到更高级的咒语例如计算. 学习过程是在一个可爱的魔幻RPG中进行,有骑士,食

提高Android Service 优先级的方法 .

  Android 系统对于内存管理有自己的一套方法,为了保障系统有序稳定的运信,系统内部会自动分配,控制程序的内存使用.当系统觉得当前的资源非常有限的时候,为了保 证一些优先级高的程序能运行,就会杀掉一些他认为不重要的程序或者服务来释放内存.这样就能保证真正对用户有用的程序仍然再运行.如果你的 Service 碰上了这种情况,多半会先被杀掉.但如果你增加 Service 的优先级就能让他多留一会,我们可以用 setForeground(true) 来设置 Service 的优先级. 为什么是

[收藏]五种提高 SQL 性能的方法

性能 五种提高 SQL 性能的方法发布日期: 4/1/2004 | 更新日期: 4/1/2004Johnny Papa Data Points Archive 有时, 为了让应用程序运行得更快,所做的全部工作就是在这里或那里做一些很小调整.啊,但关键在于确定如何进行调整!迟早您会遇到这种情况:应用程序中的 SQL 查询不能按照您想要的方式进行响应.它要么不返回数据,要么耗费的时间长得出奇.如果它降低了报告或您的企业应用程序的速度,用户必须等待的时间过长,他们就会很不满意.就像您的父母不想听您解释

五种提高SQL性能的方法

性能 五种提高 SQL 性能的方法Johnny Papa 有时, 为了让应用程序运行得更快,所做的全部工作就是在这里或那里做一些很小调整.啊,但关键在于确定如何进行调整!迟早您会遇到这种情况:应用程序中的 SQL 查询不能按照您想要的方式进行响应.它要么不返回数据,要么耗费的时间长得出奇.如果它降低了报告或您的企业应用程序的速度,用户必须等待的时间过长,他们就会很不满意.就像您的父母不想听您解释为什么在深更半夜才回来一样,用户也不会听你解释为什么查询耗费这么长时间.("对不起,妈妈,我使用了太多

一种实现Win32消息处理处理函数的新方法 - 基于Thunk实现的类成员消息处理函数

Windows是一个消息驱动的操作系统,在系统中发生的所有消息均需要通过消息处理过程(或叫窗口过程)进行处理.由于C++给我们在程序设计中带来更多的灵活性(如继承.重载.多态等),所以我们都希望能够使用C++的类来封装Windows中的窗口过程函数,但是Windows规定了窗口过程函数必须定义为一个全局函数,也就是说需要使用面向过程的方法来实现,为了使用面向对象的技术来实现消息处理,我们必须另辟它径.目前我们在网络上见得比较多的方式是使用Thunk将即将传递给窗口过程的第一个参数(HWND hW

五种提高 SQL 性能的方法_MsSql

发布日期: 4/1/2004 | 更新日期: 4/1/2004 Johnny Papa Data Points Archive 有时, 为了让应用程序运行得更快,所做的全部工作就是在这里或那里做一些很小调整.啊,但关键在于确定如何进行调整!迟早您会遇到这种情况:应用程序中的 SQL 查询不能按照您想要的方式进行响应.它要么不返回数据,要么耗费的时间长得出奇.如果它降低了报告或您的企业应用程序的速度,用户必须等待的时间过长,他们就会很不满意.就像您的父母不想听您解释为什么在深更半夜才回来一样,用户

Ajax: Web应用开发的一种新方法

ajax|web   原文:  http://www.neokeen.com/mornlee/2005/02/21/1108998494781.html Ajax是什么 异步JavaScript + XML(Asynchronous JavaScript + XML)的缩写.Ajax不是某种新技术,只是几种技术的集成创新的一种新方法.其技术组成有:基于标准的表示技术: XHTML , CSS动态显示和交互技术:Document Object Model(文件对象模型)数据互换和操作技术: XML