2012-01-13 29 views
5

Tôi hy vọng nó tồn tại.Android tương đương với applicationDidBecomeActive và applicationWillResignActive (từ iOS)

Tôi muốn lưu trữ thời gian khi ứng dụng mất tiêu điểm và sau đó kiểm tra xem ứng dụng có bị mất tiêu điểm trong hơn n-phút để hiển thị khóa hay không.

Xem cách ứng dụng bao gồm các hoạt động, tôi nghĩ rằng sẽ không có tương đương trực tiếp. Làm thế nào tôi có thể đạt được kết quả tương tự?

EDIT
Tôi cố gắng để mở rộng các lớp ứng dụng để registerActivityLifecycleCallbacks() và nhận ra rằng tôi sẽ không thể sử dụng phương pháp này vì nó là chỉ có sẵn trong API Cấp 14 +

Trả lời

4

Cho phép tôi chia sẻ cách tôi tạo giải pháp tương thích ngược.

Tôi đã triển khai khóa ứng dụng của mình khi khởi chạy nếu có mật mã được liên kết với tài khoản. Để hoàn thành, tôi cần phải xử lý các tình huống mà các ứng dụng khác (bao gồm cả hoạt động ở nhà) tiếp quản trong n-phút.

Tôi đã kết thúc tạo BaseActivity mà tất cả Hoạt động của tôi mở rộng.

// 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); 
    } 

} 

Nếu bạn định sử dụng giải pháp này và gặp sự cố khi triển khai lớp DataOperations, vui lòng nhận xét và tôi có thể đăng mã cần thiết.

2

Tham khảo Application class trong android. Mở rộng lớp này.

Hy vọng điều này có thể giúp bạn

+0

Cảm ơn, giờ tôi đã có nơi để bắt đầu. –

Các vấn đề liên quan