Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-如何通过按下按钮停止TimerTask_Android_Timer - Fatal编程技术网

Android-如何通过按下按钮停止TimerTask

Android-如何通过按下按钮停止TimerTask,android,timer,Android,Timer,我想做的是,让一个函数每10秒运行一次。 现在,我还试图让这个功能在用户按下退出按钮完成活动时停止。 但是如果用户在活动中按下home键或back键,那么我希望 函数在后台运行 为了做到这一点,我已经完成了接下来的几个步骤- 我已经在清单上声明了以下活动- <activity android:name="com.example.one.ViewList" > <intent-filter> <action and

我想做的是,让一个函数每10秒运行一次。 现在,我还试图让这个功能在用户按下退出按钮完成活动时停止。 但是如果用户在活动中按下home键或back键,那么我希望 函数在后台运行

为了做到这一点,我已经完成了接下来的几个步骤-

我已经在清单上声明了以下活动-

       <activity android:name="com.example.one.ViewList" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" >
            </action>

            <category android:name="android.intent.category.NORMAL" >
            </category>
        </intent-filter>
    </activity>
我还在活动代码中添加了下一个类-

    private class MyTimerTask extends TimerTask {
    @Override
    public void run() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
               //code to get and send location information
                    Log.d("MY TIMER TASK", "RUNNING");
            }
        });
    }
}
现在,在单击退出按钮时,我已经下载了下一个代码-

    myTimer.cancel();
  finish():
有趣的是,当活动正在运行,我按下退出按钮时,该功能肯定会停止

当我在活动中按下后退或主页按钮时,该功能正在后台按我希望的方式运行

但当我回到活动并按下退出按钮时,即使应用程序已关闭,该功能仍会在后台继续运行

那么,当我按下按钮而不是破坏活动时,如何停止此功能


感谢您提供的任何帮助

此代码经过测试并正在运行:

public class MainActivity extends Activity implements  OnClickListener {

private CountDownTimer countDownTimer;
private boolean timerHasStarted = false;
private Button startB;
public TextView text;
private final long startTime = 30 * 1000;
private final long interval = 1 * 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startB = (Button) this.findViewById(R.id.button);
        startB.setOnClickListener(this);
        text = (TextView) this.findViewById(R.id.timer);
        countDownTimer = new MyCountDownTimer(startTime, interval);
        text.setText(text.getText() + String.valueOf(startTime/1000));
    }
    @Override
    public void onClick(View v) {
    if (!timerHasStarted) {
    countDownTimer.start();
    timerHasStarted = true;
    startB.setText("STOP");
    } else {
    countDownTimer.cancel();
    timerHasStarted = false;
    startB.setText("RESTART");
    }
    }

public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}

@Override
public void onFinish() {
text.setText("Time's up!");
}

@Override
public void onTick(long millisUntilFinished) {
text.setText("" + millisUntilFinished/1000);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}
这是xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FF0000" >

<TextView
android:id="@+id/timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:paddingRight="10dip"
android:textSize="50dp" />

<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Start" />

</RelativeLayout>


so在OnClick方法中删除finish()。或者我不明白你的问题,我猜你不明白-我想在按下退出按钮时停止它-这就是为什么我使用finish.so在onDestroy方法中的活动中,你应该编写myTimer.cancel();物理退出按钮?或者应用程序中的按钮?很抱歉-appHi上有一个退出按钮,谢谢您的帮助。但我不明白我想让它做的事情应该放在哪里——是在onTick吗?我不明白你的意思。这也是倒计时,当它完成时,它不会再开始-我需要的东西,每隔几秒将再次运行
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FF0000" >

<TextView
android:id="@+id/timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:paddingRight="10dip"
android:textSize="50dp" />

<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Start" />

</RelativeLayout>