0

変数の変更を確認する必要があります。変更が発生した場合は、テキストビューを更新します。

そのため、この目的のために while ループを使用して新しいスレッドを作成しました。Thread.sleep() を介して、1 秒ごとに変数をチェックします

このスレッドは、onCreate() で作成および開始されました。なので1回です。

問題は、携帯電話を(縦から横に、または...)ひっくり返すたびに、新しいスレッドが作成されることです。

ここに私のコードがあります:

public class HomeActivity extends Activity
{
private final static int LOAD_SUCCESSFULL = 1;
private final long LOCATION_THREAD_SLEEP = 1000;    
private boolean flag = false;   
static TextView lat;
static TextView lon;    
static Location currentLocation = null; 
Thread locationThread;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);        
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.new_home2);
    this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.new_home_titlebar);

    lat = (TextView)findViewById(R.id.t2rt3);
    lon = (TextView)findViewById(R.id.t2rt4);

    /* FindLocation class is a helper class that find location via simcard or gps in separate thread,
        same problem happen with this thread also, it create multiple thread, for ease of work i 
        commented this part.
    */      
    //FindLocation fn = new FindLocation(this);

    locationThread = new Thread(null, loadLocation, "loadLocationHomePage");

    locationUpdater();
}

private static Handler locationUpdateHandler = new Handler()
{
    public void handleMessage(Message msg)
    {
        switch(msg.what)
        {
        case LOAD_SUCCESSFULL:
            lat.setText(Double.toString(currentLocation.getLatitude()));
            lon.setText(Double.toString(currentLocation.getLongitude()));
            //stopThread();
            break;              
        }
    }
};

private Runnable loadLocation = new Runnable()
{
    public void run()
    {
        //boolean flag = false;
        while(!flag)
        {
            if(Data.currLocation != null)
            {                   
                currentLocation = new Location(Data.currLocation);                  
                Message msg = locationUpdateHandler.obtainMessage(LOAD_SUCCESSFULL);                    
                locationUpdateHandler.sendMessage(msg); 
                //return;
                flag = true;
                //return;
            }               
            else
            {
                try 
                {
                    Thread.sleep(LOCATION_THREAD_SLEEP);
                } 
                catch (InterruptedException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }               
        }
    }
};

public void locationUpdater()
{
    //Thread locationThread = new Thread(null, loadLocation, "loadLocationHomePage");
    locationThread.start();
}

どうすればこれを解決できますか?

ここに画像の説明を入力

4

3 に答える 3

0

Androidがこれを行う理由がわかりません。コードを onResume() に入れた場合、この動作は理にかなっていますが、私の場合はわかりません。

とにかく、私は汚い解決策を見つけました。アイデアは、すべてのスレッドのリストを見つけてそれらを検索し、存在する場合は別のスレッドを作成しないようにすることです。

public boolean checkThreadExist()
{
    Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
    Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]);
    for(int i = 0; i < threadArray.length ; i++)
    {
        if(threadArray[i].getName().equalsIgnoreCase("loadLocationHomePage"))
            return true;
    }

    return false;
}

更新された onCreate() :

if(checkThreadExist())
    {

    }
    else
    {
        locationThread = new Thread(null, loadLocation, "loadLocationHomePage");            
        locationUpdater();            
    }
于 2013-09-15T08:29:16.733 に答える
0

おそらく、可能な限り最も効率的な方法でこれを行っていないと思います。

しかし、単純に、複数のワーカー スレッドが生成されるのを防ぐにはどうすればよいかという質問の場合は、UIless フラグメントを調べる必要があります。

http://www.vogella.com/articles/AndroidFragments/article.html#headlessfragments1

于 2013-09-15T06:54:09.207 に答える