2009-12-28 40 views

Trả lời

7

Đây là những gì bạn có thể làm:

  • sử dụng ApplicationManager.getForegroundProcessId()
  • sử dụng ApplicationManager.getVisibleApplications() để có được tất cả các ứng dụng đang chạy
  • sử dụng ApplicationManager.getProcessId() để tìm kiếm các ứng dụng bởi quá trình id
  • làm điều này trong TimerTask với khoảng thời gian được xác định

    public class AppListenerApp extends Application { 
    int mForegroundProcessId = -1; 
    
    public AppListenerApp() { 
        Timer timer = new Timer(); 
        timer.schedule(mCheckForeground, 2000, 2000);      
    } 
    
    public static void main(String[] args) { 
        AppListenerApp app = new AppListenerApp(); 
        app.enterEventDispatcher(); 
    } 
    
    TimerTask mCheckForeground = new TimerTask() { 
        public void run() { 
         int id = getForegroungProcessID(); 
         if(id != mForegroundProcessId) 
         { 
          mForegroundProcessId = id; 
          String name = 
           getAppNameByProcessId(mForegroundProcessId); 
          showMessage(name); 
         } 
        }; 
    }; 
    
    private int getForegroungProcessID() { 
        return ApplicationManager.getApplicationManager() 
          .getForegroundProcessId(); 
    } 
    
    private String getAppNameByProcessId(int id) { 
        String result = null; 
        ApplicationManager appMan = 
           ApplicationManager.getApplicationManager(); 
        ApplicationDescriptor appDes[] = 
           appMan.getVisibleApplications(); 
        for (int i = 0; i < appDes.length; i++) { 
         if (appMan.getProcessId(appDes[i]) == id) { 
          result = appDes[i].getName(); 
          break; 
         } 
        } 
        return result; 
    } 
    
    private void showMessage(String message) { 
        synchronized (Application.getEventLock()) { 
         Dialog dlg = new Dialog(Dialog.D_OK, message, 
             Dialog.OK, null, Manager.FIELD_HCENTER); 
         Ui.getUiEngine() 
             .pushGlobalScreen(dlg, 1, UiEngine.GLOBAL_QUEUE); 
        } 
    } 
    } 
    
+0

Cảm ơn bạn đã trả lời ... Thay thì đây là có bất kỳ API Listener hay bất kỳ loại sự kiện qua đó chúng ta sẽ nhận được Viện dẫn Foreground ứng dụng hiện hành. –

+0

Nếu ứng dụng của bạn, bạn luôn có thể sử dụng http://www.blackberry.com/developers/docs/4.5.0api/net/rim/device/api/system/Application.html#activate%28%29 sự kiện .. nhưng trong các trường hợp khác, tôi không thấy tùy chọn. –

+0

Ok cảm ơn bạn đã trả lời ur –

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