2013-04-23 133 views
-2

我似乎在這裏相當多!活動A開始活動B,如何關閉活動A

我知道這個問題已經被問已經在這裏和其他地方,我曾試圖實施發佈答案,而是基於一個if語句我

我有一個活動,(Activity A)它不工作,它開始Activity B。我的問題是Activity A一直在運行,並在Activity A我發出警報,並振動手機,兩者都繼續前進,而Activity B啓動和運行(我可以看到在logcat)它永遠不會因爲這一點前來。

這裏是我的我的if語句

 if (dynamicActivation > threshold) { 

        alarmHandler.post(new Runnable() { 
         public void run() { 
          int duration = Toast.LENGTH_SHORT; 
          CharSequence text = "YOUR CHILD HAS TRAVELLED OUTSIDE PROXIMITY"; 
          Context context = getApplicationContext(); 
          proximityAlert = Toast.makeText(context, text, 
            duration); 
          proximityAlert.show(); 
          alarmSound.start(); 
          //v.vibrate(3000); 

          try { 
           Thread.sleep(5000); 
           openMaps(); 

          } catch (InterruptedException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 
         } 
        }); 
       } 

代碼這裏是從openMaps()函數的代碼

public void openMaps() { 

    Class track = ParentReal.class; 
    Intent PRealIntent = new Intent(this, track); 

    PRealIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    startActivity(PRealIntent); 

    ANN.this.finish(); 


} 

我應該在的情況下添加它引起的問題是,還有另外一個活動,一個菜單,一旦一個按鈕被點擊,它會打開Activity A ...我應該關閉菜單活動以及一旦我開始Activity A

在此先感謝!

編輯

我不知道爲什麼downvote,但我會試着解釋我的問題,我有三個活動A,B,C A爲我的菜單,在按鈕點擊根據if語句的結果在活動B中啓動活動B活動C已啓動。

問題是C運行,進程可以在logcat中看到但不打開。 B繼續播放警報並震動,然後手機停止工作,警報和振動繼續進行。我有上面的if語句的代碼和openMaps()函數的代碼,這是基於if的結果調用的,其中我試圖打開Activity C並關閉B我希望更清晰;)

ANOTHER編輯

好吧,我有一個想法是什麼問題,我只是不知道如何解決它。我有一個線程,裏面的線程是一個無限的while循環,在這個無限的while循環內我檢查兩個座標之間的距離(我希望它們不斷更新)。

問題是,全局聲明的變量是在循環內初始化的,所以我必須在那個內部運行我的if語句,以訪問那些...我試着用一個布爾'測試',如果它是真的執行if聲明然後將測試設置爲false,但這不起作用,我很害怕。

這裏是我的代碼

Thread dist = new Thread(new Runnable() { 
     public void run() { 

      while (true) { 

       child = new Location("point A"); 

       child.setLatitude(Clatitude); 
       child.setLongitude(Clongitude); 

       parent = new Location("point B"); 

       parent.setLatitude(Platitude); 
       parent.setLongitude(Plongitude); 

       distance = child.distanceTo(parent); 
       DisRound = 550; 
       dynamicActivation = DisRound * weight;       

       try { 
        Thread.sleep(1000); 

        if (test) 
        { 
         if(dynamicActivation > threshold) 
         { 
          finish(); 
          new Timer().schedule(new MapsTimer(), 5000); 
          test = false;        
         } 
         Log.d("ACTIVATION", Boolean.toString(test)); 
        } 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     } 
    }); 
    dist.start(); 

和繼承人的MapsTimer任務

private class MapsTimer extends TimerTask { 
    @Override 
    public void run() { 
     runOnUiThread(new Runnable() { 

      public void run() { 
       int duration = Toast.LENGTH_SHORT; 
       CharSequence text = "YOUR CHILD HAS TRAVELLED OUTSIDE PROXIMITY"; 
       Context context = getApplicationContext(); 
       proximityAlert = Toast.makeText(context, text, duration); 
       proximityAlert.show(); 
       alarmSound.start();     

       openMaps(); 
      } 
     }); 
    } 
} 

我呼籲結束if語句中。我已經在這裏待了10個小時,它正在融化我的頭,我確信它很簡單,但只有當你知道如何!

問候, 加里

+0

在移動活動之前A到B使用完成語句。 – itsrajesh4uguys 2013-04-23 13:36:08

+0

你發佈了一個正在UI線程(主線程)上運行的'Runnable',並且在該'Runnable'中你睡了5秒鐘。至少,從您發佈的代碼中,我假設代碼正在UI線程中運行。那很不好。你不想阻塞UI線程5秒鐘。此外,振動和聲音不會阻止「活動」來到顯示器的前方。看看你的logcat,可能有其他跡象表明存在麻煩。不要過濾logcat,你可能會錯過重要的東西。 – 2013-04-23 17:15:28

+0

謝謝大衛,我已經改變了我的代碼,我知道問題是什麼,我只是不確定如何處理它。 – 2013-04-23 17:54:59

回答

0

謝謝大家爲了你的幫助,我自己整理了一下。我剛剛在while循環中插入了一個break。具體如下:

Thread dist = new Thread(new Runnable() { 
     public void run() { 

      while (true) { 

       child = new Location("point A"); 

       child.setLatitude(Clatitude); 
       child.setLongitude(Clongitude); 

       parent = new Location("point B"); 

       parent.setLatitude(Platitude); 
       parent.setLongitude(Plongitude); 

       distance = child.distanceTo(parent); 

       // DisRound = Math.round(distance * 100.0)/100.0; 
       DisRound = 550; 

       dynamicActivation = DisRound * weight;       

       try { 
        Thread.sleep(1000); 

         if(dynamicActivation > threshold) 
         { 
          proximityAlert.show(); 
          alarmSound.start(); 
          new Timer().schedule(new MapsTimer(), 5000); 
          break;       
         } 
         Log.d("ACTIVATION", Boolean.toString(test)); 

       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
      finish(); 
     } 
    }); 
    dist.start(); 

因此,一旦顯示報警,報警被打,並mapsTimer被調用,它打破了無限循環的,它不需要任何更多的,然後我調用finish() ;

1

你的問題,你的代碼是不明確的,但在一般情況之後,可切換到活動B,你應該調用finish(),它完成的活動代碼中的

+2

這應該是在評論而不是答案... – SilentKiller 2013-04-23 14:17:31

+0

你可以從我的代碼中看到我稱之爲finish(),但不幸的是它沒有,所以我需要知道我在做什麼錯,或者我可以要糾正它,如果有人能幫上忙,那就太好了 – 2013-04-23 14:38:57

相關問題