如何在 android 中的另一个 activity 中暂停计时器
how to pause timer in another activity in android
我有一个 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);
}
我想在此 activity:
中暂停 timer1
public class AppleActivity extends Activity implements OnClickListener{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
这是我的问题,当我在程序中更改选项卡时,程序没有调用 'onCreate',而且我不知道我在哪里暂停计时器以及如何暂停计时器。
(我想要当 android 选项卡处于活动状态时,timer1 启动,当 apple 选项卡处于活动状态时,timer1 停止)
试试这个。
AndroidActivity.timer1
它是 public static
对象,因此您可以到达任何需要的地方。还有一件事 Timer class 没有 pause()
方法。所以你必须使用 cancel()
.
使用这个
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);
}
}
为什么你需要让你的 Activity
活着?理想情况下,从 AndroidActivity
导航到 AppleActivity
时,您应该在 onPause
.
中停止计时器
在任何 Activity
你有定时器尝试使用这个:
AndroidActivity:
public void destroyTimer() {
if (timer != null) {
isTimerCancelled = true;
timer.cancel();
timer = null;
}
}
@Override
protected void onPause() {
super.onPause();
destroyTimer();
// do other stuffs
}
我有一个 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);
}
我想在此 activity:
中暂停 timer1public class AppleActivity extends Activity implements OnClickListener{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
这是我的问题,当我在程序中更改选项卡时,程序没有调用 'onCreate',而且我不知道我在哪里暂停计时器以及如何暂停计时器。 (我想要当 android 选项卡处于活动状态时,timer1 启动,当 apple 选项卡处于活动状态时,timer1 停止)
试试这个。
AndroidActivity.timer1
它是 public static
对象,因此您可以到达任何需要的地方。还有一件事 Timer class 没有 pause()
方法。所以你必须使用 cancel()
.
使用这个
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);
}
}
为什么你需要让你的 Activity
活着?理想情况下,从 AndroidActivity
导航到 AppleActivity
时,您应该在 onPause
.
在任何 Activity
你有定时器尝试使用这个:
AndroidActivity:
public void destroyTimer() {
if (timer != null) {
isTimerCancelled = true;
timer.cancel();
timer = null;
}
}
@Override
protected void onPause() {
super.onPause();
destroyTimer();
// do other stuffs
}