2012-01-13 12 views
5

私はそれが存在することを願っています。AndroidはapplicationDidBecomeActiveおよびapplicationWillResignActiveと同等です(iOSから)

アプリケーションがフォーカスを失った時刻を保存しておき、ロックを起動するのにn分以上フォーカスが失われていないかどうか確認したいと思います。

アプリケーションがどのようにアクティビティから構成されているかを見て、私は直接の同等のものはないと思う。どのように私は同様の結果を達成することができますか?私はregisterActivityLifecycleCallbacks()にApplicationクラスを拡張しようとしましたが、それはAPIレベルで14+

答えて

4

私は後方互換性の解決策を作った方法を共有することを許可役立つかもしれません。

アカウントに関連付けられたパスコードがある場合、私は起動時に既に自分のアプリケーションのロックを実装していました。完全であるためには、他のアプリケーション(ホームアクティビティを含む)がn分かかる状態を処理する必要がありました。

すべてのアクティビティが拡張されるBaseActivityを作成しました。

// DataOperations is a singleton class I have been using for other purposes. 
/* It is exists the entire run time of the app 
    and knows which activity was last displayed on screen. 
    This base class will set triggeredOnPause to true if the activity before 
    "pausing" because of actions triggered within my activity. Then when the 
    activity is paused and triggeredOnPause is false, I know the application 
    is losing focus. 

    There are situations where an activity will start a different application 
    with an intent. In these situations (very few of them) I went into those 
    activities and hard-coded these lines right before leaving my application 

    DataOperations datao = DataOperations.sharedDataOperations(); 
    datao.lostFocusDate = new Date(); 
*/ 

import java.util.Date; 

import android.app.Activity; 
import android.content.Intent; 
import android.util.Log; 

public class BaseActivity extends Activity { 
    public boolean triggeredOnPause; 

    @Override 
    public void onResume(){ 
     super.onResume(); 
     DataOperations datao = DataOperations.sharedDataOperations(); 
     if (datao.lostFocusDate != null) { 
      Date now = new Date(); 
      long now_ms = now.getTime(); 
      long lost_focus_ms = datao.lostFocusDate.getTime(); 
      int minutesPassed = (int) (now_ms-lost_focus_ms)/(60000); 
      if (minutesPassed >= 1) { 
       datao.displayLock(); 
      } 
        datao.lostFocusDate = null; 
     } 
     triggeredOnPause = false; 
    } 

    @Override 
    public void onPause(){ 
     if (triggeredOnPause == false){ 
      DataOperations datao = DataOperations.sharedDataOperations(); 
      datao.lostFocusDate = new Date(); 
     } 
     super.onPause(); 
    } 
    @Override 
    public void startActivity(Intent intent) 
    { 
     triggeredOnPause = true; 
     super.startActivity(intent); 
    } 
    @Override 
    public void startActivityForResult(Intent intent, int requestCode) { 
     triggeredOnPause = true; 
     super.startActivityForResult(intent, requestCode); 
    } 

} 

あなたはこのソリューションを使用してトラブル私のDataOperationsクラスと同等のものを実装する必要があるとしている場合は、コメントと私は必要なコードを投稿することができますしてください。

2

のみ利用可能ですので、私はこのアプローチを使用することはできません実現

EDIT
はアンドロイドでApplication classを参照してください。このクラスを拡張する。

希望これはあなたが

+0

ありがとうございました。私はどこかで始めましょう。 –

関連する問題