gpt4 book ai didi

java - 闹钟安卓应用

转载 作者:行者123 更新时间:2023-12-02 00:23:28 24 4
gpt4 key购买 nike

谁能帮我解决我的问题吗?我必须让闹钟在当前时间 10 分钟后响起,并且必须每 10 分钟重复一次,我编写了此代码,但它不起作用,它随机启动:"

public class AlarmNewActivity extends Activity {
/** Called when the activity is first created. */

Intent intent;
PendingIntent sender;
AlarmManager am;
Button bStart, bStop ;
long mCurrentTime, firstTime ;
Calendar calendar;
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

bStart = (Button) findViewById (R.id.button1);
bStop = (Button) findViewById (R.id.button2);
tv = (TextView) findViewById (R.id.textView1);

intent = new Intent(AlarmNewActivity.this, RepeatingAlarm.class);
sender = PendingIntent.getBroadcast(AlarmNewActivity.this, 0, intent, 0);
am = (AlarmManager)getSystemService(ALARM_SERVICE);
calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());


mCurrentTime = calendar.getTimeInMillis();


bStart.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
tv.setText(""+ firstTime + "\n" + mCurrentTime );

am.setRepeating(AlarmManager.RTC_WAKEUP,
mCurrentTime + 10 *1000, 5*1000, sender);
new Handler().postDelayed(new Runnable() {
public void run() {
bStop.performClick();
}
}, ( mCurrentTime + 50 * 1000 ));

}
});


//==================================================================================

bStop.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

am.cancel(sender);
}
});
}
}

最佳答案

mCurrentTime = calendar.getTimeInMillis();

应该在 onClick 内部以获取点击时的当前时间

public void onClick(View v) {

mCurrentTime = calendar.getTimeInMillis();
...
...
}

关于java - 闹钟安卓应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10484701/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com