Android应用程序结构:预定线程

时间:2013-10-03 08:27:24

标签: java android multithreading android-activity

我的应用程序出现问题。我想要的是:

MainActivity推出&创建一个预定的线程,定期检查是否启用了WiFi服务。如果是,请转到新活动。如果不是那么就发出警告&将用户带到WiFi设置页面。

当用户返回主活动时,MainActivity代码现在将感知WiFi服务已启用&将它们发送到第二个活动。

我有这个工作。这是代码:

@Override
protected void onResume()
{
    super.onResume();

    ScheduledExecutorService oScheduledExecutor = Executors.newSingleThreadScheduledExecutor();

    try
    {
        oScheduledExecutor.scheduleAtFixedRate({RUNNABLE HERE}, 0, 5, TimeUnit.SECONDS);
    }
    catch (Exception e)
    {
        System.out.println("(MainActivity) Caught Exception here. #1");
        System.out.println("(MainActivity) Error: " + e.getMessage() + " Cause: " + e.getCause());
        e.printStackTrace();
    }
}

@Override
protected void onStart()
{
    super.onStart();

    // Assign WifiManager to System service
    oWiFiManager = (WifiManager) getSystemService(WIFI_SERVICE);

        // Create Runnable
        oWiFiUpdater = new Runnable() {

            @Override
            public void run()
            {

                // If we should show WiFi Disabled
                if (shouldShowWiFiAlert())
                {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            launchWiFiDisabledAlert();
                        }
                        });
                }

                        Intent oAPListIntent = new Intent(getApplicationContext(), APList.class);
                        startActivity(oAPListIntent);
            }
        }
};

但是,当我们处于第二个活动时,第一个线程仍在运行。我认为当从View中删除Activity时,所有线程都停止运行??

我希望执行程序仅在Activity可见时运行!任何想法!?

编辑:感谢来自njzk2的灵感

@Override
protected void onResume()
{
    super.onResume();

    createWiFiAlertDialog();

    boolean bWiFiEnabling = wifiEnabling();
    while (bWiFiEnabling)
    {
        try
        {
            doSyncedWait(500);
        }
        catch (Exception e)
        {
            System.out.println("(MainActivity) Exception caught waiting. " + e.getMessage());
        }
        bWiFiEnabling = wifiEnabling();
    }
    boolean bWiFiEnabled = wifiReady();
    if (!bWiFiEnabled)
    {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                AlertDialog oAlertDialog = m_oAlertDialog;
                oAlertDialog.show();
            }
        });
    }
    else
    {
        Intent oIntent = new Intent(this,APList.class);
        startActivity(oIntent);
    }
}

private boolean wifiEnabling()
{
    WifiManager oWiFiManager = m_oWiFiManager;
    if (oWiFiManager == null) return false;

    if (oWiFiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING) return true;
    return false;
}

private boolean wifiReady()
{
    WifiManager oWiFiManager = m_oWiFiManager;
    if (oWiFiManager == null) return false;

    // If the WiFi state is anything other than enabled, then wait.
    if (oWiFiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED) return true;
    return false;
}

1 个答案:

答案 0 :(得分:0)

如果您为runnable提供对活动的引用,并在trueonResume()false onPause()中设置布尔值为{{1}},则应该能够引用来自runnable的boolean,然后使用if语句仅在boolean设置为true时运行。