我正在开发一个应用程序,它会在收到" ACTION_SCREEN_ON"后启动countDownTimer。 我有以下工作代码。我只是想在指示的位置实现倒数计时器代码。
package com.example.myapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class Receiver extends BroadcastReceiver {
public static final String TAG = "Receiver Tutorial";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null && Intent.ACTION_SCREEN_ON.equals(action)) {
Toast.makeText(context, "Screen is turned ON", Toast.LENGTH_LONG).show();
// Place for CountDown Timer Code
}
}
}
以下是我从Android Developers Page获得的CountDownTimer代码。
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
但我不知道如何在第一段代码中实现它。
答案 0 :(得分:1)
尝试以下方式,
public class Receiver extends BroadcastReceiver
{
public static final String TAG = "Receiver Tutorial";
private CountDownTimer countDownTimer;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
countDownTimer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
}
};
if (action != null && Intent.ACTION_SCREEN_ON.equals(action)) {
Toast.makeText(context, "Screen is turned ON", Toast.LENGTH_LONG).show();
countDownTimer.start();
}
}
}
答案 1 :(得分:0)
public class Receiver extends BroadcastReceiver {
public static final String TAG = "Receiver Tutorial";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null && Intent.ACTION_SCREEN_ON.equals(action)) {
Toast.makeText(context, "Screen is turned ON", Toast.LENGTH_LONG).show();
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
}
}
}