Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.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 函数在OnClick中工作,但在OnCreate()中不工作_Android_Countdowntimer - Fatal编程技术网

Android 函数在OnClick中工作,但在OnCreate()中不工作

Android 函数在OnClick中工作,但在OnCreate()中不工作,android,countdowntimer,Android,Countdowntimer,我试图为用户显示一个倒计时计时器。一个函数(myfunc)通过单击按钮就可以完美地工作。但我希望在创建活动后立即运行它。在Oncreate方法中。但是myfunc在OnCreate方法中不起作用 这是主活动的代码 public class MainActivity extends AppCompatActivity { private enum TimerState { STOPPED, RUNNING } private static final long TIMER_LEN

我试图为用户显示一个倒计时计时器。一个函数(myfunc)通过单击按钮就可以完美地工作。但我希望在创建活动后立即运行它。在Oncreate方法中。但是myfunc在OnCreate方法中不起作用

这是主活动的代码

public class MainActivity extends AppCompatActivity {
private enum TimerState {
    STOPPED,
    RUNNING
}

private static final long TIMER_LENGHT = 60; // Sixty seconds
private long mTimeToGo;
private CountDownTimer mCountDownTimer;
private TimerState mState;

@BindView(R.id.main_timer)
TextView mTimerText;

@BindView(R.id.main_timer_button)
Button mTimerButton;

PrefUtils mPreferences;
long startTime;
Calendar rightNow;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
    mState=TimerState.STOPPED;
    mPreferences = new PrefUtils(this);
    startTime = mPreferences.getStartedTime();
    rightNow = Calendar.getInstance();


            myfunc();



}

@Override
protected void onResume()
{
    super.onResume();
    initTimer();
    removeAlarm();
}
@Override
protected void onPause() {
    super.onPause();
    if (mState == TimerState.RUNNING) {
        mCountDownTimer.cancel();
        setAlarm();
    }
}
private long getNow()
{

    return rightNow.getTimeInMillis() / 1000;
}

private void initTimer()
{

    if (startTime >= 0)
    {
        mTimeToGo = (TIMER_LENGHT - (getNow() - startTime));
        if (mTimeToGo <= 0)
        { // TIMER EXPIRED
            mTimeToGo = TIMER_LENGHT;
            mState = TimerState.STOPPED;
            onTimerFinish();
        }
        else
        {
            startTimer();
            mState = TimerState.RUNNING;
        }
    }
    else
    {
        mTimeToGo = TIMER_LENGHT;
        mState = TimerState.STOPPED;
    }
    updateTimeUi();
}

private void onTimerFinish()
{

    Toast.makeText(getApplicationContext(),R.string.timer_finished,Toast.LENGTH_LONG).show();
    mTimerText.setText(R.string.timer_finished);
    mPreferences.setStartedTime(0);
    mTimeToGo=TIMER_LENGHT ;
    updateTimeUi();
}

private void updateTimeUi()
{
    mTimerText.setText(String.valueOf(mTimeToGo));
}

private void startTimer()
{
    mCountDownTimer = new CountDownTimer(mTimeToGo*1000 , 1000) {
        public void onTick(long millisUntilFinished)
        {
            mTimeToGo -= 1;
            updateTimeUi();
        }
        public void onFinish()
        {
            mState = TimerState.STOPPED;
            onTimerFinish();
            updateTimeUi();
        }
    }.start();
}


public void myfunc()
{
    if  (mState == TimerState.STOPPED) {
        mPreferences.setStartedTime(getNow());
        startTimer();
        mState = TimerState.RUNNING;
    }
}

public void setAlarm() {
    long wakeUpTime = (mPreferences.getStartedTime() + TIMER_LENGHT) * 1000;
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, TimerExpiredReceiver.class);
    PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        am.setAlarmClock(new AlarmManager.AlarmClockInfo(wakeUpTime, sender), sender);
    } else {
        am.set(AlarmManager.RTC_WAKEUP, wakeUpTime, sender);
    }
}

public void removeAlarm() {
    Intent intent = new Intent(this, TimerExpiredReceiver.class);
    PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.cancel(sender);
}
}

广播接收器也在这里

 @Override
public void onReceive(Context context, Intent intent)
{
    Intent i = new Intent(context, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pIntent = PendingIntent.getActivity(context, 0, i, 0);

    NotificationCompat.Builder b = new NotificationCompat.Builder(context);
    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    b.setSound(notification)
            .setContentTitle(context.getString(R.string.timer_finished))
            .setAutoCancel(true)
            .setContentText(context.getString(R.string.timer_finished))
            .setSmallIcon(android.R.drawable.ic_notification_clear_all)
            .setContentIntent(pIntent);

    Notification n = b.build();
    NotificationManager mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(0, n);
}

在调用myfunc之前,请调用initTimer一次。因为onResume将在onCreate之后调用,或者在onCreate中延迟几秒钟

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
mState=TimerState.STOPPED;
mPreferences = new PrefUtils(this);
startTime = mPreferences.getStartedTime();
rightNow = Calendar.getInstance();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
myfunc();     
}
}, 100);//Delay 100ms

}

将代码放入onResume您正在启动onResume中的计时器,该计时器在onCreate方法之后调用。这就是它不起作用的原因。解释一下你想做什么,我相信有一种更简单的方法,我使用一个固定的倒计时。即使用户终止活动,当用户终止活动后再次打开应用程序时,倒数计时器也应在终止活动后运行,并应显示经过的时间。现在剩下的时间当用户在切换应用程序后看到活动时,计时器将重新启动。因为它不应该从哪里开始。单击按钮后,直到时间结束后才会重新启动。即使应用程序被终止。在这种情况下,您也会延迟几毫秒,然后调用myfunc();onCreate仍然存在上述注释中提到的相同问题。当其reach onCreate方法时,MSState状态是什么?默认情况下,MSState=STOPPED。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
mState=TimerState.STOPPED;
mPreferences = new PrefUtils(this);
startTime = mPreferences.getStartedTime();
rightNow = Calendar.getInstance();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
myfunc();     
}
}, 100);//Delay 100ms

}