6

どのアプリケーションが開始され、何がフォアグラウンドに移動されるかをリッスンするバックグラウンド アプリケーションを作成したいと考えています。

返信してください 質問が明確でない場合は、再度説明します。

ありがとう

4

1 に答える 1

7

これがあなたにできることです:

  • ApplicationManager.getForegroundProcessId()を使用します
  • ApplicationManager.getVisibleApplications()を使用して、実行中のすべてのアプリを取得します
  • ApplicationManager.getProcessId()を使用して、プロセスIDでアプリを検索します
  • 定義された期間でTimerTaskでこれを行います

    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);
        }
    }
    }
    
于 2009-12-28T10:53:58.397 に答える