2016-04-29 100 views
1

我有代碼的計時器,但如果我改變片段計時器被重置爲00:00我希望該計時器仍然計數後,我點擊停止或暫停或這意味着這個計時器仍然計數在後臺 如何做到這一點? 這是我的代碼,如何在後臺運行計時器?

public class TimerFragment extends BaseFragment { 
    private Button startButton; 
    private Button pauseButton; 
    private TextView timerValue; 
    private long startTime = 0L; 
    private Handler customHandler = new Handler(); 
    long timeInMilliseconds = 0L; 
    long timeSwapBuff = 0L; 
    long updatedTime = 0L; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     rootView = inflater.inflate(R.layout.fragment_timer, container, false); 

     initialize(); 
     //when start 

     startTime = SystemClock.uptimeMillis(); 
     customHandler.postDelayed(updateTimerThread, 0); 

     //pause 
     pauseButton.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View view) { 

       timeSwapBuff += timeInMilliseconds; 
       customHandler.removeCallbacks(updateTimerThread); 

      } 
     }); 

     return rootView; 
    } 

    private Runnable updateTimerThread = new Runnable() { 

     public void run() { 

      timeInMilliseconds = SystemClock.uptimeMillis() - startTime; 

      updatedTime = timeSwapBuff + timeInMilliseconds; 

      int secs = (int) (updatedTime/1000); 
      int mins = secs/60; 
      secs = secs % 60; 

      timerValue.setText("" + mins + ":" 
        + String.format("%02d", secs)); 
      customHandler.postDelayed(this, 0); 
     } 

    }; 

謝謝你的好意

回答

1

問題: -當您切換片段回來定時器片段的的onCreate生命週期回調將被調用,並且走了重置計時器

customHandler.postDelayed(updateTimerThread,0);

解決方案: -移動定時器邏輯到活動類,但活動將定向變化來重新使移動定時器邏輯到應用類: -

放棄這一類在你的項目包

import java.text.SimpleDateFormat; 
import java.util.Date; 

import android.app.Application; 
import android.os.Handler; 
import android.widget.Toast; 

public class App extends Application { 

    public static App appInstance; 
    private SimpleDateFormat dateFormat; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     appInstance = this; 

     dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 
    } 

    public void afficher() { 
     Toast.makeText(getBaseContext(), dateFormat.format(new Date()), 300).show(); 
     handler.postDelayed(runnable,1000); 
    } 

    public void startTimer() { 
     runnable.run(); 
    } 

    public void stopTimer() { 
     handler.removeCallbacks(runnable); 
    } 

    Handler handler = new Handler(); 
    Runnable runnable = new Runnable() { 
     public void run() { 
      afficher(); 
     } 
    }; 

} 

設置您的應用程序名稱應用程序類別像這樣下面(android名稱屬性的應用程序): -

<application 
     android:name="com.masterdetail_webview.App" 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.masterdetail_webview.TimertestActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

現在你可以就從任何地方

findViewById(R.id.button1).setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      App.appInstance.startTimer(); 

     } 
    }); 

    findViewById(R.id.button2).setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      App.appInstance.stopTimer(); 

     } 
    }); 

添加AppController的

停止計時器只是這樣

enter image description here

+0

的Java郎空指針異常,當我打電話App.appInstance.startTimer( ); 對不起,我在android新手:( –

+0

你必須申報清單中的應用程序名稱請參閱編輯參考 <應用程序 android:name =「<您的包名> .App」 –

+0

這是一個很好的解決方案將邏輯移到應用程序範圍,如果他需要在片段中顯示時間的問題,因爲你需要訪問片段的ui。我認爲將startTimer()更改爲startTimer(Hanlder處理函數)並在片段中實現處理程序會更好 –