如何在android中的另一个活动中暂停计时器

时间:2015-05-19 06:46:44

标签: android timer tabs

我有一个tabActivity

 public class MainActivity extends TabActivity {
          @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);


            Resources ressources = getResources(); 
            TabHost tabHost = getTabHost(); 

            // Android tab
            Intent intentAndroid = new Intent().setClass(getApplicationContext(), AndroidActivity.class);
            TabSpec tabSpecAndroid = tabHost
                .newTabSpec("Android")
                .setIndicator("android", ressources.getDrawable( R.drawable.icon_android_config )).setContent(intentAndroid);

            // Apple tab
            Intent intentApple = new Intent().setClass(getApplicationContext(), AppleActivity.class);
            TabSpec tabSpecApple = tabHost
                .newTabSpec("Apple")
                .setIndicator("apple", ressources.getDrawable(R.drawable.icon_apple_config)).setContent(intentApple);

            tabHost.addTab(tabSpecAndroid);
            tabHost.addTab(tabSpecApple);

            tabHost.setCurrentTab(2);
            }}

这是我的计时器:

 public class AndroidActivity extends Activity implements OnMapClickListener  {

  public static Timer timer1 = new Timer();

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
  timer1.scheduleAtFixedRate(new TimerTask() {

                    @Override
                    public void run() {
                     runOnUiThread(new Runnable() 
                     {
                      public void run() 
                      {  

//do some thing
                      }
                     });
                   }
                }, 0, 4000);

}

我想在此活动中暂停timer1:

public class AppleActivity extends Activity implements OnClickListener{

 public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

}

这是我的问题,当我在程序中更改选项卡时,程序没有调用'onCreate',我不知道我暂停计时器的位置以及我如何暂停计时器。 (我想当android选项卡处于活动状态时,timer1启动,当apple选项卡处于活动状态时,timer1停止)

3 个答案:

答案 0 :(得分:0)

试试这个。

AndroidActivity.timer1

它是public static对象,因此您可以随时随地获取。还有一件事,Timer类没有pause()方法。所以你必须使用cancel()

答案 1 :(得分:0)

使用此

     public class AndroidActivity extends Activity implements OnMapClickListener  {

      Timer timer1;

      @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.map);

    }
    @Override
        protected void onPause() {
            // TODO Auto-generated method stub
            super.onPause();
            timer1.cancel():
        }
        @Override
            protected void onResume() {
                // TODO Auto-generated method stub
                super.onResume();
 timer1 = new Timer();
    timer1.scheduleAtFixedRate(new TimerTask() {

                            @Override
                            public void run() {
                             runOnUiThread(new Runnable() 
                             {
                              public void run() 
                              {  

        //do some thing
                              }
                             });
                           }
                        }, 0, 4000);

        }
            }

答案 2 :(得分:0)

为什么你需要保持AndroidActivity活着?理想情况下,从AppleActivity导航到onPause时,您应该停止计时器Activity

在任何public void destroyTimer() { if (timer != null) { isTimerCancelled = true; timer.cancel(); timer = null; } } @Override protected void onPause() { super.onPause(); destroyTimer(); // do other stuffs } 你有计时器尝试使用它:

AndroidActivity:

        Dim cpuWMI As ManagementObjectSearcher = New ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Processor")
    For Each instance As ManagementObject In cpuWMI.Get()
        cpuspeedTXT.Text = instance.GetPropertyValue("CurrentClockSpeed").ToString
    Next